Update of /cvsroot/pclasses/pclasses2/include/pclasses
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9919/include/pclasses
Modified Files:
Callback.h Signal.h
Log Message:
Added Signal2, Callback2.
Index: Callback.h
===================================================================
RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Callback.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Callback.h 6 Jan 2005 16:59:23 -0000 1.2
+++ Callback.h 11 Jan 2005 14:50:52 -0000 1.3
@@ -263,6 +263,127 @@
+/* ----------------- Callback2 ------------------ */
+
+//! Callback base class with two arguments
+template <typename RetType, typename ArgType1, typename ArgType2>
+class Callback2 {
+ public:
+ virtual ~Callback2() { }
+ virtual RetType exec(ArgType1, ArgType2) const = 0;
+
+ protected:
+ Callback2() { }
+};
+
+//! Callback base class with two arguments (void specialisation)
+template <typename ArgType1, typename ArgType2>
+class Callback2<void, ArgType1, ArgType2> {
+ public:
+ virtual ~Callback2() { }
+ virtual void exec(ArgType1, ArgType2) const = 0;
+
+ protected:
+ Callback2() { }
+
+};
+
+/* -------------- Function2 ------------- */
+
+//! Function callback class with two arguments
+template <typename RetType, typename ArgType1, typename ArgType2>
+class Function2: public Callback2<RetType, ArgType1, ArgType2> {
+ public:
+ typedef RetType (*FuncPtr)(ArgType1, ArgType2);
+
+ Function2(FuncPtr ptr)
+ : _funcPtr(ptr) { }
+
+ RetType exec(ArgType1 arg1, ArgType2 arg2) const
+ { return (*_funcPtr)(arg1, arg2); }
+
+ bool operator==(const Function2& f) const
+ { return _funcPtr == f._funcPtr; }
+
+ private:
+ FuncPtr _funcPtr;
+};
+
+//! Function callback class with two arguments (void specialisation)
+template <typename ArgType1, typename ArgType2>
+class Function2<void, ArgType1, ArgType2>: public Callback2<void, ArgType1, ArgType2> {
+ public:
+ typedef void (*FuncPtr)(ArgType1, ArgType2);
+
+ Function2(FuncPtr ptr)
+ : _funcPtr(ptr) { }
+
+ void exec(ArgType1 arg1, ArgType2 arg2) const
+ { (*_funcPtr)(arg1, arg2); }
+
+ bool operator==(const Function2& f) const
+ { return _funcPtr == f._funcPtr; }
+
+ private:
+ FuncPtr _funcPtr;
+};
+
+//! Returns a function-callback object
+template <typename RetType, typename ArgType1, typename ArgType2>
+Function2<RetType, ArgType1, ArgType2> function(RetType (*ptr)(ArgType1,ArgType2))
+{ return Function2<RetType, ArgType1, ArgType2>(ptr); }
+
+/* ---------- Method2 ------------ */
+
+//! Method callback class with two arguments
+template <typename RetType, class ObjT, typename ArgType1, typename ArgType2>
+class Method2: public Callback2<RetType, ArgType1, ArgType2> {
+ public:
+ typedef RetType (ObjT::*FuncPtr)(ArgType1, ArgType2);
+
+ Method2(ObjT* obj, FuncPtr ptr)
+ : _obj(obj), _funcPtr(ptr) { }
+
+ RetType exec(ArgType1 arg1, ArgType2 arg2) const
+ { return (_obj->*_funcPtr)(arg1, arg2); }
+
+ bool operator==(const Method2& f) const
+ { return _obj == f._obj && _funcPtr == f._funcPtr; }
+
+ private:
+ ObjT* _obj;
+ FuncPtr _funcPtr;
+};
+
+//! Method callback class with two arguments (void specialisation)
+template <class ObjT, typename ArgType1, typename ArgType2>
+class Method2<void, ObjT, ArgType1, ArgType2>:
+ public Callback2<void, ArgType1, ArgType2>
+{
+ public:
+ typedef void (ObjT::*FuncPtr)(ArgType1, ArgType2);
+
+ Method2(ObjT* obj, FuncPtr ptr)
+ : _obj(obj), _funcPtr(ptr) { }
+
+ void exec(ArgType1 arg1, ArgType2 arg2) const
+ { (_obj->*_funcPtr)(arg1, arg2); }
+
+ bool operator==(const Method2& f) const
+ { return _obj == f._obj && _funcPtr == f._funcPtr; }
+
+ private:
+ ObjT* _obj;
+ FuncPtr _funcPtr;
+};
+
+//! Returns a method-callback object
+template <typename RetType, class ObjT, typename ArgType1, typename ArgType2>
+Method2<RetType, ObjT, ArgType1, ArgType2>
+method(ObjT* obj, RetType (ObjT::*ptr)(ArgType1, ArgType2))
+{ return Method2<RetType, ObjT, ArgType1, ArgType2>(obj, ptr); }
+
+
} // !namespace P
#endif
Index: Signal.h
===================================================================
RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Signal.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Signal.h 6 Jan 2005 16:59:24 -0000 1.2
+++ Signal.h 11 Jan 2005 14:50:52 -0000 1.3
@@ -152,7 +152,7 @@
void unbind(CallbackType slot)
{ unbind_slot(_slots, slot); }
- virtual RetType fire() const = 0;
+ virtual RetType fire(ArgType1) const = 0;
protected:
CallbackList _slots;
@@ -194,6 +194,72 @@
};
+/* -------------------- Signal2 ---------------- */
+
+//! Base class for Signal2
+template <typename RetType, typename ArgType1, typename ArgType2>
+class SignalBase2 {
+ public:
+ typedef std::list<
+ Callback2<RetType, ArgType1, ArgType2>*
+ > CallbackList;
+
+ SignalBase2()
+ { }
+
+ virtual ~SignalBase2()
+ { unbind_slots(_slots); }
+
+ template <class CallbackType>
+ void bind(CallbackType slot)
+ { bind_slot(_slots, slot); }
+
+ template <class CallbackType>
+ void unbind(CallbackType slot)
+ { unbind_slot(_slots, slot); }
+
+ virtual RetType fire(ArgType1, ArgType2) const = 0;
+
+ protected:
+ CallbackList _slots;
+};
+
+//! Signal with two arguments
+template <typename RetType, typename ArgType1, typename ArgType2>
+class Signal2: public SignalBase2<RetType, ArgType1, ArgType2> {
+ public:
+ RetType fire(ArgType1 arg1, ArgType2 arg2) const
+ {
+ RetType ret = RetType();
+ typename CallbackList::const_iterator i = _slots.begin();
+ while(i != _slots.end())
+ {
+ ret = (*i)->exec(arg1, arg2);
+ if(ret)
+ break;
+ ++i;
+ }
+
+ return ret;
+ }
+};
+
+//! Signal with two arguments (void specialisation)
+template <typename ArgType1, typename ArgType2>
+class Signal2<void, ArgType1, ArgType2>: public SignalBase2<void, ArgType1, ArgType2> {
+ public:
+ void fire(ArgType1 arg1, ArgType2 arg2) const
+ {
+ typename CallbackList::const_iterator i = _slots.begin();
+ while(i != _slots.end())
+ {
+ (*i)->exec(arg1, arg2);
+ ++i;
+ }
+ }
+};
+
+
} // !namespace P
#endif
|