I am currently reworking my callback system to C++11 variadic templates. Basically what I am trying to do is to store a function with a return value and any number of arguments.
Afterwards this function should be called and parameters are loaded from a boost::archive
and return value should be written back to another archive.
#include <string>
#include <map>
#include <iostream>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
using iarchive = boost::archive::binary_iarchive;
using oarchive = boost::archive::binary_oarchive;
using namespace std;
template<class Interface>
class RFCProvider : public Interface {
class FuncStore {
public:
virtual ~FuncStore() {}
virtual void call(iarchive& in, oarchive& out) = 0;
protected:
template <class T> T read(iarchive& in) { T a; in >> a; return a; }
};
template<class Return, class... Args>
class FuncStoreRetN : public FuncStore {
public:
typedef Return(Interface::*Function)(Args... args);
typedef Return ReturnType;
static const int ArgCount = sizeof...(Args);
private:
struct pass {
Return res;
pass(Interface* me, Function func, Args... args) { res = (me->*func)(args...); }
};
Interface* mMe;
Function mFunc;
public:
FuncStoreRetN(Interface* me, Function func) : mMe(me), mFunc(func) {}
void call(iarchive& in, oarchive& out) {
pass c{mMe, mFunc, read<Args>(in)...};
out << c.res;
}
};
map<string, FuncStore*> functionStore;
public:
template<class Return, class... Args>
void registerFunction(const string& name, Return(Interface::*func)(Args...)) {
functionStore[name] = new FuncStoreRetN<Return, Args...>(this, func);
}
};
class TestClass {
public:
bool foo(int x) { return x == 0; }
};
int main() {
RFCProvider<TestClass> p;
p.registerFunction("foo", &TestClass::foo);
return 0;
}
From my understanding pass c{mMe, mFunc, read<Args>(in)...};
should expand to pass c{mMe, mFunc, read<int>(in), read<string>(in), ...};
am I right?
But I cant compile with g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
error: expected primary-expression before ‘>’ token
pass c{mMe, mFunc, read<Args>(in)...};
^
Any suggestions?
EDIT: added MCVE