Discussion:
C++ underpinnings with Cocoa?
(too old to reply)
Jared McIntyre
2000-09-22 07:03:28 UTC
Permalink
I read a year or two ago that it was possible to use C++ underneath
you GUI in NeXT Step and that this could be done to help port an app.
I have a an application core written in C++ that I could very easily
get to compile anywhere and I was hoping to be able to use Cocoa to
quickly write a UI on top of it. Is this possible anymore and could
someone point me to documentation that might help me port in this
manner. Also, if it is possible, how clean is the conversation
between Obj-C and C++, in other words can I get either to call each
other without having to write some sort of insane wrapper
functionality?

Sorry if this has been covered but the serch site for the list parses
out "C++" so its made information hard to find.

Thank You,
--
Jared McIntyre
Deep Fried Software
Owner/Engineer
http://www.dfsoftware.com
***@dfsoftware.edu
Frederic Stark
2000-09-22 08:13:52 UTC
Permalink
Post by Jared McIntyre
I read a year or two ago that it was possible to use C++ underneath
you GUI in NeXT Step and that this could be done to help port an app.
I have a an application core written in C++ that I could very easily
get to compile anywhere and I was hoping to be able to use Cocoa to
quickly write a UI on top of it. Is this possible anymore and could
someone point me to documentation that might help me port in this
manner. Also, if it is possible, how clean is the conversation
between Obj-C and C++, in other words can I get either to call each
other without having to write some sort of insane wrapper
functionality?
Too bad. You are looking of ObjC++, but Apple dropped it lately (I
don't think it is back in beta), so you are tied to 'insane wrapper
functionality'...

Cheers,

--fred
Jared McIntyre
2000-09-22 10:39:19 UTC
Permalink
Post by Frederic Stark
Too bad. You are looking of ObjC++, but Apple dropped it lately (I
don't think it is back in beta), so you are tied to 'insane wrapper
functionality'...
Cheers,
--fred
8-) Would anyone know where there might be a decent reference for
doing that kind of work (wrapping C++ for Obj-C that is)? We were
really hoping to bring this app to mac without having to spend a lot
of time on UI (its a plugin based server monitoring tool).

Thanks,
--
Jared McIntyre
Deep Fried Software
Owner/Engineer
http://www.dfsoftware.com
***@dfsoftware.edu
Dennis C. De Mars
2000-09-22 12:00:31 UTC
Permalink
Post by Jared McIntyre
Post by Frederic Stark
Too bad. You are looking of ObjC++, but Apple dropped it lately (I
don't think it is back in beta), so you are tied to 'insane wrapper
functionality'...
Cheers,
--fred
8-) Would anyone know where there might be a decent reference for
doing that kind of work (wrapping C++ for Obj-C that is)? We were
really hoping to bring this app to mac without having to spend a lot
of time on UI (its a plugin based server monitoring tool).
Thanks,
It's too bad that they dropped the hybrid Obj-C/C++ capability, as this kind
of problem is going to pop up a lot. There are going to be people who have
lots of C++ code they want to port, people trying to maintain cross-platform
compatibility etc., and it would be ideal if you could just make C++ calls
directly from your Obj-C code.

Anyhow, it usually boils down in these cases to the lowest common
denominator: C. You'll just have to write some glue to create C functions
that call the functions and methods you need to export, and use opaque data
types for the object pointer. It's tedious but straightforward, and for
purposes of creating the GUI, you probably only have to export a subset of
your API.

I don't know of any references for doing this, but it is pretty simple (some
of the C++ books might actually have some simple examples to illustrate the
use of the extern "C" {} construct.

There _is_ some documentation on the Apple site for their wrappers for using
Obj-C from Java and vice versa, the "Java bridge." What they did is much
more elaborate because they mere trying to make the entire Cocoa Obj-C
library accessible from Java, which means providing some way of making it
look like the Obj-C objects are Java objects, allowing Obj-C classes to be
extended in Java, etc. Now we are talking truly "insane wrapper
functionality." It's probably not possible to do that with C++, nor would it
be practical even it were possible.

- Dennis D.
Jared McIntyre
2000-10-01 20:13:32 UTC
Permalink
Post by Dennis C. De Mars
It's too bad that they dropped the hybrid Obj-C/C++ capability, as this kind
of problem is going to pop up a lot. There are going to be people who have
lots of C++ code they want to port, people trying to maintain cross-platform
compatibility etc., and it would be ideal if you could just make C++ calls
directly from your Obj-C code.
Anyhow, it usually boils down in these cases to the lowest common
denominator: C. You'll just have to write some glue to create C functions
that call the functions and methods you need to export, and use opaque data
types for the object pointer. It's tedious but straightforward, and for
purposes of creating the GUI, you probably only have to export a subset of
your API.
I don't know of any references for doing this, but it is pretty simple (some
of the C++ books might actually have some simple examples to illustrate the
use of the extern "C" {} construct.
I can see how externs will work pretty easily for an application as I
described earlier, but mine is a bit more complex than a simple C++
control class with a Objective-C UI on top. The first big problem is
that it is largely a plugin based system and these plugins define
window panes that will be placed in the apps main window. Imagine a
simple design (mono-space font will be helpful)

Objective-C App Interface
|
| Single function communication path back and forth
|
C++ Controlling class
C++ Data storage objects (shared between all levels)
C++ Platform Abstraction Layer
|
| Single function communication path back and forth
| to each plugin that is loaded
|
Objective-C (may be partially C++) Plugin
- Includes potentially many Objective-C UI elements that
must be placed in App interface.
- Does a lot of other stuff 8-)

