C++ 中的 @selector 实现

发布于 2024-11-24 23:47:43 字数 727 浏览 2 评论 0原文

我需要在 C++ 中实现类似 @selector(Objecitive-C) 的机制。通过谷歌搜索我发现了。它对我没有帮助...

template <class AnyClass> 
class ClassCallback
{
    // pointer to member function
    void (AnyClass::*function)(void);

    // pointer to object
    AnyClass object;  
public:

    ClassCallback()     {  }
    ~ClassCallback()    {  }

    ClassCallback(AnyClass _object, void(AnyClass::*_function)(void))
    : object( _object ), function( _function ) { };

    virtual void call(){ 
         (object->*function)();
    };

};

是否有任何不同的方法来实现类似于 Objective-C 的方法,例如

typedef struct objc_selector    *MY_SEL;  

需要实现 objc_selector 它是 Objective-C 中的抽象类。如何实现 objc_selector 任何想法......

I need to implement similar mechanism like @selector(Objecitive-C) in C++. By googling i have found. It does not help me...

template <class AnyClass> 
class ClassCallback
{
    // pointer to member function
    void (AnyClass::*function)(void);

    // pointer to object
    AnyClass object;  
public:

    ClassCallback()     {  }
    ~ClassCallback()    {  }

    ClassCallback(AnyClass _object, void(AnyClass::*_function)(void))
    : object( _object ), function( _function ) { };

    virtual void call(){ 
         (object->*function)();
    };

};

Is there any different approach to implement which is similar to objective-C like

typedef struct objc_selector    *MY_SEL;  

Need to implement objc_selector It is abstract class in objective-C. How to implement objc_selector any idea...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朦胧时间 2024-12-01 23:47:44

您可能想做这样的事情:

// We use something to define what "identifies" a
class selector {
private:
   std::string name;
public:
    selector(std::string const& n) : name(n) {}
    selector(const char* n) : name(n) {}
};
typedef const selector* SEL;

像这样使用它:

typedef void* (*method) (object&, ...);
struct object {
private:
   std::hash<SEL,method>* methods;
public:
   object(std::map<SEL,method>* m)  : methods(m) {};
};

selector cloneSelector = selector("clone");
typedef void* cloneImplementation(object&, ...) {
  ....
};

像这样使用它:

std::map<SEL,method> fooMethods;
fooMethods[cloneSelector]=&cloneImplementation;

class foo : public object {
public:
  foo()  : object(&fooMethods) {};
};

但是有几个问题(打字,函数签名......)。您可能不想这样做。

否则,看看代码。 Gobjc 是开源的,很容易看出它是如何完成的。查看 (GNU) gobjc 的 objc/deprecated/struct_objc_selector.h 我发现:

struct objc_selector
{
  void *sel_id;
  const char *sel_types;
};

AFAIK sel_id 是一个指向由方法名称和(编码的)参数类型组成的字符串的指针。

您可能想要对接口中的方法进行分组,并采用稍微“COM 风格”的方式来实现:

 struct interface_id {};

 class object {
 protected:
    virtual void* getVtable_(interface_id const&) = 0;
 public:
    template<class X> X* getVtable() {
        return getVtable(X::interface_id);
    }
    virtual ~object();
 };

 // Exemple interface

 struct FooInterface {
 public:
   class vtable {
     void (*fooMethod2)(void* self, int n);
     std::vector<int> (*fooMethod2)(void* self, double x);
   };
   static interface_id ID;
   static std::string NAME; // = "FooInterface"
 private:
    void* self;
    FooVtable* vtable;
 public:
    void fooMethod1(int n) {
      return vtable->fooMethod1(self,n);
    };
    void fooMethod2(double x) {
      return vtable->fooMethod2(self,double x);
    };
 };

// Exemple object

class foo_object : public bar_object {
private:
   static std::map<interface_id, void*> INTERFACES;
public:
   virtual void* getVtable_(interface_id const& id) {
     void* res = INTERFACES[&id];
     if(res!=null)
        return res;
     else
        return bar_object::getVtable(id);
   };
};

它给出了如何强制函数签名的想法。您应该能够对基于选择器的方法使用相同的东西(使用一些无聊的模板元编程代码......)。

You might want to do something like this :

// We use something to define what "identifies" a
class selector {
private:
   std::string name;
public:
    selector(std::string const& n) : name(n) {}
    selector(const char* n) : name(n) {}
};
typedef const selector* SEL;

Use it like this :

typedef void* (*method) (object&, ...);
struct object {
private:
   std::hash<SEL,method>* methods;
public:
   object(std::map<SEL,method>* m)  : methods(m) {};
};

