我可以将 boost::bind() 转换为 Objective C 函数吗?

发布于 2024-07-25 05:15:25 字数 55 浏览 4 评论 0原文

我不知道这是否可能,但如果可以,语法会是什么样子?

如果不可能,为什么不呢?

I have no idea if this is possible, but if it is, what would the syntax look like?

If not possible, why not?

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

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

发布评论

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

评论(1

涙—继续流 2024-08-01 05:15:25

您应该能够绑定到消息实现 (IMP),它们只是带有两个隐藏参数的普通 C 函数:self_cmd分别输入 idSEL

编辑:刚刚测试了以下完整示例,它似乎可以工作。

#include <stdio.h>
#include <boost/bind.hpp>
#include <Foundation/NSObject.h>

@interface MyClass : NSObject
{
}
-(int) doSomething:(int)arg;
@end

@implementation MyClass
-(int) doSomething:(int)arg
{
  printf("doSomething: self=0x%08x _cmd=0x%08x\n", self, _cmd);
  return arg + 1;
}
@end

int main(void)
{
  MyClass *myObj = [[MyClass alloc] init], *otherObj = [[MyClass alloc] init];
  typedef int (*MyFunc)(id, SEL, int);
  SEL doSomething_sel = @selector(doSomething:);
  MyFunc doSomething_impl = (MyFunc)[myObj methodForSelector:doSomething_sel];

  // bind self & _cmd arguments:
  // calls [myObj doSomething:x]
  int result = boost::bind(doSomething_impl, myObj, doSomething_sel, _1)(14);
  printf("result1: %d\n", result);

  // bind _cmd & arg:
  // calls [otherObj doSomething:3]
  result = boost::bind(doSomething_impl, _1, doSomething_sel, 42)(otherObj);
  printf("result2: %d\n", result);

  return 0;
}

使用 GNUstep,编译为:

gcc objcbind.mm -o objcbind -I/usr/include/GNUstep -lobjc -lstdc++ -lgnustep-base

在 Mac OS X 上,编译为:

gcc objcbind.mm -o objcbind -framework Foundation -lstdc++

输出:

doSomething: self=0x01a85f70 _cmd=0x00602220
result1: 15
doSomething: self=0x01a83d70 _cmd=0x00602220
result2: 43

You should be able to bind to a message implementation (IMP), which are just plain C functions with two hidden parameters, self and _cmd of types id and SEL respectively.

EDIT: Just tested the following complete example, and it appears to work.

#include <stdio.h>
#include <boost/bind.hpp>
#include <Foundation/NSObject.h>

@interface MyClass : NSObject
{
}
-(int) doSomething:(int)arg;
@end

@implementation MyClass
-(int) doSomething:(int)arg
{
  printf("doSomething: self=0x%08x _cmd=0x%08x\n", self, _cmd);
  return arg + 1;
}
@end

int main(void)
{
  MyClass *myObj = [[MyClass alloc] init], *otherObj = [[MyClass alloc] init];
  typedef int (*MyFunc)(id, SEL, int);
  SEL doSomething_sel = @selector(doSomething:);
  MyFunc doSomething_impl = (MyFunc)[myObj methodForSelector:doSomething_sel];

  // bind self & _cmd arguments:
  // calls [myObj doSomething:x]
  int result = boost::bind(doSomething_impl, myObj, doSomething_sel, _1)(14);
  printf("result1: %d\n", result);

  // bind _cmd & arg:
  // calls [otherObj doSomething:3]
  result = boost::bind(doSomething_impl, _1, doSomething_sel, 42)(otherObj);
  printf("result2: %d\n", result);

  return 0;
}

With GNUstep, compile as:

gcc objcbind.mm -o objcbind -I/usr/include/GNUstep -lobjc -lstdc++ -lgnustep-base

On Mac OS X, compile as:

gcc objcbind.mm -o objcbind -framework Foundation -lstdc++

Output:

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