title: Is there a way to wrap protocol buffers into Objective-C and still take advantage of inheritance?
body: <p>I want to use Protocol Buffers in an iOS project. I'm trying to avoid making the whole project into an Objective-C++ fiasco, so I want to wrap the C++ protobuf classes into Objective-C ones. I have several dozen protobuf messages, and while I have done this successfully one class at a time, ideally I would like to use inheritance to minimize the repeated code. I'm new to Objective-C and I haven't used what little I knew of C++ in 10 years, so this has mostly been an exercise in frustration. Below is an example of how I have wrapped a single message.  </p>
  
  <p><strong>Code</strong> </p>
  
  <p>.proto:  </p>
  
  <pre><code>message MessageA {
      optional string value = 1;
  }
  </code></pre>
  
  <p>MessageAWrapper.h:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface MessageAWrapper : NSObject
  
  @property (nonatomic) NSString *value;
  
  + (id)fromString:(NSString *)string;
  - (NSString *)serialize;
  
  @end
  </code></pre>
  
  <p>MessageAWrapper.mm:</p>
  
  <pre><code>#import "MessageA.h"
  #import "message.pb.h"
  
  @interface MessageAWrapper ()
  
  @property (nonatomic) MessageA *message;
  
  @end
  
  @implementation MessageAWrapper
  
  - (id)init
  {
      self = [super init];
      if (self) {
          self.message = new MessageA();
      }
      return self;
  }
  
  - (void)dealloc {
      delete self.message;
      self.message = NULL;
  }
  
  - (NSString *)value {
      return [NSString stringWithUTF8String:self.message-&gt;value().c_str()];
  }
  
  - (void)setValue:(NSString *)value {
     self.message-&gt;set_value([value UTF8String]);
  }
  
  - (NSString *)serialize {
      std::string output;
      self.message-&gt;SerializeToString(&amp;output);
      return [NSString stringWithUTF8String:output.c_str()];
  }
  
  + (id)fromString:(NSString *)string {
      MessageA *message = new MessageA();
      message-&gt;ParseFromString([string UTF8String]);
  
      MessageAWrapper *wrapper = [[MessageAWrapper alloc] init];
      wrapper.message = message;
      return wrapper;
  }
  
  @end
  </code></pre>
  
  <p><strong>Goal</strong>  </p>
  
  <p>There is a lot of code here that will be repeated dozens of times in which the only variation is the wrapped class type (<code>init</code>, <code>dealloc</code>, <code>serialize</code>, <code>fromString</code>), so ideally I would like to put it on a parent <code>ProtobufMesssage</code> class instead. Unfortunately I've had no success in making this work because I can't find a way for the parent class to know the class its children are using, which is required for example in <code>init</code> and <code>fromString</code>.</p>
  
  <p><strong>Things I've attempted</strong>  </p>
  
  <ul>
  <li>struct</li>
  <li>template class</li>
  <li>void*</li>
  </ul>
  
  <p><strong>Obstacles I've encountered</strong></p>
  
  <ul>
  <li>can't find a way to store a reference to a class/type</li>
  <li>can't have any C++ headers or code in the .h file (as this requires the whole project to be Objective-C++)</li>
  <li>difficulty keeping references to the protobuf message parents (<code>Message</code> or <code>MessageLite</code>) because they are abstract</li>
  </ul>
  
  <p>As I said I have very little understanding of C++ or Objective-C; most of my experience is with much higher level languages like Python and Java (though I do mostly understand basic C things like pointers).</p>
  
  <p>Is this perhaps not even possible? Am I approaching it wrong or missing something obvious? Any help would be much appreciated. Thanks.</p>