0

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++.

user1749013
  • 83
  • 2
  • 5
  • 1
    Why are you writing the wrapper in the first place? Is it just to limit the scope of the interop? – jscs Jan 16 '19 at 00:35
  • Off-topic: I'd prefer an enum (class) over the integer constants, that's safer. – Aconcagua Jan 16 '19 at 05:46
  • 1
    You can just use `objective-c++` everywhere without any wrappers. – Cy-4AH Jan 16 '19 at 08:27
  • @Josh Caswell: I am writing the wrappers to allow use of the C++ classes in Objective-C. I am not trying to limit anything. – user1749013 Jan 16 '19 at 20:18
  • @Aconcagua: I may do that. I did not write the classes, I am just doing translation, but have freedom to make alterations. – user1749013 Jan 16 '19 at 20:19
  • @Cy-4AH: I think I am heading down that road now.. – user1749013 Jan 16 '19 at 20:19
  • Hmm. You can already use the C++ classes in ObjC...that's why you're able to write wrappers for them. – jscs Jan 16 '19 at 20:20
  • You can check my two answers how to write `objective-c++` not everywhere: https://stackoverflow.com/a/26842441/1934750 and https://stackoverflow.com/a/22679104/1934750 – Cy-4AH Jan 17 '19 at 08:35
  • After doing some careful code review and contrast against an implementation for Android using JNI, it looks like I really only have to write a wrapper for one class-the rest of the classes are used by this one class internally. Thanks all for your help! – user1749013 Jan 24 '19 at 21:29

0 Answers0