Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Namespaces and ObjC




2005/08/23 v 16:14, ObjM2:
Now, if you want to use a class written in ObjM2 from within ObjC code, then you'd have to know the prefixing scheme and refer to the class in its raw prefixed name, like ...

ObjM2_FooLib__FooClass *fooclass = [[ObjM2_FooLib__FooClass alloc] init];

that's not really going to fly with ObjC developers is it?

Of course you can do ...

#define FooClass ObjM2_FooLib__FooClass

... and the ObjM2 compiler could even generate the FooClass.h header file for ObjC accordingly including the macros, but this is not going to help with strings which represent class names because the preprocessor won't replace any occurences of FooClass inside a string, or will it?!

Again, the ObjM2 compiler could insert a macro ...

#define CLASSNAME(x) "ObjM2_FooLib__" # x

... but this requires ObjC developers to use CLASSNAME(FooClass) everywhere in the ObjC code where they would otherwise just use "FooClass" which is much shorter, though in this case it would be incorrect.


There are other more important problems. I don't think this one should be a concern.


One solution would be a "flattening" keyword on the ObjM2 side (not very flexible) and other solution would be an importing function on the ObjC side and that would be pretty easy and flexible.

Cosider the following code which imports FooBarCo_ClassX as ClassX. Note that after the importClass() call, ClassX is *fully* equivalent to FooBarCo_ClassX, there's actually no way to tell which one is original and which one is imported from this point on.

#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>

static void importClass(const char* className, const char* importedName);

static void importClass(const char* className, const char* importedName){
Class class = objc_getClass(className);
if(!class)
return;
Class importedClass = calloc(2, sizeof(struct objc_class));
Class importedMetaClass = importedClass+1;


memcpy(importedClass, class, sizeof (struct objc_class));
memcpy(importedMetaClass, class->isa, sizeof (struct objc_class));



importedClass->isa = importedMetaClass;
importedClass->name = malloc(strlen (className) + 1);
importedMetaClass->name = importedClass->name;
strcpy((char*)importedClass->name , importedName);


    objc_addClass(importedClass);
}


@interface FooBarCo_ClassX:NSObject{ int foo; } - (int) foo; - (void) setFoo:(int)aFoo; @end

@implementation FooBarCo_ClassX:NSObject
- (int)        foo { return foo; }
- (void)    setFoo:(int)aFoo { foo = aFoo; }
@end

@class ClassX; /* Full interface declaration can be used to allow copile-time checks. */

int main (int argc, const char * argv[]) {
    importClass("FooBarCo_ClassX","ClassX");
    ClassX*                obj        = [[ClassX alloc] init];

    if(!obj)
        return 1;

    [obj setFoo:5];
    NSLog(@"foo: %i",[obj foo]);

    return 0;
}



--
Adam Nohejl
Loki Software
mailto:email@hidden
http://lokisw.com


_______________________________________________ Do not post admin requests to the list. They will be ignored. Objc-language mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden
References: 
 >Namespaces and ObjC (From: ObjM2 <email@hidden>)
 >Re: Namespaces and ObjC (From: glenn andreas <email@hidden>)
 >Re: Namespaces and ObjC (From: ObjM2 <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.