Note that there are some explanatory texts on larger screens.

plurals
  1. POeasing c++ to objective-c/cocoa bridging via metaprogramming?
    primarykey
    data
    text
    <p>In a pure C++ world we can generate interfacing or glue code between different components or interfaces at compile time, using a combination of template-based compile-time and runtime-techniques <em>(to e.g. mostly automatically marshall to/from calls using legacy types)</em>. </p> <p>When having to interface C++ applications with Objective-C/Cocoa for GUI, system integration or IPC though, things become harder due to the less strict typing - yet often not more then a flat repitive interface layer is needed: thin bridging delegates have to be defined or conversion code to language bridging calls has to be written. </p> <p>If you have to deal with interfaces of non-trivial size and want to avoid script-based code generation this quickly becomes cumbersome and is just a pain every time refactorings have to take place. Using a combination of (template) metaprogramming and the Objective-C runtime library, it should be possible to reduce the amount of code considerably... </p> <p>Before i go to reinvent the wheel <em>(and possibly waste time)</em>, does anyone know about techniques, best-practices or examples in that direction?</p> <hr> <p>As for an example, lets say we need a delegate that supports this informal protocol:</p> <pre><code>- (NSString*)concatString:(NSString*)s1 withString:(NSString*)s2; - (NSNumber*) indexOf:(CustomClass*)obj; </code></pre> <p>Instead of implementing an Obj-C class now that explicitly bridges to a C++-instance, i'd like to do <em>something like this</em> instead:</p> <pre><code>class CppObj { ObjcDelegate m_del; public: CppObj() : m_del(this) { m_del.addHandler &lt;NSString* (NSString*, NSString*)&gt; ("concatString", &amp;CppObj::concat); m_del.addHandler &lt;NSNumber* (CustomClass*)&gt; ("indexOf", &amp;CppObj::indexOf); } std::string concat(const std::string&amp; s1, const std::string&amp; s2) { return s1.append(s2); } size_t indexOf(const ConvertedCustomClass&amp; obj) { return 42; } }; </code></pre> <p>All that should be needed from the user to support additional types would be to specialize a conversion template function:</p> <pre><code>template&lt;class To, class From&gt; To convert(const From&amp;); template&lt;&gt; NSString* convert&lt;NSString*, std::string&gt;(const std::string&amp; s) { // ... } // ... </code></pre> <p>The example above of course does ignore support for formal protocols etc. but should get the point across. Also, due to the type-information for Objc-runtime-types being mostly decayed into <em>some-native-types or class-type</em> i don't think the explicit specification of parameter and return types for the delegate-methods can be avoided.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThis is a very interesting question. I do a lot of this kind of wrapping, and while I have a lot of patterns I use, I've never really found a metaprogramming approach to simplifying the patterns. It will be interesting to see if anyone has. C++ templates cannot be used to create ObjC classes, and C++ lacks sufficient run-time introspection to dynamically create ObjC classes. But still, some kind of template language might be appropriate; perhaps even macro based. I'm currently working on this subject for my next posting at Cocoaphany (robnapier.net), so I'll have to give it more thought.
      singulars
    2. COUnless i'm overlooking something, i could at least modify and extend Objective-C classes by using the Obj-C runtime (haven't investigated the class-registration part yet). Utilizing templates to inspect the C++ signatures to bind to etc. i don't see a big problem in that direction... at least not yet, one of the reasons for my asking :)
      singulars
    3. COIn projects I work on we use the following approach: XML+external code generator. I don't know if it is an option for you, but anyway... As far as I understood, this approach is painful in the beginning, but then you don't have to "reinvent" the solutions later each time. And it does not matter how many languages you use. And yes, in our case the XML file is generated as well: it is accessible via GUI frontend (but of course you can always change XML directly anyway).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload