lundi 22 février 2021

c++ A Generic Callback object implementation

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.

typedef std::map<std::wstring, Callback> callbacks;
string functionName = "SomethingHappened";
string xml = "<some xml 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;
    }

Aucun commentaire:

Enregistrer un commentaire