从 C++ 启动 Cocoa GUI循环并传递引用
我有一个问题...也许有几个:)
我的团队使用 Boost 和 CMake 用 C++ 编写了一个客户端来处理与平台相关的内容。效果就像一个魅力! ...有时:) 无论如何,Windows 的图形界面已经完成,但 Mac OS X 的图形界面还没有完成。这也是我的任务。当然,我使用的是 Cocoa,但问题是主循环位于 C++ 代码中,而且 GUI 应该根据客户端更新一些动画。
这是怎么做到的? 我对如何确定线程以及如何将引用从 C++ 代码传递到 GUI 代码感到非常困惑。现在,GUI 代码有自己的小 MVC,与项目的其余部分完全分离。 我环顾四周,也许我太菜鸟了:),但我没有发现任何我真正可以使用的东西......我并不真正理解这一切。
我厌倦了重新创建 NSApplication。我发现这个有用的教程: http://cocoawithlove.com/2009/01/demystifying-nsapplication-by。 html
我看过一些代表:
委托在 Objective-C 中如何工作?
我有稍微看了一下 NSNotificationCenter:
如何创建一个类在 Objective-C 中通过 NSNotificationCenter 发送和接收事件?
也许这里有人知道如何让我理解?需要注意什么?我应该看哪里?
I have a problem... maybe a couple :)
My team has written a client in C++ using Boost and CMake to take care of the platform dependent stuff. Works like a charm! ...sometimes :) Anyway the graphical interface for Windows is done but for Mac OS X it isn't. Which is also my assignment. Naturally I'm using Cocoa but the problem is that the main loop is in the C++ code and also the GUI should update a few animations depending on the client.
How is this done?
I'm very confused about how to nail the threading and how to pass references from the C++ code to the GUI code. Right now the GUI code has its own little MVC totally separated form the rest of the project.
I have looked around a lot, maybe I'm too much of a noob :), but I don't fine anything I can really use... I don't really understand it all.
I have tired to recreate the NSApplication. I found this helpful tutorial:
http://cocoawithlove.com/2009/01/demystifying-nsapplication-by.html
I have looked a little at Delegates:
How does a delegate work in objective-C?
I have looked a little at NSNotificationCenter:
How to create a class to send and receive events through NSNotificationCenter in Objective-C?
Maybe someone here knows how to make me understand? What to look into? Where I should look?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题相当笼统,所以我只能为你想要移植到 Mac OS X 并拥有原生 Cocoa UI 的主要基于 C++ 的应用程序提供一些高级架构技巧。
线程:仅仅因为 C++ 代码在 Windows 上有主线程并不意味着它需要在 Mac OS X 上。使用 NSApplication 以通常的 Cocoa 方式启动应用程序,然后在应用程序委托的
applicationDidFinishLaunching
你应该做两件事:语言障碍 您需要解决语言障碍。您的 C++ 代码无法直接访问 Objective-C 对象,反之亦然。您可以使用 Objective-C++ 解决这个问题,但最佳实践是仅将 Objective-C++ 用于薄层转换层,并将两者分开。除了提供更规范的编程环境之外,它还有助于从逻辑上将 UI 代码(Objective-C 中)与较低级别的应用程序服务(C++ 中)分离。 Rob Napier 写了很多关于如何做到这一点的文章,我建议阅读他最近的博客文章 以及他之前关于该主题的一些帖子。
更一般地说,您将使用几种技术之一从 C++ 转到 Objective-C,因为这是您询问的方向:
提供可以从 Objective-C 或 C++ 调用的 C 接口函数。它们的实现可以用任何最方便的语言编写。这些函数的标头将包含如下行,让 C++ 代码知道它们具有 C 而不是 C++ 接口:
提供 Objective-C++ 桥接函数,在调用本机之前将 C++ 对象转换为 Objective-C 对象Objective-C 代码。
编写 Objective-C++ 桥对象。它们为底层 Objective-C 对象提供了一层薄薄的 C++ 饰面,以便 C++ 层可以与其交互。
显然,您需要反向使用类似的技术来从 Objective-C 代码调用 C++。
Your question is rather general so I can only give some high level architectural tips for a predominantly C++ based application that you want to port to Mac OS X and have a native Cocoa UI.
Threads: Just because the C++ code has the main thread on Windows does not mean that it needs to be on Mac OS X. Start your application the usual Cocoa way with NSApplication and then in your app delegate's
applicationDidFinishLaunching
you should do two things:Language Barrier You have a language barrier to deal with. Your C++ code can't directly access Objective-C objects and visa versa. You can smooth over this problem with Objective-C++ but best practice is to use Objective-C++ only for a thin translation layer and keep the two separate. In addition to providing a more canonical programming environment it also helps logically to separate UI code (in Objective-C) from lower level application services (in C++). Rob Napier has written a lot about how to do this and I suggest reading his recent blog post along with some of his earlier posts on the subject.
To elaborate a bit more generally you will be using one of several techniques to go from C++ to Objective-C since that is the direction you asked about:
Provide C interface functions that can be called from Objective-C or C++. Their implementation can be written in whatever language is most convenient. The header for these functions will have lines like the following to let the C++ code know they have C and not C++ interfaces:
Provide Objective-C++ bridge functions that take C++ objects and translated them to Objective-C objects before calling the native Objective-C code.
Write Objective-C++ bridge objects. These provide a thin C++ veneer to an underlying Objective-C object so the C++ layer can interact with it.
Obviously you will need to use similar techniques in the reverse to call from Objective-C code to C++.