Changeset 7

Show
Ignore:
Timestamp:
06/11/06 15:20:38 (6 years ago)
Author:
Kashia
Message:

Added new Object system, see winmain

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Rakefile

    r6 r7  
    11require 'rake/clean' 
    22 
     3aresdir = '/Users/john/build/cvs/d/ares/ares' 
    34PROG = './testcoco.app/Contents/MacOS/testcoco' 
    45 
     
    89 
    910 
    10 SRC = FileList['winmain.d', 'objc_classinfos.d', 'docoa/objc/convenience.d'
     11SRC = FileList['winmain.d', 'objc_classinfos.d', 'docoa/objc/convenience.d', 'docoa/objc/objc_classinfos.d'
    1112 
    1213OBJ = SRC.ext('o') 
    1314 
    14 DFLAGS = "-Iares -g -fdebug"  
    15 raise "specify ares directory here and remove this line" 
     15DFLAGS = "-I#{aresdir} -g -fdebug"  
    1616LDFLAGS = "-framework Cocoa" 
    1717 
  • trunk/docoa/objc/convenience.d

    r5 r7  
    77 
    88private import docoa.objc.objc; 
     9private import docoa.objc.objc_classinfos; 
    910 
    1011private import docoa.corefoundation.cfstring; 
     
    1415private import docoa.foundation.nsobjcruntime; 
    1516 
    16 BOOL objc_create_class_definition(char[] name, char[] superclassName ) { 
    17     Class meta_class; 
    18     Class super_class; 
    19     Class new_class; 
    20     Class root_class; 
    21      
    22     // Ensure that the superclass exists and that someone 
    23     // hasn't already implemented a class with the same name 
    24     // 
    25     super_class = cast(Class)objc_lookUpClass(superclassName); 
    26     if (super_class is Nil) { 
    27         return NO; 
    28     } 
    29      
    30     if (cast(Class)objc_lookUpClass(name) !is Nil) { 
    31         return NO; 
    32     } 
    33      
    34     // Find the root class 
    35     // 
    36     root_class = super_class; 
    37     while(root_class.super_class !is Nil) { 
    38         root_class = root_class.super_class; 
    39     } 
    40      
    41     // Allocate space for the class and its metaclass 
    42     // 
    43     new_class = cast(Class)calloc(2, objc_class.sizeof); 
    44     if (!new_class) 
    45         throw new OutOfMemoryException(); 
    46     meta_class = &new_class[1]; 
    47      
    48      
    49     // setup class 
    50     new_class.isa   = meta_class; 
    51     new_class.info  = ObjCClassType.CLS_CLASS; 
    52     meta_class.info = ObjCClassType.CLS_META; 
    53      
    54     // Create a copy of the class name. 
    55     // For efficiency, we have the metaclass and the class itself  
    56     // to share this copy of the name, but this is not a requirement 
    57     // imposed by the runtime. 
    58     // 
    59     new_class.name = cast(char*)malloc(name.length + 1); 
    60     if (!new_class.name) 
    61         throw new OutOfMemoryException(); 
    62     memcpy(new_class.name, name.ptr, name.length); 
    63     meta_class.name = new_class.name; 
    64      
    65     // Allocate empty method lists. 
    66     // We can add methods later. 
    67     // 
    68     new_class.methodLists = cast(objc_method_list**)calloc(2, (objc_method_list*).sizeof); 
    69     if (!new_class.methodLists) 
    70         throw new OutOfMemoryException(); 
    71     *new_class.methodLists = cast(objc_method_list*)-1; 
    72     meta_class.methodLists = &new_class.methodLists[1]; 
    73     *meta_class.methodLists = cast(objc_method_list*)-1; 
    74      
    75     // Connect the class definition to the class hierarchy: 
    76     // Connect the class to the superclass. 
    77     // Connect the metaclass to the metaclass of the superclass. 
    78     // Connect the metaclass of the metaclass to the metaclass of  the root class. 
    79     // 
    80     new_class.super_class  = super_class; 
    81     meta_class.super_class = super_class.isa; 
    82     meta_class.isa         = root_class.isa; 
    83      
    84     // Set the sizes of the class and the metaclass. 
    85     // 
    86     new_class.instance_size  = super_class.instance_size; 
    87     meta_class.instance_size = meta_class.super_class.instance_size; 
    88      
    89     // Finally, register the class with the runtime. 
    90     // 
    91     objc_addClass(new_class); 
    92     return YES; 
     17class DocoaException : Exception 
     18
     19    this(char[] msg) { 
     20        super("Docoa Error: " ~ msg); 
     21    } 
     22     
     23    this(char[] msg, id obj) { 
     24        char[] str = msg; 
     25        str = str ~ "Info: " ~ get_info(obj); 
     26        super(str); 
     27    } 
     28     
     29    char[] get_info(id obj) { 
     30        Class klass = obj.isa; 
     31        char[] str = klass.name[0..(.strlen(klass.name))]; 
     32        return "Class name: " ~ str; 
     33    } 
    9334} 
    9435 
     
    10546    meta_class.info = ObjCClassType.CLS_META; 
    10647     
    107     new_class.name = cast(char*)malloc(name.length + 1); 
     48    new_class.name = cast(char*)calloc(1, name.length + 1); 
    10849    if (!new_class.name) 
    10950        throw new OutOfMemoryException(); 
     
    11152    meta_class.name = new_class.name; 
    11253     
    113     debug { writef("setting up class: %s\n", new_class.name); } 
     54    debug { writef("setting up class: %s\n", new_class.name[0..(.strlen(new_class.name))]); } 
    11455     
    11556    // Allocate empty method lists. 
     
    14384    objc_method_list* mlist; 
    14485     
    145     mlist = cast(objc_method_list*)calloc(1, objc_method_list.sizeof + objc_method.sizeof * (size+1)); 
     86    mlist = cast(objc_method_list*)calloc(1, objc_method_list.sizeof + objc_method.sizeof * size); 
    14687    if (!mlist) 
    14788        throw new OutOfMemoryException(); 
     
    187128    base_class = cast(Class)objc_lookUpClass(superclass_name); 
    188129    if (base_class is Nil) { 
    189         return null
     130        throw new DocoaException("No Such superclass: " ~ superclass_name)
    190131    } 
    191132     
     
    203144    // the same name 
    204145    // 
    205     debug { writef("class already implemented: %s\n", ((cast(Class)objc_lookUpClass(name) != Nil) ? "Yes" : "No")); } 
    206146    if ((intermediate_class = cast(Class)objc_lookUpClass(name)) != Nil) { 
     147        writef("Docoa Warning: Class already implemented, returning class."); 
    207148        return intermediate_class; 
    208149    } 
     
    214155        root_class = root_class.super_class; 
    215156    } 
    216     debug { writef("root class name: %s\n", root_class.name); } 
     157    debug { writef("root class name: %s\n", root_class.name[0..(.strlen(root_class.name))]); } 
    217158     
    218159    // Allocate space for the class and its metaclass 
     
    227168        meta_class,  
    228169        name, 
    229         base_class, 
    230         root_class, 
    231         base_class.instance_size, null, 
    232         null 
     170        base_class, root_class, 
     171        base_class.instance_size,  
     172        null, null 
    233173        ); 
    234174     
     
    241181    // 
    242182    objc_addClass(intermediate_class); 
    243     debug { writef("added class %s to runtime.\n", intermediate_class.name); } 
     183    debug { writef("added class %s to runtime.\n", intermediate_class.name[0..(.strlen(intermediate_class.name))]); } 
    244184     
    245185    return intermediate_class; 
     
    250190} 
    251191 
     192// FIXME: doesn't work 
    252193char[] fromCFString(CFStringRef cfstr) { 
    253194    char* cstr = null; 
     
    317258    printf("iv  offset: %i\n", var.ivar_offset); 
    318259} 
     260 
     261class ObjCClass { 
     262    Class klass; 
     263     
     264    this(char[] name, char[] base_class = "NSObject") { 
     265        klass = objc_build_class_with_base(name, base_class); 
     266        assert(klass != Nil); 
     267    } 
     268     
     269    void info() { 
     270        char[] name = klass.name[0..(.strlen(klass.name))]; 
     271        showClassInfos(name); 
     272    } 
     273     
     274    Class objc_class() { 
     275        return klass; 
     276    } 
     277     
     278    ObjCMethodList new_methodlist(size_t initial_size = 0) { 
     279        return new ObjCMethodList(initial_size); 
     280    } 
     281     
     282    void add_methods(ObjCMethodList mlst) { 
     283        objc_method_list* lst = mlst.lst; 
     284         
     285        debug { writefln("adding ", lst.method_count, " methods"); } 
     286         
     287        class_addMethods(klass, lst); 
     288    } 
     289     
     290    class ObjCMethodList { 
     291        objc_method_list* lst; 
     292        size_t max_methods; 
     293         
     294        this(size_t initial_size = 0) { 
     295            lst = objc_alloc_method_list(initial_size); 
     296            max_methods = initial_size; 
     297        } 
     298         
     299        void add(char[] name, char[] types, IMP imp) { 
     300            lst.method_count += 1; 
     301             
     302            if (lst.method_count > max_methods) { 
     303                objc_method_list* newlst = cast(objc_method_list*)realloc(lst, objc_method_list.sizeof + (objc_method.sizeof * lst.method_count)); 
     304             
     305                if (newlst is null) { 
     306                    throw new OutOfMemoryException(); 
     307                } 
     308                lst = newlst; 
     309                max_methods += 1; 
     310            } 
     311             
     312            objc_method* m = &((cast(objc_method*)&lst.method_list)[lst.method_count-1]); 
     313            objc_init_method(m, sel_registerName(name), types, imp); 
     314        } 
     315    } // end class ObjCMethodList 
     316} // end class ObjCClass 
  • trunk/main.d

    r5 r7  
    33private import std.stdio; 
    44 
    5 import objc_classinfos; 
     5private import docoa.objc.objc_classinfos; 
    66 
    77int main(char[][] args) { 
  • trunk/winmain.d

    r5 r7  
    99private import std.c.string; 
    1010 
    11 import objc_classinfos; 
     11private import docoa.objc.objc_classinfos; 
    1212 
    1313alias objc_object NSTableView; 
     
    1919*/ 
    2020class TestController { 
    21     static SEL[char[]] selectors; 
    22     static Class klass; 
    23      
     21    // data source for the table 
    2422    static char[][char[]][] table; 
    2523     
    2624    static id nitroProjectTable; // IBOutlet 
    2725     
    28     static this() { 
     26    // Contains an Objective-C class 
     27    static ObjCClass kl; 
     28     
     29    static this() 
     30    { 
     31        // this builds a objc class with a given base (empty second parameter  
     32        // would actually mean the same) 
     33        // 
     34        kl = new ObjCClass(this.classinfo.name, "NSObject"); 
    2935         
    30         selectors["testAction"] = sel_registerName("testAction:"); 
    31         selectors["testAction2"] = sel_registerName("testAction2:"); 
    32         selectors["numberOfRowsInTableView"] = sel_registerName("numberOfRowsInTableView:"); 
    33         selectors["tableView_objectValueForTableColumn_row"] = sel_registerName("tableView:objectValueForTableColumn:row:"); 
    34         selectors["setNitroProjTable"] = sel_registerName("setNitroProjTable:"); 
     36        // create a new method list, the parameter is only an advice, the  
     37        // function will aquire more space, if you go beyond it 
     38        // 
     39        auto mlist = kl.new_methodlist(5); 
    3540         
    36         klass = objc_build_class_with_base(this.classinfo.name, "NSObject"); 
     41        mlist.add("testAction:", "v8@0:4@", &testAction); 
     42        mlist.add("testAction2:", "v8@0:4@", &testAction2); 
     43        mlist.add("numberOfRowsInTableView:", "i8@0:4@", &numberOfRowsInTableView); 
     44        mlist.add("tableView:objectValueForTableColumn:row:", "@8@0:4@@i", &tableView_objectValueForTableColumn_row); 
     45        mlist.add("setNitroProjTable:", "v12@0:4@", &setNitroProjTable); 
    3746         
    38         int method_count = 5; 
    39         objc_method_list* lst = objc_alloc_method_list(method_count); 
    40         objc_add_method(lst, method_count, selectors["testAction"], "v8@0:4@", &testAction); 
    41         objc_add_method(lst, method_count, selectors["testAction2"], "v8@0:4@", &testAction2); 
    42         objc_add_method(lst, method_count, selectors["numberOfRowsInTableView"], "i8@0:4@", &numberOfRowsInTableView); 
    43         objc_add_method(lst, method_count, selectors["tableView_objectValueForTableColumn_row"], "@8@0:4@@i", &tableView_objectValueForTableColumn_row); 
    44         objc_add_method(lst, method_count, selectors["setNitroProjTable"], "v12@0:4@", &setNitroProjTable); 
    45          
    46         class_addMethods(klass, lst); 
     47        kl.add_methods(mlist); 
    4748         
    4849        table.length = 3; 
     
    7172      Data-sources for table view 
    7273      ALWAYS add (id self, SEL _cmd) to the beginning of functions, ObjC issue 
    73        
    74       idea: 
    75        
    76       NSPopUpButtonCell * popup = [[[NSPopUpButtonCell alloc] init] autorelease]; 
    77       [popup setBordered:NO]; 
    78       [popup addItemWithTitle:@"1"]; 
    79       //... 
    80       [col setDataCell:popup]; 
    8174    */ 
    8275    static int numberOfRowsInTableView(id self, SEL _cmd, NSTableView* aTableView) { 
     
    117110    id inst = class_createInstance(kl, kl.instance_size); 
    118111     
    119     objc_msgSend(inst, TestController.selectors["testAction"], kl); 
     112    objc_msgSend(inst, sel_getUid("testAction:"), kl); 
    120113     
    121114    printf("after obj test\n");