<?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; objective-c</title>
	<atom:link href="http://colonelpanic.net/tag/objective-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>iOS and iCloud: overcoming &#8220;bad file descriptor&#8221; errors</title>
		<link>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/</link>
		<comments>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 06:49:48 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General Development]]></category>
		<category><![CDATA[icloud]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://colonelpanic.net/?p=638</guid>
		<description><![CDATA[Pretty quick one today. I beat my head against this for several hours before I found the obvious solution, so I thought I&#8217;d jot the info down here for reference, and maybe to help someone else out. NOTE: There is now a relevant answer on StackOverflow. Unfortunately, this answer wasn&#8217;t there when I needed it. [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty quick one today. I beat my head against this for several hours before I found the obvious solution, so I thought I&#8217;d jot the info down here for reference, and maybe to help someone else out.</p>
<p><strong>NOTE:</strong> There is now <a href="http://stackoverflow.com/questions/8714525/cannot-sync-simple-text-file-with-icloud-bad-file-descriptor">a relevant answer</a> on StackOverflow. Unfortunately, this answer wasn&#8217;t there when I needed it.</p>
<h3>The problem</h3>
<p>Recently, while adding <a href="http://www.apple.com/icloud/">iCloud</a> support to the <a title="Day One - your premier journaling solution." href="http://dayoneapp.com/">Day One</a> iOS app, I ran into an issue copying files to and from the application&#8217;s Documents directory and its corresponding iCloud directory. Day One can run in one of three modes:</p>
<ol>
<li>Sync entries via iCloud</li>
<li>Sync entries via <a href="https://www.dropbox.com/">Dropbox</a></li>
<li>Don&#8217;t sync entries</li>
</ol>
<p>If you&#8217;ve developed for iCloud at all, you know that iCloud documents live within specific, &#8220;ubiquitous&#8221; directories on the user&#8217;s device (discovered via NSFileManager&#8217;s URLForUbiquityContainerIdentifier: method). So in order to enable iCloud sync, we needed to copy all files into the appropriate ubiquitous container. Conversely, if the user elects to disable iCloud then we need to copy all files from the ubiquitous container into the application&#8217;s Documents directory.</p>
<h3>The assumption</h3>
<p>We wrote a simple method to copy files from one directory to another, either going into or coming out of iCloud. It uses a basic NSDirectoryEnumerator to find all the files in the current source directory, check if they are present in the destination directory, and copy them there if they aren&#8217;t. Here&#8217;s a simplified example:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>sourceDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self currentWorkingDir<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// current directory, ie ubiquitous iCloud directory</span>
<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>destDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self destDirForMovingFromDir<span style="color: #002200;">:</span>sourceDir<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// where should we copy to?</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fm <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDirectoryEnumerator</span> <span style="color: #002200;">*</span>dirEnum <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fm enumeratorAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>sourceDir path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>mergeError;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>sourceFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dirEnum nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>sourceFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sourceDir URLByAppendingPathComponent<span style="color: #002200;">:</span>sourceFile<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>destFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>destDir URLByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>sourceFile lastPathComponent<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm fileExistsAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>destFileURL path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm copyItemAtURL<span style="color: #002200;">:</span>sourceFileURL toURL<span style="color: #002200;">:</span>destFileURL error<span style="color: #002200;">:&amp;</span>mergeError<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR (copy error): %@ -&gt; %@ - %@&quot;</span>, sourceFileURL, destFileURL, mergeError<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre>
</div>
</div>
<p>I frequently found &#8220;bad file descriptor&#8221; errors being logged out. We were assuming that the NSDirectoryEnumerator would only list files that were actually present on the device. As it turns out, that is not the case when enumerating a ubiquitous directory.</p>
<h3>The solution</h3>
<p>Once you realize that the NSDirectoryEnumerator will list all ubiquitous files that exist on any device, regardless of whether or not they have been downloaded to the current device, the solution becomes pretty obvious:</p>
<p><em>You can&#8217;t copy a file that hasn&#8217;t been downloaded.</em></p>
<p>Adding that check is fairly straight forward. Since our method doesn&#8217;t know if we are going into or out of iCloud, we implemented a pretty general solution. Modifying the above example, it looks something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="objc" style="font-family:monospace;">...
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>sourceFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dirEnum nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    ...
    <span style="color: #11740a; font-style: italic;">// By default, assume all files are able to be copied.</span>
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>ableToBeCopied <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// However, ubiquitous items that are not downloaded CANNOT be copied</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fm isUbiquitousItemAtURL<span style="color: #002200;">:</span>sourceFileURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>sourceFileURL getResourceValue<span style="color: #002200;">:&amp;</span>ableToBeCopied forKey<span style="color: #002200;">:</span>NSURLUbiquitousItemIsDownloadedKey error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Now copy the file if it doesn't exist and is able to be copied</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm fileExistsAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>destFileURL path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#91;</span>ableToBeCopied boolValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fm copyItemAtURL<span style="color: #002200;">:</span>sourceFileURL toURL<span style="color: #002200;">:</span>destFileURL error<span style="color: #002200;">:&amp;</span>mergeError<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR (copy error): %@ -&gt; %@ - %@&quot;</span>, sourceFileURL, destFileURL, mergeError<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre>
</div>
</div>
<h3>Conclusion</h3>
<p>See what I mean? Pretty obvious, and I can&#8217;t believe I lost so much time tracking this down. If the source file is ubiquitous, it might not be downloaded, and thus you might not be able to copy it. So check it first.</p>
]]></content:encoded>
			<wfw:commentRss>http://colonelpanic.net/2012/01/ios-and-icloud-overcoming-bad-file-descriptor-errors/feed/</wfw:commentRss>
		<slash:comments>0</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>