So the C++ based data structure that defines the plugin for the app
must contain some reference of the objective-c window pane object so
that the app interface can communicate with it. Will this be a
problem? Having not worked in Objective-C much I am having a hard
time wrapping my brain around this issue because I am not completely
sure what kind of pointer abstraction, etc might be support by this
environment to get this to work.

Thanks for the help. Also, if anyone is willing to put up a few
simple examples of C++ used in an Objective-C environment it would be
much appreciated for moth myself and I am sure the many other
developers that will be interested in porting apps and trying to
maintain a codebase between the different environments.
--
Jared McIntyre
Deep Fried Software
Owner/Engineer
http://www.dfsoftware.com
***@dfsoftware.edu
Jonathan Wight
2000-09-22 12:14:44 UTC
Permalink
Post by Dennis C. De Mars
Anyhow, it usually boils down in these cases to the lowest common
denominator: C. You'll just have to write some glue to create C functions
that call the functions and methods you need to export, and use opaque data
types for the object pointer. It's tedious but straightforward, and for
purposes of creating the GUI, you probably only have to export a subset of
your API.
Has anyone considered using SWIG to automatically produce the wrapper code?
SWIG works great for producing wrappers from C/C++ to Python (and Perl and
others). With some modifications it could be used to produce the interface
between ObjC and C/C++.

http://www.swig.org/

Be interested if anyone has done this.

Jon.
Byron Han
2000-09-22 18:14:14 UTC
Permalink
Aloha
There are presently no automated tools to generated a C/ObjC
compatible wrapper to C++ classes. I had to manually generate a
wrapper so that my Cocoa app and my command line tools could access
my C++ framework. It was not difficult, but was tedious and I have
to keep remember to update my wrapper to keep it in sync with changes
to my C++ framework. Fortunately, I only have two classes to worry
about with maybe a total of 3 dozen methods to deal with.

I wrote the framework originally in C++ because I was bringing it up
under MacOS though now I wish that I had just bitten the bullet and
written the whole thing in ObjC from the beginning... of course that
does not help those of you with legacy (!) C++ code to use.
Greg Titus
2000-10-01 20:48:07 UTC
Permalink
Post by Jared McIntyre
So the C++ based data structure that defines the plugin for the app
must contain some reference of the objective-c window pane object so
that the app interface can communicate with it. Will this be a
problem? Having not worked in Objective-C much I am having a hard
time wrapping my brain around this issue because I am not completely
sure what kind of pointer abstraction, etc might be support by this
environment to get this to work.
Shouldn't be a problem. Just treat your Objective-C objects as (void *)
pointers in the C and C++ code, and then when you get back into the top
level Objective-C app interface, cast those void *'s back to the
Objective-C objects you expect.

The one little complication is that your middle data storage layer can't
free up those objects without help. Hopefully your plugin architecture is
designed in such a way that you call back into the plugin to free up
storage (and at that point it can go back into Objective-C to release the
objects).

--Greg

Continue reading on narkive:
Loading...