下面有個類,在這個類中,有個含有boost::function引數的成員函式,現在要在v8中實作它的介面,但問題是怎么將v8::Argument轉換成類中需要的引數boost::function。代碼如下:
class Node{
typedef boost::function<bool(Node*)> EnumCallBack;
public:
bool enumerateChildren(EnumCallBack callback) const
{
if (true == children_.empty())
{
return false;
}
for (Nodes::const_iterator iter = children_.begin();
iter != children_.end();
iter++)
{
callback(*iter);
}
return true;
};
private:
vector<Node*> children_;
};
Handle<Value> NodeEnumerateChildren(const Arguments& args)
{
Local<Object> self=args.Holder();
Local<External> wrap=Local<External>::Cast(self->GetInternalField(0));
void *ptr=wrap->Value();
if (args[0]->IsFunction())
{//convert the argument to boost::function
Handle<Function> ff=Handle<Function>::Cast(args[0]);
boost::function<bool (const Node*)> func=bind(ff,_1);
return Boolean::New(static_cast<Node*>(ptr)->enumerateChildren(func));
}
else
return Boolean::New(false);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/110929.html
標籤:API
