From what I gather, you want to call Y::call_it()
with a std::function
object appropriately bound. Given that your inner function takes different numbers of arguments, it is necessary to create a std::function<bool()>
generator for the cases where additional arguments are being passed. Assuming the X
object can be bound at registration time, registration of a member function without additional arguments is straight forward:
template <int Magic, typename RC>
void register_call(RC (X::*member)()) {
somewhere->register_call<Magic>(
std::bind(&Y::call_it, this,
std::function<bool()>(std::bind(member,
std::ref(this->x_)))));
}
When passing one or more arguments, it is necessary to create a std::function<bool()>
object upon call time because the additional argument needs to be bound. I don't think that this can be done without a helper function but it can be done with one helper function per number of arguments:
template <typename RC, typename Arg0>
static std::function<bool()>
bind_argument(RC (X::*member)(Arg0), X& x, Arg0 const& arg0) {
return std::bind(member, std::ref(x), arg0);
}
template <int Magic, typename RC,
typename Arg0, typename PlaceHolder>
void register_call(RC (X::*member)(Arg0), PlaceHolder pc) {
somewhere->register_call<Magic>(
typename some_traits_type<Magic>::type(
std::bind(&Y::call_it, this,
std::bind(&bind_argument<RC, Arg0>, member,
std::ref(this->x_), pc))));
}
The helper function creates a function with an additional argument being bound. Note that the function being bound is constructed to be of the same type as the one expected by the register function: This is necessary, e.g., to create a function taking additional, ignored, arguments.
Below is a test program I used to see if things compile. I don't have a C++2003 compiler with TR1 at hand and have compiled the code with a C++2011 compiler. However, I don't think I have used any of the C++2011 extension which isn't available from C++2003 with TR1.
#include <functional>
enum {
MAGIC_OK,
MAGIC_FAILED,
MAGIC_FOO,
MAGIC_BAR,
MAGIC_FBZ,
MAGIC_BAZ
};
template <int> struct server_traits;
template <> struct server_traits<MAGIC_FOO> {
typedef std::function<bool()> type;
};
template <> struct server_traits<MAGIC_BAR> {
typedef std::function<bool(std::string&)> type;
};
template <> struct server_traits<MAGIC_FBZ> {
typedef std::function<bool(long)> type;
};
template <> struct server_traits<MAGIC_BAZ> {
typedef std::function<bool(std::string, long)> type;
};
// this I just use
class server {
public:
template<unsigned int MagicTag>
bool register_call(typename server_traits<MagicTag>::type) {
return true;
}
};
server s;
server* somewhere = &s;
// this needs to be called from the server
class X {
public:
bool foo() { return true; }
bool bar(std::string&) { return true; }
bool baz(int) { return true; }
};
// this is the glue
class Y {
public:
Y(X& x) : x_(x) {
register_call<MAGIC_FOO>(&X::foo );
register_call<MAGIC_BAR>(&X::bar, std::placeholders::_1);
register_call<MAGIC_FBZ>(&X::baz, std::placeholders::_1);
register_call<MAGIC_BAZ>(&X::baz, std::placeholders::_2);
}
private:
X& x_;
int call_it(std::function<bool()> f) {
return f() ? MAGIC_OK : MAGIC_FAILED;
}
template <int Magic, typename RC>
void register_call(RC (X::*member)()) {
somewhere->register_call<Magic>(
std::bind(&Y::call_it, this,
std::function<bool()>(std::bind(member,
std::ref(this->x_)))));
}
template <typename RC, typename Arg0>
static std::function<bool()>
bind_argument(RC (X::*member)(Arg0), X& x, Arg0 const& arg0) {
return std::bind(member, std::ref(x), arg0);
}
template <int Magic, typename RC,
typename Arg0, typename PlaceHolder>
void register_call(RC (X::*member)(Arg0), PlaceHolder pc) {
somewhere->register_call<Magic>(
typename server_traits<Magic>::type(
std::bind(&Y::call_it, this,
std::bind(&bind_argument<RC, Arg0>, member,
std::ref(this->x_), pc))));
}
};
int main()
{
X x;
Y y(x);
}