diff options
Diffstat (limited to 'thinfunc.h')
| -rw-r--r-- | thinfunc.h | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -33,7 +33,7 @@ template<class F> struct func; template<class R, class... Args> struct func<R(Args...)>{ - virtual R operator()(Args...) = 0; + virtual R operator()(Args...) const = 0; }; template<class T, class F> @@ -43,6 +43,7 @@ class bound_anchor; template<class T, class R, class... Args> class anchor<T, R(Args...)>{ + friend T; public: template<anchor<T, R(Args...)> (T::*Self), R (T::*Method)(Args...)> using bound_type = bound_anchor<T, @@ -50,15 +51,28 @@ public: private: alignas(bound_type<nullptr, nullptr>) char body[sizeof(bound_type<nullptr, nullptr>)]; -public: +protected: template<anchor<T, R(Args...)> (T::*Self), R (T::*Method)(Args...)> auto& bind(){ - return *(new (this->body) bound_type<Self, Method>()); + static_assert(sizeof(bound_type<Self, Method>) == sizeof(body)); + static_assert( + (func<R(Args...)> *)(bound_type<nullptr, nullptr> *)nullptr + == (func<R(Args...)> *)(bound_type<Self, Method> *)nullptr); + return *(new (body) bound_type<Self, Method>()); } +public: template<anchor<T, R(Args...)> (T::*Self), R (T::*Method)(Args...)> - auto& assume_bound() const{ - return *(bound_type<Self, Method> *)this->body; + auto& bound(){ + return *(bound_type<Self, Method> *)body; + } + func<R(Args...)>& bound(){ + return *(func<R(Args...)> *)(bound_type<nullptr, nullptr> *)body; } + anchor(const anchor&) = delete; + anchor(const anchor&&) = delete; + anchor& operator=(const anchor&) = delete; + anchor& operator=(const anchor&&) = delete; + anchor() = default; }; template< @@ -75,7 +89,7 @@ public: bound_anchor(const bound_anchor&&) = delete; bound_anchor& operator=(const bound_anchor&) = delete; bound_anchor& operator=(const bound_anchor&&) = delete; - virtual R operator()(Args... args){ + virtual R operator()(Args... args) const{ auto offset = (char *)&(((T *)0)->*Self) - (char *)0; T *container = (T *)((char *)this - offset); return (container->*Method)(args...); |
