Improved MethodBinder

This commit is contained in:
Pavel Kirienko 2014-03-09 16:53:32 +04:00
parent c1a8f818f8
commit c7351c4bbb

View File

@ -20,13 +20,9 @@ class MethodBinder
ObjectPtr obj_;
MemFunPtr fun_;
public:
MethodBinder(ObjectPtr o, MemFunPtr f)
: obj_(o)
, fun_(f)
void validateBeforeCall() const
{
if (!try_implicit_cast<bool>(o, true) ||
!try_implicit_cast<bool>(f, true))
if (!operator bool())
{
#if UAVCAN_EXCEPTIONS
throw std::runtime_error(typeid(*this).name());
@ -37,13 +33,41 @@ public:
}
}
void operator()() { (obj_->*fun_)(); }
public:
MethodBinder()
: obj_()
, fun_()
{ }
MethodBinder(ObjectPtr o, MemFunPtr f)
: obj_(o)
, fun_(f)
{ }
operator bool() const
{
return try_implicit_cast<bool>(obj_, true) && try_implicit_cast<bool>(fun_, true);
}
void operator()()
{
validateBeforeCall();
(obj_->*fun_)();
}
template <typename Par1>
void operator()(Par1 p1) { (obj_->*fun_)(p1); }
void operator()(Par1 p1)
{
validateBeforeCall();
(obj_->*fun_)(p1);
}
template <typename Par1, typename Par2>
void operator()(Par1 p1, Par2 p2) { (obj_->*fun_)(p1, p2); }
void operator()(Par1 p1, Par2 p2)
{
validateBeforeCall();
(obj_->*fun_)(p1, p2);
}
};
}