Array of functions

Discussion in 'Mac Programming' started by mchering, Jan 17, 2009.

  1. macrumors newbie

    Joined:
    Jan 17, 2009
    #1
    Hello everyone,

    I have a problem on how to implement an array of functions like it would look like in C++ code:

    static Item* (*chunkProcs[nrOfItemTypes])(QDataStream* in) = {
    Item::createFromStream,
    ItemB::createFromStream
    };

    How should this look like in Objective-C ?

    I tried something like this:

    static Item* (*chunkProcs[nrOfItemTypes])(NSMutableData* in) = {
    [Item createFromStream],
    [ItemB createFromStream]
    };

    and used this array like this:

    Item* item = chunkProcs:in;

    But this gives me an "initializer element is not constant" error.

    Can somebody help me with this?

    Peter
     
  2. macrumors 65816

    Flynnstone

    Joined:
    Feb 25, 2003
    Location:
    Cold beer land
    #2
    Not sure if I can help exactly, but ...

    Objective -C is C.
    So you should be able to have an array of pointers to functions. C style.
     
  3. thread starter macrumors newbie

    Joined:
    Jan 17, 2009
    #3
    Thanks for your reply, I also think that this should be possible with Objective-C but at the moment I don't get the right syntax for that problem :)
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    You probably want to use the SEL type and the @selector() directive.
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I thought this, too, but it seems that the OP is wanting to call factory methods in this way, and I honestly didn't know if you could get a SEL for a class method, and invoke it with NSInvocation (with the target as a class?), and NSObject's performSelector is an instance method. I started writing something up, but it got long, then i realized it might not even work for a factory method and gave up (finding myself too lazy, on a sit-around Saturday, to try it).

    I still don't know, after a few searches, if you can use class methods with this method. I would almost be inclined to have regular C functions that call the class methods and return the result, and keep pointers to those functions, but that isn't very fun.

    -Lee
     
  6. thread starter macrumors newbie

    Joined:
    Jan 17, 2009
    #6
    perhaps it is realisable with implementation pointers?
    Does anybody have an example for using them? Unfortunately i could only find some usenet faqs and they were not very comprehensible to me

    Peter
     
  7. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #7
    An IMP is a pointer to a function that returns an id and takes (id, SEL, ...) as its arguments.

    You get an IMP from a selector like this:

    Code:
    SEL mySelector = @selector(someMethodWithArg1:arg2:);
    IMP methodPtr = [myObj messageForSelector:mySelector];
    Then you call the method by dereferencing the function pointer. The first argument is self, the second is _cmd, and the other arguments follow.

    Code:
    methodPtr(myObj, mySelector, valueForArg1, valueForArg2);
    This should work for class methods too.
     

Share This Page