简体   繁体   中英

c++ A Generic Callback object implementation

EDIT : I've rewote this question in: new question

I'm currently using an implementation of callback mechanism from flash for c++, ASInterface.inl you can see an example here:

https://github.com/cpzhang/bud/blob/a37348b77fb2722c73d972a77827056b0b2344f6/trunk/tool/flash/ASInterface.inl#L393 .

I'm looking for a standard implementation for generic callback like that (in std or in boost or something else), that is not coupled with flash player. What it does basically is to implement a generic Callback object that can be called with arbitrary number of arguments of primitive types.

//init callbacks
typedef std::map<std::wstring, Callback> callbacks;
void SomethingHappened(int a, int b) {print a + b;}
string functionName = "SomethingHappened";
callbacks[functionName] = &SomethingHappened;
//use callbacks after xml input arrived:
string xml = "<some xml input document/>";
Callbacks::iterator itCallback = callbacks.find(functionName);
if (itCallback != callbacks.end())
    {
        //parse arguments
        std::vector<std::wstring> args;
        _Args::split(xml, args);
        ASValue::Array arguments;
        for (size_t i = 0, s = args.size(); i < s; ++i)
        {
            ASValue arg; arg.FromXML(args[i]);
            arguments.push_back(arg);
        }
        ASValue returnValue;
        //***this is where the magic happens: call the function***
        HRESULT result = itCallback->second.Call(arguments, returnValue);
        return result;
    }

edit this is my flow: I get input message from "server": Message(String command, String xmlArguments) it find the callback named: command it invokes command with arguments (which are arbitrary in number and types). What I'm looking is the implementation of the generic command (as there is in the link.)

I didn't closely inspect all the nooks and crannies of that code, but is there something against this:

 using Callback = std::function<ASValue(ASValue::Array const&)>;

std::function is in C++ since c++11. If you don't have that (?) you can use boost::function<> with the same semantics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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