From: <sv...@ww...> - 2005-04-02 16:43:04
|
Author: mkrose Date: 2005-04-02 08:42:57 -0800 (Sat, 02 Apr 2005) New Revision: 1499 Modified: trunk/CSP/SimCore/Util/Callback.h trunk/CSP/SimCore/Util/CallbackDecl.h Log: Add callback adapter templates for methods that return a value. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1499 Modified: trunk/CSP/SimCore/Util/Callback.h =================================================================== --- trunk/CSP/SimCore/Util/Callback.h 2005-04-02 16:38:12 UTC (rev 1498) +++ trunk/CSP/SimCore/Util/Callback.h 2005-04-02 16:42:57 UTC (rev 1499) @@ -19,16 +19,16 @@ /** * @file Callback.h - * @brief Provides an adaptor for sigc++-1.2 member function slots. + * @brief Provides adaptors for sigc++-1.2 member function slots. * * The adaptors do not require the target class to subclass SigC::Object, * but are equally safe (unlike SigC::slot_class). Automatic disconnect * is handled by making the callbacks instance variables, so they are - * destroyed (and thereby disconnect) at the same time the target object + * destroyed (and thereby disconnected) at the same time the target object * is destroyed. * - * At present the signal and callback classes only support slots with no - * return value and a limited number of arguements. + * At present the signal and callback classes only support slots with a + * limited number of argument and return value combinations. * * Sample use: * @@ -67,6 +67,23 @@ namespace simcore { +// Signals ---------------------------------------------------------------------------- + +class Signal0: public SigC::Signal0<void> { }; + +template <typename M> +class Signal1: public SigC::Signal1<void, M> { }; + +template <typename M, typename N> +class Signal2: public SigC::Signal2<void, M, N> { }; + +template <typename R> +class Signal0R: public SigC::Signal0<R> { }; + +template <typename R, typename M> +class Signal1R: public SigC::Signal1<R, M> { }; + + // Callbacks -------------------------------------------------------------------------- template <class C> @@ -81,6 +98,7 @@ class Callback0: private simdata::ScopedPointer<SigC::Object>, public SigC::Slot0<void> { public: + typedef Signal0 Signal; template <class C> Callback0(C *instance, void (C::*method)()) : simdata::ScopedPointer<SigC::Object>(new _CallbackAdaptor0<C>(instance, method)), @@ -100,6 +118,7 @@ template <typename M> class Callback1: private simdata::ScopedPointer<SigC::Object>, public SigC::Slot1<void, M> { public: + typedef Signal1<M> Signal; template <class C> Callback1(C *instance, void (C::*method)(M)) : simdata::ScopedPointer<SigC::Object>(new _CallbackAdaptor1<C, M>(instance, method)), @@ -119,24 +138,34 @@ template <typename M, typename N> class Callback2: private simdata::ScopedPointer<SigC::Object>, public SigC::Slot2<void, M, N> { public: + typedef Signal2<M, N> Signal; template <class C> Callback2(C *instance, void (C::*method)(M, N)) : simdata::ScopedPointer<SigC::Object>(new _CallbackAdaptor2<C, M, N>(instance, method)), SigC::Slot2<void, M, N>(SigC::slot(*dynamic_cast<_CallbackAdaptor2<C, M, N>*>(get()), &_CallbackAdaptor2<C, M, N>::bounce)) { } }; +template <class C, typename R> +class _CallbackAdaptor0R: public SigC::Object { + typedef R (C::*Method)(); + C *_instance; + Method _method; +public: + inline R bounce() { return (_instance->*_method)(); } + _CallbackAdaptor0R(C *instance, Method method) : _instance(instance), _method(method) { } +}; -// Signals ---------------------------------------------------------------------------- +template <typename R> +class Callback0R: private simdata::ScopedPointer<SigC::Object>, public SigC::Slot0<R> { +public: + typedef Signal0R<R> Signal; + template <class C> + Callback0R(C *instance, R (C::*method)()) : + simdata::ScopedPointer<SigC::Object>(new _CallbackAdaptor0R<C, R>(instance, method)), + SigC::Slot0<R>(SigC::slot(*dynamic_cast<_CallbackAdaptor0R<C, R>*>(get()), &_CallbackAdaptor0R<C, R>::bounce)) { } +}; -class Signal0: public SigC::Signal0<void> { }; -template <typename M> -class Signal1: public SigC::Signal1<void, M> { }; - -template <typename M, typename N> -class Signal2: public SigC::Signal2<void, M, N> { }; - - // ScopedPointer Callbacks ----------------------------------------------------------- template <class C> @@ -153,7 +182,12 @@ ScopedCallback2<M, N>::ScopedCallback2(C *instance, void (C::*method)(M, N)) : simdata::ScopedPointer<Callback2<M, N> >(new Callback2<M, N>(instance, method)) { } +template <typename R> +template <class C> +ScopedCallback0R<R>::ScopedCallback0R(C *instance, R (C::*method)()) + : simdata::ScopedPointer<Callback0R<R> >(new Callback0R<R>(instance, method)) { } + } // namespace simcore #endif // __SIMCORE_UTIL_CALLBACK_H__ Modified: trunk/CSP/SimCore/Util/CallbackDecl.h =================================================================== --- trunk/CSP/SimCore/Util/CallbackDecl.h 2005-04-02 16:38:12 UTC (rev 1498) +++ trunk/CSP/SimCore/Util/CallbackDecl.h 2005-04-02 16:42:57 UTC (rev 1499) @@ -34,10 +34,12 @@ class Callback0; template <typename M> class Callback1; template <typename M, typename N> class Callback2; +template <typename R> class Callback0R; class Signal0; template <typename M> class Signal1; template <typename M, typename N> class Signal2; +template <typename R> class Signal0R; struct ScopedCallback0: public simdata::ScopedPointer<Callback0> { template <class C> ScopedCallback0(C *instance, void (C::*method)()); @@ -53,6 +55,11 @@ template <class C> ScopedCallback2(C *instance, void (C::*method)(M, N)); }; +template <typename R> +struct ScopedCallback0R: public simdata::ScopedPointer<Callback0R<R> > { + template <class C> ScopedCallback0R(C *instance, R (C::*method)()); +}; + } // namespace simcore #endif // __SIMCORE_UTIL_CALLBACKDECL_H__ |