简体   繁体   English

从C ++库通知Objective C

[英]Notify Objective C from C++ library

I'm implementing a static C++ library for sorting algorithm using Xcode. 我正在实现一个静态C ++库,用于使用Xcode进行排序算法。 I want to use this lib (file .a) in my iPhone App. 我想在我的iPhone应用程序中使用此库(文件.a)。 Of course I will have to write a wrapper for C++ and C (file .mm).However my problem is that I want to be notified from this lib after each fixed period. 当然,我将不得不为C ++和C(文件.mm)写一个包装器,但是我的问题是我希望在每个固定时间后从该lib收到通知。

For example, I'm implementing "Insertion" algorithm, and I want the process happens in 10s, and then after each second, the lib will return a new sorted array (of course it's incomplete) until the last complete sorted array. 例如,我正在实现“插入”算法,并且我希望该过程在10秒钟内发生,然后在每秒之后,库将返回一个新的排序数组(当然它是不完整的),直到最后一个完整的排序数组为止。 How can I do this? 我怎样才能做到这一点? How to notify Objective C from static lib C++ like this ? 如何从静态lib C ++这样通知Objective C? If not using static lib, I suppose the problem is not difficult, but I want to re-use this lib in Java also. 如果不使用静态库,我想问题并不难,但是我也想在Java中重用此库。

I have a similar project where much of the logic is implemented as a C++ library, which can be compiled either as a static or dynamic library. 我有一个类似的项目,其中大部分逻辑都实现为C ++库,可以将其编译为静态或动态库。 I also have a command line C++ test program, so all of my callbacks, which basically amount to how far they have got with something with the option to cancel, are implemented as simple C++ callbacks: 我也有一个命令行C ++测试程序,因此我所有的回调(基本上等于它们可以选择取消的功能)都实现为简单的C ++回调:

extern "C"
{
/**
 * Callback function from long operations.
 *
 * @param gameNum The number of the game being processed (1-based).
 * @param percentComplete Processing progress. (0.0 to 100.0).
 * @param contextInfo Context Info passed to the database method.
 *
 * @return false to terminate processing, else true.
 */
typedef bool(*DATABASE_CALLBACK_FUNC)(unsigned gameNum, float percentComplete, void *contextInfo);
}

This works equally well in the C++ command line tool as the Objective-C++ based Cocoa app. 在C ++命令行工具中,这与基于Objective-C ++的Cocoa应用程序一样有效。

Notice that I use extern C to make it also possible to use from C. 请注意,我使用extern C使其也可以从C使用。

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

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