Real time processing data using algorithms written in c++ - c++

Here is the scenario:
My swift app collects data from bluetooth and should process them in real-time. These data comes mainly from IMU(gyroscope, magnetometer and accelerometer).
Algorithms that process all of that is going to be written in c++ and utilize some libraries like Eigen. How should I approach such problem? From what I've found:
1) put c++ files into my project, write wrapper in objective-c and bridge it to Swift. Also now sure if I can include Eigen in mobile app easily. This would be tedious process I suppose
2) Get all algorithms as library( .dll, .lib and call it directly from swift, not sure if it's possible)
3) Rewrite all algorithms to Swift, utilize Eigen substitute for Swift, not sure if anything like this exist. Also, this solution is less efficient and would probably fail because of deadlines.
How should I approach such problem? How to solve it in a most efficient way, where I can make use of already exisiting c++ code?
I believe some of you would see this question as opinion based, but I do not know how to state this problem in a way that excludes any ambiguity.
How to run c++ files that uses Eigen in iOS app written is Swift?
Thanks in advance

You cannot call c++ from Swift. However, you can call c++ from objective-c++. And you can call objective-c++ from both Swift and objective-c.
Just make sure that your public-facing #interface code (in the .h file) contains only objective-c. (no classes, no templates, no namespaces, no including c++ headers).
Your #implementation goes in a file with extension .mm which will compile as objective-c++ - giving you an objective-c interface with the full use of c++ in the implementation of the Objective-C object.
c++ objects live quite happily as the member variables of an objective-c++ implementation. However, they will be default-constructed then the objc runtime calls Init. If you are using objects which don't have default constructors, you will need to either wrap them in a boost::optional or a std::unique_ptr (etc).
You can them import the objective-c objects into your Swift program.
Full example for anyone who has not done this before:
https://github.com/madmongo1/swift-to-cpp-demo.git

Related

Developing for iOS in C++ (with/without Xcode)

What are the methods of developing for iOS with C++? I am familiar with using Objective-C++ within Xcode, by selecting Obj-C as my language when creating a new project, but what else is there. I know there are tools like Xamarin that allow for iOS development in C#, however i'm curious as to what officially supported ways of developing for iOS in C++ there are? Can an iOS app be written completely in C++ with no Objective-C and compile?
I have an app that pretty freely intermingles Objective-C (.mm files), Objective-C++ (.m files), and C++ (.cpp) as needed. The C++ code is legacy functionality that implements the heavy lifting of our app and has worked across a number of platforms for 20+ years. No need to re-implement that.
To write a C++ object in XCode, just do it. Create .cpp and .h files like you would anywhere else.
When you need to interact with a C++ object from Objective-C, you can either go into the properties of the .m file in XCode and change it to use the Objective-C++ compiler, or (better yet) just name it .mm instead of .m. Then you can include your C++ header files with standard #include statements, create C++ objects with new, delete them with delete, and access members with dot notation and pointer notation just like you would from C++.
Objective-C is a lot easier to use when dealing with the UI and system calls. It isn't hard to learn. There are probably ways to access all of those objects and methods from C++, but the gymnastics you would have to do each time might make it easier to simply learn enough Objective-C (or Swift) to do those parts in the language Apple intended. (Keep in mind that to figure out how to do everything in C++ while looking at sample code and documentation that assumes Objective-C or Swift, you're going to have to master those languages anyway.)
Yes, you can even go to extreme lengths to avoiding Objective-C, you won't be able to do so completely since UIKit depends on it, but nearly every other framework has a C API. Take a look at this question and the answer to it How to write iOS app purely in C
C is not C++, but the answer will give you a hint on what you can do. It will involve including the objc runtime, and using objc_msgsend.

Swift vs Objective-C - for reusing C++ code

We have an android application having the critical code written in C++ in JNI library.
We are about to implement iOS version of the same application. The plan is to implement in Objective-c as it's straight forward to integrate C code with it. However, as Swift is picking up, we would like to have suggestion on Swift vs Objective-C keeping in mind that we have to use the existing C++ libraries and any Swift bottlenecks.
The way to reuse C++ code is to write an Objective-C wrapper class, with the header file containing nothing that is C++, but the implementation file written in Objective-C++ (.mm suffix).
An Objective-C class can be used from Swift and from Objective-C, so for a Swift project, it doesn't matter if you have a few Objective-C classes.
There is no way currently to call C++ from Swift directly, and I wouldn't expect it for a while. And if you look at how you call C from Swift, you'll probably decide that you are quite happy with the situation.
Thanks for responding. After careful consideration, we decided to go with Objective-C and even released the application.
One of the key reason was that most developers in our company are proficient in C/C++ and Objective-c was easy to get comfortable with than the Swift.
Integration with existing C/C++ code was easy without any bridging code once we defined the Makefile templates and used them within existing Makefiles. Only additional thing was to make scripts for fat libraries.
Hence based on our experience, I will recommend Objective-C over Swift.
There is no problem writing a Swift application and integrate in it a c++ source code.
Here are the steps:
1) Add the c++ source files to your Xcode project. If you are using a 3rd party libreries you will need to add it as framework to your project.
2) create a Header bridge file and add it to you project. This file will have the c++ functions signature you will like to use from the swift modules. as explained in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
3) When you call the c++ functions mention in the Header Bridge file you need to create a special pointers object, as described in -
https://gist.github.com/neilpa/b430d148d1c5f4ae5ddd
Hope this will help.

C++ algorithm in iOS application

I've got a DSP audio aglo in C# that I'd like to incorporate into iOS. A friend suggested that I convert to C++ instead of Objective-C, as it would be platform agnostic. Is this a good practice, any pitfalls to be aware of? My instinct is to keep it native with Obj-C. How would I access C++ functions within iOS?
Thank you
You can freely mix C++ with Objective-C, it's known as "Objective-C++". To let the compiler know that is your intent, you need to rename you .m file as .mm.
Mixing code like that is not necessarily a good idea, and fairly pointless if you aim is portability. Better is to keep you C++ in separate .cpp files, and create a bare-bones wrapper class (obj-c++) to pass data between your objective-C and C++ objects (effectively the wrapper is mediating obj-C methods and C++ functions to send and return data).
I have recently posted a couple of simple image processing examples of this on github - openCVSquares and openCVStitch. It's worth you taking a look, just to see how the C++ code is separated out from obj-C.
The openCV libraries have a C++ interface, so using C++ in the project is unavoidable. It would be possible to mash all of the code into .mm files, but that's an easy way to make a mess.
As regards portability, you get this by separating your UI code from your data-manipulation code. You should be doing this anyway for MVC, so it will be little additional work to write your model in C++ as per these examples. The model is portable, the UI is - and should be - platform specific.
Whether you should do this will depend on other factors such as your fluency with each language, the choice of frameworks and libraries, and the likelihood of porting. Audio processing would be a natural candidate for such an approach though.
Some detail about the samples
Don't worry if you can't get the code to run (the openCV framework can be troublesome...) the main thing is to see how the code is pieced together. I've tried to keep the examples very simple.
The C++ code was ported from C++ samples included with the openCV distro: squares.cpp and stitching.cpp. I tried to alter these as little as possible in order to assess this idea of portability.
Roughly, I had to address two issues...
1/ removed any UI code (so we are just sending data back to the obj-C side).
2/ modify the main() function so it becomes a callable function from the obj-C side. In the .cpp header file I declare a simple class with a single static function to facilitate this. Obviously there is scope to get much more elaborate but this should be enough for you to get the idea.

Cocoa and Objective-C++

I think I understand how Objective-C++ works in relation to Cocoa, and I am fairly versed in the basics of command-line C++ but sort of afraid to try mixing it with Objective-C. I can create a GUI with Interface Builder in Xcode, and even make classes (.h and .m files) for the interface automatically. The outlets, maybe actions (I still get those confused) leave the curly brackets open to put your own code in. So could I just put C++ code in those brackets to to take input from those buttons (and see the output, etc.)?
I guess if this is the case, one wouldn't have to learn any Obj-C, as long as they knew where to put the C++ code! Am I wrong?
Also, could anyone help me figure that out or at least point me somewhere I can learn more about this? This topic is fairly scarce on the web and I don't understand Apple's documentation.
My suggestion would be to have Objective-C files in your project (the .m file), then keep your Objective-C++ in separate files (as diciu mentions, .mm files).
One strategy would be use to these Objective-C++ classes simply as wrappers over C++ classes: write your code in C++, in .cpp files like normal. Then, when you need to access some functionality from your C++ from an Objective-C file, write a wrapper Objective-C++ class and call that from your Objective-C.
This last bit feels like a lot of work for small projects, and it is, but if you have a lot of C++ that you need to use, and a comparatively little bit of Objective-C to write, this works pretty well. (Imagine a situation where you are writing a Cocoa interface for a well-factored Windows or Linux program: where all the application logic was written in a platform agnostic manner, and you "just" have to write the UI in Cocoa to get it running on the Mac.
But, another reason for doing this wrapper approach is that Objective-C++ is a hybrid language, so has limitations as to what it's capable of, in both directions.
The thing you need to understand about objective-c++ is that the name is a lie. or at least implies a lot more than you might think.
Objective-c++ does not mean you can write hybrid objective-c++ "objects". It means that you can use c++, and objective c, in the same file. they can reference each other directly - the cannot inherit or derive from each other.
Rename .m files to .mm and you will be able to write (Objective-)C++ code in them.

What Are Some Quirks/Surprises with Using .mm Files in Objective-C?

I want to use some C++ STL collections in my Objective-C iPhone app. Apparently this is possible by giving files the extension ".mm" . What are some of the quirks/surprises associated with this?
I want to use the basic containers that I'm familiar with (vector, queue, set, ...)
Cheers!
See Using C++ With Objective-C for a detailed list of what you can and can't do. You can do most things that you would expect. You just can't do things like have a C++ class inherit from an Objective-C class or vice-versa, you can't mix C++ exceptions with Objective-C exceptions, and C++ introduces several new keywords not present in Objective-C.
The major quirk of Objective-C++ is that if you don't pass -fobjc-call-cxx-cdtors to g++, it won't call the constructor and destructor of C++ instance variables in of ObjC objects. So remember to turn that option on and you should be good.
The files will be compiled with the Objective-C++ compiler, so things will behave more like g++ than gcc, but since that's what you want it probably doesn't count as a surprise. From the Objective-C side, I'm not aware of any gotchas, and I've worked on several large projects that made significant used of Objective-C++. Just keep in mind that Objective-C reserved words are reserved in Objective-C++ as well, so you can occasionally run into issues with third-party C++ libraries (e.g., if they use "id" as a parameter in a public interface).
You'd be surprised how well it works. I haven't really gotten into any problems intermixing Obj-C and C++ in a .mm file.