diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2009-12-11 19:22:59 +0000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2009-12-11 19:22:59 +0000 |
commit | 70b75d656ef0053206e0d7ebad7483f4f6993f68 (patch) | |
tree | 1d58c9bba09d882399990acb4890b164eb1ebf92 /libs | |
parent | 9fa8a7e9c2b8d8a447d02a2064e1940506af21c5 (diff) |
add CTClosure for a closure that takes one argument at call time
git-svn-id: svn://localhost/ardour2/branches/3.0@6351 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs')
-rw-r--r-- | libs/pbd/pbd/closure.h | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/libs/pbd/pbd/closure.h b/libs/pbd/pbd/closure.h index e5de2c10da..4151a347d2 100644 --- a/libs/pbd/pbd/closure.h +++ b/libs/pbd/pbd/closure.h @@ -86,7 +86,7 @@ struct Closure { /* will crash if impl is unset */ void operator() () const { (*impl)(); } -private: + protected: ClosureBaseImpl* impl; }; @@ -154,6 +154,46 @@ Closure closure (T& t, void (T::*m)(A1,A2), A1 a1, A2 a2) { return Closure (new template<typename T, typename A1, typename A2, typename A3> Closure closure (T& t, void (T::*m)(A1, A2, A3), A1 a1, A2 a2, A3 a3) { return Closure (new ClosureImpl3<T,A1,A2,A3>(t,m , a1, a2, a3)); } +/*---------*/ + +template<typename A> +struct CTClosureBaseImpl : ClosureBaseImpl { + CTClosureBaseImpl() {} + + virtual void operator() () { operator() (A()); } + virtual void operator() (A arg) = 0; + +protected: + virtual ~CTClosureBaseImpl() { } +}; + +template<typename A> +struct CTClosure : public Closure { + CTClosure() {} + CTClosure (CTClosureBaseImpl<A>* i) : Closure (i) {} + CTClosure (const CTClosure& other) : Closure (other) {} + + /* will crash if impl is unset */ + void operator() (A arg) const { (*(dynamic_cast<CTClosureBaseImpl<A>*> (impl))) (arg); } +}; + +template<typename T, typename A> +struct CTClosureImpl1 : public CTClosureBaseImpl<A> +{ + CTClosureImpl1 (T& obj, void (T::*m)(A)) + : object (obj), method (m) {} + void operator() (A call_time_arg) { (object.*method) (call_time_arg); } + + private: + T& object; + void (T::*method)(A); +}; + +/* functor wraps a method that takes 1 arg provided at call-time */ + +template<typename T, typename A> +CTClosure<A> closure (T& t, void (T::*m)(A)) { return CTClosure<A> (new CTClosureImpl1<T,A>(t,m)); } + } #endif /* __pbd_closure_h__ */ |