On Feb 21, 2014, at 3:17 PM, [email protected] wrote:

> I have an NSNumber containing 5.
> The NSNumber  instance is a key in a dictionary collection.
> In order to obtain the value I need to pass a pointer to a memory location 
> containing 5 to Mono.
> However not all keys in the dictionary need be NSNumber instances, they may 
> be NSStrings.
> If this case to obtain the value I need to pass a pointer to a memory 
> location containing a Mono data structure.
> 
> So:
> [DBObject_key -monoPtr] returns a (void *).
> [NSString_key -monoPtr] returns a MonoObject *
> [NSNumber_key -monoPtr] returns say an int*.
> 
> So I want to be able to send the same message to any class and get an 
> appropriate void* pointer.

This is an unreasonable and nonsensical requirement.  NSNumber can hold 
floating point values and integers of varying sizes.  There's no one 
representation of its content, so there's no way for any recipient of a 
supposed "pointer to that content" (however that might be obtained) to 
interpret the pointer.

If you have some specific representation in mind, then don't use NSNumber.  Use 
a custom class wrapping the specific representation you want to use.


> This usage pattern, within a generated code environment too, makes the 
> imposition of an lvalue that I can take an address from undesirable.

However undesirable it may be, it's a consequence of you wanting an address for 
things which don't have an address.  The thing has to be stored, at least 
temporarily, in storage which has a proper address (and a predictable size and 
semantic interpretation).

> Plus it also needs to be an NSNumber, not a custom NSObject subclass.

Why?

If you really, really want to try to do this _and_ you can pick a single 
in-memory representation for all NSNumber content values, you could do 
something similar to what NSString does for -UTF8String.  You'll notice that 
that method's returned string has the lifetime of an autoreleased object, not 
the original string object.  I believe that it just creates an autoreleased 
NSData holding the UTF-8-encoded bytes and returns that NSData's -bytes.  So, 
you'd do something like:

@interface NSNumber (mugginsoft_MonoPtr)
- (void*) monoPtr;
@end

@interface NSNumber (mugginsoft_MonoPtr)
- (void*) monoPtr
{
        // NOTE:        This falls down for signed integer values and 
floating-point values.
        //              You could pick another type but it would have similar 
problems.
        unsigned long long value = [self unsignedLongLongValue];
        return [[NSData dataWithBytes:&value length:sizeof(value)] bytes];
}
@end

You may need to do this in a translation unit with ARC disabled so that the 
NSData really will end up in the autorelease pool rather than having a lifetime 
precisely restricted to your method.

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to