简体   繁体   English

dispatch_queue_t 作为 C++ 类中的实例变量

[英]dispatch_queue_t as instance variable in C++ class

I am trying to write a wrapper around (GCD) queue.我正在尝试围绕 (GCD) 队列编写一个包装器。

I have it like this in header file我在头文件中有这样的

#include <dispatch/dispatch.h>

namespace myspace {

class Queue 
{

public: 
    Queue(dispatch_queue_t dispatchQueue);
private:
    dispatch_queue_t dispatchQueue_;
}
}

The counterpart of header is .mm file with implementation.标题的对应物是带有实现的 .mm 文件。

I import Queue.h into some other pure C++ file and when linking I get:我将 Queue.h 导入其他一些纯 C++ 文件,链接时我得到:

Undefined symbols for architecture armv7:
   "myspace::Queue::Queue(dispatch_queue_s*)", referenced from:
        myspace::NiceClass::CreateOneMoreQueue() in NiceClass.o 

In NiceClass I create a new Queue:在 NiceClass 中,我创建了一个新队列:

Queue *queue = new Queue(dispatch_get_main_queue());

I'm stuck with this one.我被这个困住了。 It seems like I can't do it like this.好像我不能这样做。 I could change dispatch_queue_t to void* and it works like that but I lose type information and get lots of casts.我可以将dispatch_queue_t更改为void*并且它的工作原理是这样,但是我丢失了类型信息并获得了很多强制转换。

I would appreciate any idea.我将不胜感激任何想法。

EDIT: this is implementation in Queue.mm.编辑:这是 Queue.mm 中的实现。 Memory management and other details omitted.省略了内存管理和其他细节。

Queue::Queue(dispatch_queue_t dispatchQueue) 
{
    dispatchQueue_ = dispatchQueue;
    if (dispatchQueue_ == NULL) {
        dispatchQueue_ = dispatch_get_main_queue();
    }
}

void Queue::AddMessage(Handler *handler, Context *context)
{
    Message *msg = new Message;
    msg->phandler = handler;
    msg->context = context;
    dispatch_async(dispatchQueue_, ^{
        handler->ProcessMessage(msg);
    });
}

EDIT2: Here is a test project which fails for me zip 32k EDIT2:这是一个测试项目,它对我来说失败了zip 32k

Wow!哇! 5 years ago! 5年前! For anyone who stumbles across this now!对于现在偶然发现的任何人!

If myspace is a 'cpp'(Pure C++) file and Queue is a 'mm'(ObjC++) file, you will have linker failures across the language barrier when passing "dispatch" objects as they are NSObjects in ObjC++.如果 myspace 是“cpp”(纯 C++)文件而 Queue 是“mm”(ObjC++)文件,则在传递“调度”对象时,由于它们是 ObjC++ 中的 NSObjects,您将跨越语言障碍出现链接器故障。

Change both sides to '.mm' and it should resolve the linker issues.将两边更改为“.mm”,它应该可以解决链接器问题。

add these lines:添加这些行:

namespace myspace {
....
}

around the implementation of围绕实施

Queue::....

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

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