<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ColonelPanic &#187; c++</title>
	<atom:link href="http://colonelpanic.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://colonelpanic.net</link>
	<description>Dumping our corps so you don&#039;t have to</description>
	<lastBuildDate>Mon, 30 Jan 2012 17:50:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Boost.Preprocessors Sequences</title>
		<link>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/</link>
		<comments>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 16:18:13 +0000</pubDate>
		<dc:creator>Georg Fritzsche</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[preprocessor]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=205</guid>
		<description><![CDATA[After recently answering a question on Stackoverflow using Boost.Preprocessor, i wondered how the sequences, e.g. SUM_MACRO((1)(2)(3)), actually work. Implementing Macros operating on sequences Sequences give you a convenient array-like data-structure wich can be passed to macros: #define HEAD(sequence) \ BOOST_PP_SEQ_ELEM(0, sequence) std::cout]]></description>
			<content:encoded><![CDATA[<p>After recently <a href="http://stackoverflow.com/questions/3150700/need-meta-programming-magic-to-define-a-mother-load-of-bit-fields-in-an-error-fre/3151243#3151243">answering</a> a question on Stackoverflow using <a href="http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/index.html">Boost.Preprocessor</a>, i wondered how the <a href="http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/data/sequences.html">sequences</a>, e.g. <code>SUM_MACRO((1)(2)(3))</code>, actually work.</p>
<p><span id="more-205"></span></p>
<h2>Implementing Macros operating on sequences</h2>
<p>Sequences give you a convenient array-like data-structure wich can be passed to macros:</p>
<pre lang="c++">
#define HEAD(sequence) \
    BOOST_PP_SEQ_ELEM(0, sequence)

std::cout << HEAD((0)(1)(2)); // prints "0"
</pre>
<p>Now how can that be implemented? First there are two characters that have special status for the preprocessor: commas and parentheses.<br />
Parentheses can be used to </p>
<ul>
<li>wrap expressions to be left alone by macro expansion, e.g. because they contain commas</li>
<li>can be bound to macro names as parameter lists</li>
</ul>
<p>The binding of parameter lists doesn't have to happen immediately, so the following works fine:</p>
<pre lang="c++">
#define IDENTITY(x) x
#define HEAD(sequence) IDENTITY sequence
std::cout << HEAD((42)); // prints "42"
</pre>
<p>The macro-expansion here is the following:</p>
<pre lang="c++">
HEAD((42))
IDENTITY (42)
42
</pre>
<p>That already works fine for a one-element sequence, but what if it had more elements:</p>
<pre lang="c++">
std::cout << HEAD((42)(23));
// results in:
std::cout << 42(23);
</pre>
<p>... which is obviously wrong. We need to get rid of the rest of the sequence by going through another macro:</p>
<pre lang="c++">
#define FIRST(x) x, DOESNT_EXIST
#define HEAD(sequence) FIRST sequence
std::cout << HEAD((42)(23));
</pre>
<p>This now results in:</p>
<pre lang="c++">
HEAD((42)(23))
FIRST (42)(23) // FIRST binds (42)
42, DOESNT_EXIST(23)
</pre>
<p>Passing this result through another macro that takes two arguments, we can ignore the second part:</p>
<pre lang="c++">
#define HEAD(sequence) IGNORE_SECOND(FIRST sequence)
#define FIRST(x) x, FOO
#define IGNORE_SECOND(x) IGNORE_SECOND_I(x) // needed for expansion of x
#define IGNORE_SECOND_I(x, _) x
</pre>
<p>Which now expands to:</p>
<pre lang="c++">
HEAD((42)(23))
IGNORE_SECOND(FIRST (42)(23)) // FIRST binds (42)
IGNORE_SECOND_I(42, DOESNT_EXIST(23))
42
</pre>
<h2>Getting elements by index</h2>
<p>Now the macros we pass the comma-separated expansion to don't have to throw away the second part:</p>
<pre lang="c++">
#define SECOND(sequence) IGNORE_SECOND(TAIL sequence)
#define TAIL(x) FIRST // throw away first element
</pre>
<p>... with which we get something like the following expansion:</p>
<pre lang="c++">
SECOND((1)(2)(3))
IGNORE_SECOND(TAIL (1)(2)(3))
IGNORE_SECOND_I(FIRST (2)(3))
IGNORE_SECOND_I(2, DOESNT_EXIST(3))
2
</pre>
<p>Now combining that with the concatenation operator <code>##</code> we can define macros that operate index-based:</p>
<pre lang="c++">
#define GET_BY_INDEX(index, sequence) IGNORE_SECOND( GET_##index sequence )
#define GET_0(x) x, DOESNT_EXIST
#define GET_1(_) GET_0
#define GET_2(_) GET_1
// ...
</pre>
<p>... which could lead to the following expansion:</p>
<pre lang="c++">
GET_BY_INDEX(2, (1)(2)(3)(4))
IGNORE_SECOND(GET_2 (1)(2)(3)(4))
IGNORE_SECOND_I(GET_1 (2)(3)(4))
IGNORE_SECOND_I(GET_0 (3)(4))
IGNORE_SECOND_I(3, DOESNT_EXIST(4))
3
</pre>
<h2>Counting elements</h2>
<p>To get the size of a sequence the elements in it need to be counted. The trick here is that substitution for macros that take arguments stops if there are no parameter-lists left that could be bound, so the following:</p>
<pre lang="c++">
#define SIZE(sequence) SIZE_0 sequence
#define SIZE_0(_) SIZE_1
#define SIZE_1(_) SIZE_2
#define SIZE_2(_) SIZE_3
// ...
</pre>
<p>would expand <code>SIZE((1)(2))</code> to <code>SIZE_2</code>. All that is left then is to map that name to a number:</p>
<pre lang="c++">
#define SIZE(sequence) \
    CONCAT(SIZE_, SIZE_0 sequence) // concatenate SIZE_ with SIZE_N
#define CONCAT(a, b) CONCAT_I(a, b) // allow for macro expansion
#define CONCAT_I(a, b) a ## b
#define SIZE_SIZE_0 0
#define SIZE_SIZE_1 1
#define SIZE_SIZE_2 2
// ...
</pre>
<p>... which would lead to expansions like:</p>
<pre lang="c++">
SIZE((1)(2))
CONCAT(SIZE_, SIZE_0 (1)(2))
CONCAT_I(SIZE_, SIZE_0 (1)(2))
CONCAT_I(SIZE_, SIZE_1 (2))
CONCAT_I(SIZE_, SIZE_2)
SIZE_ ## SIZE_2
SIZE_SIZE_2
2
</pre>
<p>- Georg</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/07/boost-preprocessors-sequences/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Objective-Glue</title>
		<link>http://colonelpanic.net/2010/03/objectiveglue/</link>
		<comments>http://colonelpanic.net/2010/03/objectiveglue/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 16:19:19 +0000</pubDate>
		<dc:creator>Georg Fritzsche</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[bridging]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=175</guid>
		<description><![CDATA[With template metaprogramming and the Objective-C runtime bridging between Objective-C and C++ can be simplified. This post describes a prototype for generating Objective-C delegates that bridge to C++ code.]]></description>
			<content:encoded><![CDATA[<p>A while ago, i inquired on <a href="http://stackoverflow.com/questions/2276840/easing-c-to-objective-c-cocoa-bridging-via-metaprogramming">StackOverflow</a> if anybody knew about meta-programming techniques to ease bridging between Objective-C and C++:</p>
<blockquote><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 (to e.g. mostly automatically marshall to/from calls using legacy types).</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 &#8211; yet often not more then a flat repetitive 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&#8230;
</p></blockquote>
<p>As i didn&#8217;t get any answers that were satisfactory for me, i started on a prototype.<br />
<span id="more-175"></span><br />
The following is an overview on the idea behind <a href="http://bitbucket.org/cygmatic/cygmatic/src/tip/og/">the prototype</a>.</p>
<h2>Motivation</h2>
<p>What annoyed me most is that every time i just need to get some events of an Objective-C instance to some C++ instance, i need to create a separate Objective-C class and manually forward from there to the target (C++) object.</p>
<p>Lets say we have the following informal delegate protocol:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2;</pre>
</div>
</div>
<p>Currently i have to first create an Objective-C instance:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> Glue <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    CppClass<span style="color: #002200;">*</span> instance;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCppInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CppClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ins;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2;
<span style="color: #a61390;">@end</span></pre>
</div>
</div>
<p>&#8230; with the appropriate implementation:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> Glue
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCppInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CppClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ins <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        instance <span style="color: #002200;">=</span> ins;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>concatString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s1 withString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>s2 <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">const</span> std<span style="color: #002200;">::</span><span style="color: #a61390;">string</span> s <span style="color: #002200;">=</span>
        instance<span style="color: #002200;">-</span>&gt;concatStrings<span style="color: #002200;">&#40;</span>toStdString<span style="color: #002200;">&#40;</span>s1<span style="color: #002200;">&#41;</span>, toStdString<span style="color: #002200;">&#40;</span>s2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> toNsString<span style="color: #002200;">&#40;</span>s<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre>
</div>
</div>
<p>Then i still have to take care of holding and initializing that Objective-C instance in the C++ instance, with possibly additional work for <a href="http://en.wikipedia.org/wiki/Opaque_pointer">opaque pointers</a> because i don&#8217;t want to bring Objective-C into the C++ header.</p>
<h2>Objective-Glue</h2>
<p>The idea was that there should be a way to at least cut down on the amount of code and to mostly automatically generate the bridging code.</p>
<p>I now found some time and came up with a basic prototype. Given the following class definition:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> CppClass <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> concatStrings<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> s1, <span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> s2<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> s1<span style="color: #000040;">+</span>s2<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>&#8230; a suitable delegate can be build as follows:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">CppClass cpp<span style="color: #008080;">;</span>
og<span style="color: #008080;">::</span><span style="color: #007788;">ObjcClass</span> objc<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;MyGlueClass&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
objc.<span style="color: #007788;">add_handler</span><span style="color: #000080;">&lt;</span>NSString<span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span>, NSString<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span>
       <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;concatString:withString:&quot;</span>, <span style="color: #000040;">&amp;</span>cpp, <span style="color: #000040;">&amp;</span>CppClass<span style="color: #008080;">::</span><span style="color: #007788;">concatStrings</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>Here the explicit template parameter to <code>add_handler()</code> specifies the Objective-C signature, the first parameter specifies the method name and the last two pass the C++ instance and the method to call on it.</p>
<p>Free functions can also be used:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">objc.<span style="color: #007788;">add_handler</span><span style="color: #000080;">&lt;</span>NSString<span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span>, NSString<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span>
       <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;concatString:withString:&quot;</span>, <span style="color: #000040;">&amp;</span>concatStrings<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p><a href="http://www.boost.org/doc/html/function.html">Boost.Function</a> objects can also be passed.</p>
<p>The Objective-C instance can then be called like the following:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">id</span> ins <span style="color: #002200;">=</span> objc.get_instance<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ins concatString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;abcd&quot;</span> withString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;efgh&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">assert</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>result compare<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;abcdefgh&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedSame<span style="color: #002200;">&#41;</span>;</pre>
</div>
</div>
<h2>How?</h2>
<p>While the Objective-C signature has to be specified explicitly even for formal protocols (as in the type encodings the type information for Objective-C classes is lost), the C++ signature can be looked up at compile time. Knowing those two signatures we can use template functions to get a suitable <code>IMP</code> function, that <i>&#8220;stores&#8221;</i> the knowledge of the signatures and does the necessary conversions.</p>
<p><i>Note: Behind the scenes, Objective-C methods are simple C functions that take two additional parameters, of type <code>id</code> for the instance and <code>SEL</code> for the method being called. These functions are called <code>IMP</code> or implementation functions.</i></p>
<p>Combining that with the fact that the <a href="http://developer.apple.com/mac/library/documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C runtime</a> allows to add and replace method implementations via its API, we can generate classes that call suitable <code>IMP</code> functions. To allow for member functions, free functions or <a href="http://www.boost.org/doc/libs/release/libs/bind">Boost.Bind</a> generated expressions to be invoked, functors (<a href="http://www.boost.org/doc/html/function.html">Boost.Function</a>) are used to store the callees.</p>
<p>The functors are stored in a map in the indexed ivar section of the instance, look-up is done via the selector that <code>IMP</code> functions are passed.<br />
The <code>IMP</code> function can then cast the retrieved pointer to the correct signature type and invoke the functor.</p>
<p><!–-nextpage-–></p>
<h2>Generating the instance</h2>
<p>Using the Objective-C runtime, we can generate a custom class. We first need the super-class (if any):</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> superClassName <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;NSObject&quot;</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> className      <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;MyObjcClass&quot;</span><span style="color: #008080;">;</span>
id superClass <span style="color: #000080;">=</span> objc_getClass<span style="color: #008000;">&#40;</span>superClassName.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>Then we need to allocate a new class pair and register it:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">Class myClass <span style="color: #000080;">=</span> objc_allocateClassPair
                    <span style="color: #008000;">&#40;</span>superClass, className.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
objc_registerClassPair<span style="color: #008000;">&#40;</span>myClass<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>The last parameter to <code>objc_allocateClassPair()</code> reserves additional space per instance for a pointer, so we can store the callee map later.</p>
<p>Now that the class is registered, we can create an instance:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">id myInstance <span style="color: #000080;">=</span> class_createInstance<span style="color: #008000;">&#40;</span>myClass, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>To add methods, we need to use <code>class_addMethod()</code> if the class doesn&#8217;t have such a method yet, or <code>method_setImplementation()</code> to override an existing one:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> methodName <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;concatString:withString:&quot;</span><span style="color: #008080;">;</span>
SEL mySelector <span style="color: #000080;">=</span> sel_registerName<span style="color: #008000;">&#40;</span>methodName.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
BOOL added <span style="color: #000080;">=</span> class_addMethod<span style="color: #008000;">&#40;</span>myClass, mySelector, myImp, <span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>added<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// class already has such a method, override it</span>
    Method m <span style="color: #000080;">=</span> class_getInstanceMethod<span style="color: #008000;">&#40;</span>myClass, mySelector<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    method_setImplementation<span style="color: #008000;">&#40;</span>m, myImp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</div>
</div>
<p>Now for expressions like <code>[myObj concatString:s1 withString:s2]</code>, the custom implementation <code>myImp</code> would get called. But how do we generate a suitable <code>IMP</code> function?</p>
<h2>Generating IMPs</h2>
<p>As we know the ObjcC and the C++ signatures, we can use template specialization to get a suitable function. To avoid too much repitition, partial specialization of a template class with a static member function is used:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">size_t</span> Arity, <span style="color: #0000ff;">bool</span> returnsVoid, <span style="color: #0000ff;">class</span> ObjcSig, <span style="color: #0000ff;">class</span> CppSig<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> imp<span style="color: #008080;">;</span></pre>
</div>
</div>
<p>For e.g. <code>CppClass::concatStrings</code>, we have an arity of 2 and it doesn&#8217;t return <code>void</code>. This would require the following specialization:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ObjcSig, <span style="color: #0000ff;">class</span> CppSig<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> imp<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">2</span>, <span style="color: #0000ff;">false</span>, ObjcSig, CppSig<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> <span style="color: #008080;">???</span> f<span style="color: #008000;">&#40;</span>id self, SEL sel, <span style="color: #008080;">???</span> a1, <span style="color: #008080;">???</span> a2<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">// ... invoke Callee</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p>Looking up the needed forms of the ObjC and C++ arguments and return types can be done via helper meta functions using Boosts FunctionTypes and TypeTraits libraries. With those types known, <code>f()</code> becomes something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> ObjcRetType f<span style="color: #008000;">&#40;</span>id self, SEL sel, ObjcArg0 a0, ObjcArg1 a1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    boost<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span>CppSig<span style="color: #000080;">&gt;</span> callee <span style="color: #000080;">=</span> lookup_callee<span style="color: #000080;">&lt;</span>CppSig<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>self, sel<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> convert<span style="color: #000080;">&lt;</span>PlainObjcRet, PlainCppRet<span style="color: #000080;">&gt;</span> <span style="color: #666666;">// convert&lt;ToType,FromType&gt;</span>
        <span style="color: #008000;">&#40;</span>callee
          <span style="color: #008000;">&#40;</span>convert<span style="color: #000080;">&lt;</span>PlainCppArg0, PlainObjcArg0<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a0<span style="color: #008000;">&#41;</span>,
           convert<span style="color: #000080;">&lt;</span>PlainCppArg1, PlainObjcArg1<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</div>
</div>
<p><i>Note: The <code>Plain*</code> types denote argument or return types stripped of references and constness.</i></p>
<p>With such an approach, we can choose a suitable <code>IMP</code> function at compile-time:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> imp<span style="color: #000080;">&lt;</span>function_arity, is_same<span style="color: #000080;">&lt;</span>ReturnType, <span style="color: #0000ff;">void</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">value</span>,
            ObjcSig, CppSig<span style="color: #000080;">&gt;</span> ImpType<span style="color: #008080;">;</span>
IMP myImp <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>IMP<span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>ImpType<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h2>Conversions</h2>
<p>Conversions are done via a static member function of a templated struct <code>og::converter</code>, which allows for partial specialization in user code. It is declared as following:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> To, <span style="color: #0000ff;">class</span> From<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">struct</span> converter <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> To convert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">typename</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">call_traits</span><span style="color: #008080;">::</span><span style="color: #007788;">param_type</span><span style="color: #000080;">&lt;</span>From<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">type</span> from<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<p><em>Note: <a href="http://www.boost.org/doc/libs/1_42_0/libs/utility/call_traits.htm"><code>param_type</code></a> is a Boost utility that maps the type to the appropriate form (pass-by-value for fundamental types, pass-by-const-reference for complex types, &#8230;).</em></p>
<p>To add additional conversions, specializations of <code>converter</code> are added anywhere before the <code>add_handler()</code> call that would use them. E.g. to enable conversions from <code>NSString</code> to <code>std::string</code> the following (simplified) specialization can be added:</p>
<div class="wp_syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;&gt;</span> <span style="color: #0000ff;">struct</span> converter<span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, NSString<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> convert<span style="color: #008000;">&#40;</span>NSString<span style="color: #000040;">*</span> from<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#91;</span>from UTF8String<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre>
</div>
</div>
<h2>Conclusion</h2>
<p>While the prototype shows that the basic idea works, there are still some issues that need to be worked out:</p>
<ul>
<li>support for formal protocols</li>
<li>support for properties</li>
<li>support for type-encodings (in a non-painful way)</li>
<li>allowing full customization of class and instance (currently dealloc is needed and no class methods supported)</li>
<li>convenience for wrapping complete C++ instance as Objective-C instances?</li>
<li>&#8230; probably more</li>
</ul>
<p>For those interested, here is some background reading material:</p>
<ul>
<li><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html">Objective-C Runtime Programming Guide</a></li>
<li><a href="http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html">Understanding the Objective-C runtime</a></li>
<li><a href="http://developer.apple.com/mac/library/documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C Runtime Reference</a></li>
</ul>
<p>As previously mentioned, this is a first prototype&#8230; so constructive input and ideas are welcome. The current source can be <a href="http://bitbucket.org/cygmatic/cygmatic/src/tip/og/">viewed online</a> via a bitbucket repository.<br />
The prototype relies on some parts of <a href="http://www.boost.org/">Boost</a> and was tested with Boost 1.42.</p>
<p><i>&#8211; Georg</i></p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2010/03/objectiveglue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

