Is it possible to directly use C ++ with the Metal API

Since Metal Language is also based on C ++ 11, and C ++ would seem to fit perfectly, which is a proven runtime language, I am looking for a fully bypass Objective-C / Swift. I would like to stay in the C ++ arena. Is there a possibility?

+4
source share
3 answers

Technically yes, but that would be extremely ugly and could break some other parts of Objective-C.

Objective-C, like C ++, started working as a preprocessor. One of the advantages of this is that each Objective-C method is also displayed as a C function call, which takes an object instance and a method selector as the first two arguments, respectively, and then the other declared arguments in order from left to right.

Both NSObjectand C calls that make up the Objective-C runtime can search for the current C function that will be called to invoke the method.

That way, you could create a C ++ class that grabbed all the C function pointers (it could even do this from the C runtime, making it pure C ++ code, not Objective-C ++), and immediately jumped to the corresponding code like this.

: Objective-C (.. C ), , , , . C, - , , . , .

, ++ Objective-C ++ , .

class MTLArray {
    id m_instance;
    static NSUInteger (* s_arrayLength)(id object, SEL selector);
};

MTLArray::MTLArray() {
    // assuming you use the m_ pattern for instance variables
    m_instance = [MTLArray new];

    // assuming you use s_ for static variables; also pretending
    // the mapping from method to C function will never change —
    // KVO is the most prominent exception but otherwise you can
    // be exceedingly confident, albeit you'll be relying on
    // empirical behaviour, not the formal contract
    if(!s_arrayLength) {
        s_arrayLength = [MTLArray instanceMethodForSelector:@selector(arrayLength)];
    }
}

NSUInteger MTLArray::getArrayLength() {
    return s_arrayLength(m_instance, @selector(arrayLength));
}

... +instanceMethodForSelector: , , . typedef , .

+5

API Objective-C, ++, - Objective-C ++. Objective-C, ++ C .

Objective-C Objective-C ++, ".m" ".mm". ( ++ .)

, ++ Objective-C, , std::vector, ++ 11, , .

+1

. API Objective-C. Objective-C ++, Objective-C .

0
source

All Articles