selector cloneSelector = selector("clone");
typedef void* cloneImplementation(object&, ...) {
  ....
};

Use it like this :

std::map<SEL,method> fooMethods;
fooMethods[cloneSelector]=&cloneImplementation;

class foo : public object {
public:
  foo()  : object(&fooMethods) {};
};

There are several problems however (typing, function signature …). You probably do not want to do this.

Ohterwise, look at the code. Gobjc is open source, it is quite simple to see how it is done. Looking at objc/deprecated/struct_objc_selector.h of (GNU) gobjc I find:

struct objc_selector
{
  void *sel_id;
  const char *sel_types;
};

AFAIK sel_id is a pointer to a string made of the method name and the (encoded) argument types.

You might want to group methods in interfaces and have a slightly more "COM-ish" way of doing it :

 struct interface_id {};

 class object {
 protected:
    virtual void* getVtable_(interface_id const&) = 0;
 public:
    template<class X> X* getVtable() {
        return getVtable(X::interface_id);
    }
    virtual ~object();
 };

 // Exemple interface

 struct FooInterface {
 public:
   class vtable {
     void (*fooMethod2)(void* self, int n);
     std::vector<int> (*fooMethod2)(void* self, double x);
   };
   static interface_id ID;
   static std::string NAME; // = "FooInterface"
 private:
    void* self;
    FooVtable* vtable;
 public:
    void fooMethod1(int n) {
      return vtable->fooMethod1(self,n);
    };
    void fooMethod2(double x) {
      return vtable->fooMethod2(self,double x);
    };
 };

// Exemple object

class foo_object : public bar_object {
private:
   static std::map<interface_id, void*> INTERFACES;
public:
   virtual void* getVtable_(interface_id const& id) {
     void* res = INTERFACES[&id];
     if(res!=null)
        return res;
     else
        return bar_object::getVtable(id);
   };
};

It gives an an idea of how to enforce function signatures. You should be able to use the same kind of thing for the selector-based approach (using some boring template-meta-programming code …).

荒人说梦 2024-12-01 23:47:44

我在使用Cocos2d-x时遇到了类似的问题,在我的例子中,我使用callfunc_selector来获取选择器:

假设一个类Foo和一个方法Bar() 在其中,只需调用 callfunc_selector(Foo::Bar); ,它就会向该方法返回一个 CCObject*

I had a similar problem when using Cocos2d-x, and in my case I used callfunc_selector to get the selector:

Suppose a class Foo and a method Bar() inside it, just call callfunc_selector(Foo::Bar); and it will return a CCObject* to the method.

冷弦 2024-12-01 23:47:43

关于 Objective-C 消息传递系统和选择器的一些精度。

选择器 (SEL) 是一种不透明类型,但它只是类似于 char * 的东西,仅包含方法的名称(因此许多类可以共享相同的选择器)。

Obj-C 方法是一个结构,包含:

  1. SEL 字段
  2. 包含有关返回类型和参数的信息的 char *
  3. IMP 字段

类似于:

struct objc_method
{
    SEL    method_name;
    char * method_types;
    IMP    method_imp;
};
typedef objc_method Method;

IMP 只是一个 C 函数指针。

Just a few precisions on the Objective-C messaging system, with the selectors.

A selector (SEL) is an opaque type, but it's only something like a char *, containing only the name of the method (so many classes can share the same selector).

An Obj-C method is a struct containing:

  1. A SEL field
  2. A char * containing infos about the return type and arguments.
  3. An IMP field

Something like:

struct objc_method
{
    SEL    method_name;
    char * method_types;
    IMP    method_imp;
};
typedef objc_method Method;

An IMP is just a C function pointer.

冷情 2024-12-01 23:47:43

Objective-C 和 C++ 是非常不同的语言,据我所知,C++ 没有任何与 Objective-C 的选择器等效甚至相似的东西。如果您解释了您要解决的问题,我相信这里有人会很乐意讨论从 C++ 角度处理该问题的方式。

Objective-C and C++ are very different languages, and as far as I know C++ doesn't have anything equivalent or even similar to Objective-C's selectors. If you explain the problem you're trying to solve, I'm sure someone here will be happy to discuss the way that problem might be dealt with from a C++ perspective.

孤城病女 2024-12-01 23:47:43

如果您对在编译 C++ 文件之前使用自定义预处理器感到满意,则可以使用 Qt 的信号和槽机制,这是消息传递系统在稍微扩展的 C++ 中的实现。请参阅此处

If you're happy with using a custom preprocessor before compiling C++ files, you can use Qt's signal and slot mechanism, which is an implementation of message-passing system in a slightly-extended C++. See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文