00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef GLSIGNAL_H
00019 #define GLSIGNAL_H
00020
00021 #include <vector>
00022 #include "glassert.h"
00023
00037 template <class ArgType, class ReturnType = void>
00038 class Slot
00039 {
00040 public:
00041 virtual ReturnType operator ()(ArgType arg) = 0;
00042 };
00043
00044
00045 template <class ArgType, class ReturnType = void>
00046 class Signal
00047 {
00048 std::vector< Slot<ArgType,ReturnType>* > slotList;
00049 public:
00050 Signal(){ }
00051
00052 std::vector<ReturnType> send(ArgType arg)
00053 {
00054 std::vector<ReturnType> returns;
00055 for(unsigned i = 0; i < slotList.size(); ++i )
00056 returns.push_back((*slotList[i])(arg));
00057 return returns;
00058 }
00059
00060 void sendWithVector(ArgType arg, std::vector<ReturnType>& vec)
00061 {
00062 for(unsigned i = 0; i < slotList.size(); ++i )
00063 vec.push_back((*slotList[i])(arg));
00064 }
00065
00066 void connect(Slot<ArgType,ReturnType>* s)
00067 { slotList.push_back(s); }
00068
00069 void disconnect(Slot<ArgType,ReturnType>* s)
00070 { slotList.erase(s); }
00071 };
00072
00073 template <class ArgType>
00074 class Signal<ArgType, void>
00075 {
00076 std::vector< Slot<ArgType,void>* > slotList;
00077 public:
00078 Signal(){ }
00079
00080 void send(ArgType arg)
00081 {
00082 for(unsigned i = 0; i < slotList.size(); ++i )
00083 (*slotList[i])(arg);
00084 }
00085
00086 void connect(Slot<ArgType,void>* s)
00087 { slotList.push_back(s); }
00088
00089 void disconnect(Slot<ArgType,void>* s)
00090 { slotList.erase(s); }
00091 };
00092
00093 #endif