简体   繁体   English

使用C ++编写的算法实时处理数据

[英]Real time processing data using algorithms written in c++

Here is the scenario: 这是场景:

My swift app collects data from bluetooth and should process them in real-time. 我的Swift应用程式会从蓝牙收集资料,并应进行即时处理。 These data comes mainly from IMU(gyroscope, magnetometer and accelerometer). 这些数据主要来自IMU(陀螺仪,磁力计和加速度计)。

Algorithms that process all of that is going to be written in c++ and utilize some libraries like Eigen . 处理所有这些问题的算法将用c ++编写,并利用一些库,例如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. 1)将c ++文件放入我的项目中,在Objective-c中编写包装器并将其桥接到Swift。 Also now sure if I can include Eigen in mobile app easily. 现在还要确定我是否可以轻松地将Eigen包含在移动应用中。 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) 2)将所有算法获取为库( .dll.lib ,然后直接从swift调用它,不确定是否可行)

3) Rewrite all algorithms to Swift, utilize Eigen substitute for Swift, not sure if anything like this exist. 3)将所有算法重写为Swift,使用Eigen替代Swift,不确定是否存在类似的东西。 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? 如何以最有效的方式解决它,在这里我可以利用已经存在的c ++代码?

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? Swift是如何在iOS应用中运行使用Eigen的C ++文件的?

Thanks in advance 提前致谢

You cannot call c++ from Swift. 您不能从Swift调用c ++。 However, you can call c++ from objective-c++. 但是,您可以从Objective-C ++调用C ++。 And you can call objective-c++ from both Swift and objective-c. 您可以从Swift和Objective-c调用Objective-C ++。

Just make sure that your public-facing @interface code (in the .h file) contains only objective-c. 只需确保面向公众的@interface代码(在.h文件中)仅包含Objective-C。 (no classes, no templates, no namespaces, no including c++ headers). (没有类,没有模板,没有名称空间,不包括c ++头)。

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. 您的@implementation放在扩展名为.mm的文件中,该文件将作为Objective-C ++进行编译-为您提供一个Objective-C接口,并在实现Objective-C对象时充分利用了C ++。

c++ objects live quite happily as the member variables of an objective-c++ implementation. C ++对象作为目标C ++实现的成员变量生活得很愉快。 However, they will be default-constructed then the objc runtime calls Init . 但是,它们将是默认构造的,然后objc运行时将调用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). 如果您使用的对象没有默认构造函数,则需要将它们包装在boost::optionalstd::unique_ptr (等)中。

You can them import the objective-c objects into your Swift program. 他们可以将Objective-C对象导入到Swift程序中。

Full example for anyone who has not done this before: 完整的示例,适用于以前从未做过的任何人:

https://github.com/madmongo1/swift-to-cpp-demo.git https://github.com/madmongo1/swift-to-cpp-demo.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM