libmx3 / mx3

a sample project showcasing/collecting cross platform techniques on mobile
MIT License
1.17k stars 146 forks source link

Quick question about talking with native code #66

Open wesselj1 opened 9 years ago

wesselj1 commented 9 years ago

So the more I've become familiar with the example the only way I've seen that the C++ code talks back to the native code is with callbacks. Is there another way to talk from the C++ side of the code to the native side? Or do you just have to use the callback pattern that is being used to go from C++ to native?

skabbes commented 9 years ago

First of all, what are you trying to accomplish? Maybe I can help.

Essentially for the same c++ code to interact with Java and ObjC code with no changes, you'll need some sort of polymorphism. Now, I suppose you don't necessarily need runtime polymorphism, you could accomplish it at compile time, but djinni doesn't support this (its kind of a weird thing to support anyway).

So, given that we need polymorphism - we have to interact with the host-language code over pure virtual interfaces that get implemented in either Java or Objc. So, you'll need a kind of dependency injection pattern (what you're calling callbacks) to make that work.

wesselj1 commented 9 years ago

Hey @skabbes,

Sorry I never got back to reply to this. I moved on to another part of the project that I am working on so I do not know that I will be getting back to exploring this.

Essentially though, I was wanting to setup something similar to Square's Otto. So we would need to be able to call from the C++ code to other parts (possibly multiple) of the native code. I think this would be a useful thing for mx3, but I don't know if anyone else needs this or if there is another method to this issue that I am missing, such as some polymorphic approach as you had mentioned. Honestly, I do not see how to use polymorphism to tie the c++ code to the Java or ObjC. That might just be my own misunderstanding of something.

skabbes commented 9 years ago

Well, otto seems to only be Java-based. So, it would kind of only be single platform (therefore polymorphism wouldn't really get you anything).

However, if you wanted an "event bus" like thing. You'd need to define that interface, and then implement it on android (with otto) and on iOS with something else.

EventBus = interface +o +j {
   publish(message: string);
}

Then in your code, you'd need to do one of the following:

#ifdef __android__
EventBus * e = new JavaEventBus();
#endif
#ifdef __APPLE__
EventBus * e = new ObjcEventBus();
#endif

Or, you could simply pass the EventBus * as a parameter to your application (which is what djinni forces you to do). Does that make more sense?

wesselj1 commented 9 years ago

Yes, Otto is for Java only. What you've explained makes sense, but I would actually be wanting to do the publishing of events and routing to the appropriate subscribers within the C++ code. I was basically looking to not use Otto but make some a cross-platform solution to use in place of Otto since it is Java only. However, due to some other dependencies the project has gone to strictly native code for iOS and Android.