I have a factory class in C++ that I need to make use of in an Objective-C project, so I am trying to write a wrapper for it.
Below is the superclass definition of the Widget class.
class Widget
{
public:
Widget();
virtual ~Widget();
virtual Thingamajig getThingamajig() = 0;
};
The factory will return a subclass of Widget, and the Objective-C program will use the Thingamajig provided by Widget (I omitted details on Thingamajig-just presume it is a very simple data container class).
Here's the factory class:
class WidgetFactory
{
public:
/*...a series of integer constants defining the different types of widgets to be returned by
createWidget function below.*/
WidgetFactory();
~WidgetFactory();
Widget* createWidget( int type );
};
Because I am writing a wrapper, the createWidget method in the factory class is non-static (createWidget was originally static).
Now, here is what I have for the Objective-C wrapper thus far. It is the header for an Objective-C++ (mm file):
@interface WidgetFactoryWrapper: NSObject
@end
@interface WidgetFactoryWrapper()
-(Widget*) createWidget: (int)type;
@end
Here are my questions:
1) I got the idea of writing the wrapper like this from another SO article here (Calling C++ from Objective-C). However, I do not think the last step of writing something like CplusplusMMClass, another Objective-C++ class that uses the Objective-C++ wrapper, is necessary here-I should just call the Objective-C++ wrapper here directly. Is my thinking incorrect?
2) While I accept that I will have to write a wrapper around Widget, will I have to do so for all its subclasses?
3) If you were in my proverbial shoes, how would you go about doing this?
I'm tempted to just translate everything into Objective-C, but I want to be able to reuse what's already been written in C++.