diff --git a/libuavcan/include/uavcan/internal/method_binder.hpp b/libuavcan/include/uavcan/internal/method_binder.hpp index 8d42ff1153..4335edc859 100644 --- a/libuavcan/include/uavcan/internal/method_binder.hpp +++ b/libuavcan/include/uavcan/internal/method_binder.hpp @@ -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(o, true) || - !try_implicit_cast(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(obj_, true) && try_implicit_cast(fun_, true); + } + + void operator()() + { + validateBeforeCall(); + (obj_->*fun_)(); + } template - void operator()(Par1 p1) { (obj_->*fun_)(p1); } + void operator()(Par1 p1) + { + validateBeforeCall(); + (obj_->*fun_)(p1); + } template - void operator()(Par1 p1, Par2 p2) { (obj_->*fun_)(p1, p2); } + void operator()(Par1 p1, Par2 p2) + { + validateBeforeCall(); + (obj_->*fun_)(p1, p2); + } }; }