Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for all primitive types via NSValue #201

Closed
alexgarbarev opened this issue Mar 21, 2014 · 5 comments
Closed

Add support for all primitive types via NSValue #201

alexgarbarev opened this issue Mar 21, 2014 · 5 comments

Comments

@alexgarbarev
Copy link
Contributor

I mean adding support for all structures and types, using method value:withObjCType of NSValue.

For example:

PrimitiveStruct structure;
structure.fieldA = 23;
structure.fieldB = 32.1234;

NSValue *value = [NSValue value:&structure withObjCType:@encode(PrimitiveManStruct)];

Plan to implement:

  • Add length calculation to TyphoonTypeDescriptor (Use NSGetSizeAndAlignment function for that)
  • Refactor category NSValue+TCFInstanceBuilder to make it universal for all types
void *buffer = malloc(length);
[value getValue:buffer];
[invocation setArgument:buffer atIndex:_index];
free(buffer);
  • Replace KVC property injection by own implementation via NSInvocation
  • Try to catch custom setter and getter method names from runtime for properties (not just set)
@drodriguez
Copy link
Contributor

Replace KVC property injection by own implementation via NSInvocation

What’s the reason behind this intended change?

@alexgarbarev
Copy link
Contributor Author

KVC can't handle NSValue with non-nsvalue structs, pointers, c-strings, unions, selectors etc. I.e. KVC can't handle NSValue created by [NSValue value:&structure withObjCType:@encode(PrimitiveManStruct)]; method with custom types.
It crashes :-(

@drodriguez
Copy link
Contributor

Ok. Understood.

alexgarbarev added a commit that referenced this issue Mar 22, 2014
@alexgarbarev
Copy link
Contributor Author

Seems, like all works great. Now we can inject all primitive types in typhoon!
Unknown structs, unions, c-arrays and other via NSValue wrapper.
To do it - just init NSValue by value:withObjCType: method. Or use my macros:

#define NSValueFromPrimitive(primitive) ([NSValue value:&primitive withObjCType:@encode(typeof(primitive))])

Macros usage example:

//char *
char *string = "Hello world";
[definition injectProperty:@selector(cString) with:NSValueFromPrimitive(string)];

//Selector
[initializer injectParameterWith:NSValueFromPrimitive(@selector(selectorValue))];

//Structure
PrimitiveManStruct structure;
structure.fieldA = 23;
structure.fieldB = LONG_MAX;
[initializer injectParameterWith:NSValueFromPrimitive(structure)];

alexgarbarev added a commit that referenced this issue Mar 23, 2014
…rty injection for all cases except unwrapping NSValue/NSNumber
@jasperblues
Copy link
Member

Leaving this open so that I remember to update the documentation in the Typhoon 2.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants