MAK Data Logger API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
signalSlot.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 1992-2025 MAK Technologies, Inc
3 ** All rights reserved.
4 ******************************************************************************/
5 
10 
11 
12 #ifndef signalSlot_H_
13 #define signalSlot_H_
14 
15 #include <vlutil/vlSmartPointers.h>
16 #include <algorithm>
17 #include <list>
18 
19 namespace MAKLogger
20 {
21 
22 template <typename SignalType>
23 class DtSlot
24 {
25 public:
26 
27  DtSlot(){}
28  virtual ~DtSlot(){}
29 
30  virtual void operator()(const SignalType&) = 0;
32  virtual void* id() = 0;
33  virtual int idSize() = 0;
34 
36  {
38  if(this->idSize() != slot2.idSize()) return false;
40  return memcmp(this->id(),slot2.id(),this->idSize()) == 0;
41  }
42 
43 };
44 
45 template <typename SignalType>
46 class DtFunctionSlot : public DtSlot<SignalType>
47 {
48 public:
49  typedef void (*SlotFunction)(const SignalType&);
50 
52  myFunction(function)
53  {
54  }
55 
57  virtual void operator()(const SignalType& value)
58  {
59  myFunction(value);
60  }
61 
62  virtual int idSize()
63  {
64  return sizeof(myFunction);
65  }
66 
67  virtual void* id()
68  {
69  return &myFunction;
70  }
71 
72 protected:
74 };
75 
76 template <typename ClassType,typename SignalType>
77 class DtMemberSlot : public DtSlot<SignalType>
78 {
79 public:
80 
81  typedef void (ClassType::*SlotMemberFunction)(const SignalType&);
82  struct MemberSlotId
83  {
84  MemberSlotId(ClassType* objectValue, SlotMemberFunction memberFunctionValue)
85  : object(objectValue)
86  , memberFunction(memberFunctionValue)
87  {
88  }
89  ClassType* object;
91  };
92 
93  DtMemberSlot(ClassType* obj,SlotMemberFunction memberFunction)
94  : myId(obj, memberFunction)
95  {}
96 
97  virtual ~DtMemberSlot()
98  {
99  }
100 
101  virtual void operator()(const SignalType& value)
102  {
103  (myId.object->*myId.memberFunction)(value);
104  }
105 
106  virtual int idSize()
107  {
108  return sizeof(myId);
109  }
110 
111  virtual void* id()
112  {
113  return &myId;
114  }
115 
116 protected:
118 };
119 
120 
121 template <typename SignalType>
122 class DtSignal
123 {
124 public:
125  typedef void (*DtSlotFunction)(const SignalType&);
126 
128  typedef std::shared_ptr<SlotType> SlotTypeSP;
129  typedef std::list<SlotTypeSP> SlotList;
130 
131  void connect(SlotTypeSP slot)
132  {
133  mySlots.push_back(slot);
134  }
135 
137  {
138  typename SlotList::iterator newEnd = std::remove_if(mySlots.begin(),mySlots.end(), std::bind2nd(std::ptr_fun(compare),slot));
139  mySlots.erase(newEnd, mySlots.end());
140  }
141 
142  void operator()(const SignalType& value)
143  {
144  typename SlotList::iterator func = mySlots.begin();
145  typename SlotList::iterator end = mySlots.end();
146  for(;func != end;++func)
147  {
148  SlotTypeSP& s = *func;
149  s->operator()(value);
150  }
151  }
152 
153 protected:
154 
155  static bool compare(SlotTypeSP slot1,SlotTypeSP slot2)
156  {
157  return *(slot1.get()) == *(slot2.get());
158  }
159 
161 };
162 
163 template <typename SignalType>
164 class DtValueSignal : public DtSignal<SignalType>
165 {
166 public:
167  DtValueSignal(SignalType& valueRef) :
168  myRef(valueRef)
169  {
170  }
171 
172  void setAndEmitOnNotEqual(const SignalType& newValue)
173  {
174  if(newValue != myRef)
175  {
176  myRef = newValue;
177  (*this)(myRef);
178  }
179  }
180 
181  void setAndEmitOnEqual(const SignalType& newValue)
182  {
183  if(newValue == myRef)
184  {
185  myRef = newValue;
186  (*this)(myRef);
187  }
188  else
189  {
190  myRef = newValue;
191  }
192  }
193 
194  void setAndEmit(const SignalType& newValue)
195  {
196  myRef = newValue;
197  (*this)(myRef);
198  }
199 
200 
201 protected:
202  SignalType& myRef;
203 };
204 
205 
206 
207 template<typename SignalType>
208 void DtConnect(DtSignal<SignalType>& signal,void (*slot)(SignalType))
209 {
211 }
212 
213 template <typename ClassType,typename SignalType>
214 void DtConnect(DtSignal<SignalType>& signal,ClassType* member,void (ClassType::*slot)(const SignalType&))
215 {
216 #ifdef __solaris__
218 #else
220 #endif
221 }
222 
223 template<typename SignalType>
224 void DtDisconnect(DtSignal<SignalType>& signal,void (*slot)(SignalType) )
225 {
227 }
228 
229 template <typename ClassType,typename SignalType>
230 void DtDisconnect(DtSignal<SignalType>& signal,ClassType* member,void (ClassType::*slot)(const SignalType&) )
231 {
233 }
234 
235 }
236 
237 #endif
DtFunctionSlot(SlotFunction function)
Definition: signalSlot.h:51
void(* DtSlotFunction)(const SignalType &)
Definition: signalSlot.h:125
virtual void operator()(const SignalType &)=0
Definition: signalSlot.h:122
bool operator==(DtSlot< SignalType > &slot2)
Definition: signalSlot.h:35
virtual void * id()
Ids necessary for comparison.
Definition: signalSlot.h:67
Definition: signalSlot.h:23
SlotFunction myFunction
Definition: signalSlot.h:73
Definition: signalSlot.h:82
MemberSlotId myId
Definition: signalSlot.h:117
void operator()(const SignalType &value)
Definition: signalSlot.h:142
DtValueSignal(SignalType &valueRef)
Definition: signalSlot.h:167
DtSlot< SignalType > SlotType
Definition: signalSlot.h:127
void setAndEmitOnNotEqual(const SignalType &newValue)
Definition: signalSlot.h:172
void(ClassType::* SlotMemberFunction)(const SignalType &)
Definition: signalSlot.h:81
void(* SlotFunction)(const SignalType &)
Definition: signalSlot.h:49
std::list< SlotTypeSP > SlotList
Definition: signalSlot.h:129
virtual void * id()=0
Ids necessary for comparison.
MemberSlotId(ClassType *objectValue, SlotMemberFunction memberFunctionValue)
Definition: signalSlot.h:84
virtual void * id()
Ids necessary for comparison.
Definition: signalSlot.h:111
void setAndEmitOnEqual(const SignalType &newValue)
Definition: signalSlot.h:181
void disconnect(SlotTypeSP slot)
Definition: signalSlot.h:136
virtual void operator()(const SignalType &value)
Definition: signalSlot.h:101
virtual ~DtMemberSlot()
Definition: signalSlot.h:97
DtMemberSlot(ClassType *obj, SlotMemberFunction memberFunction)
Definition: signalSlot.h:93
SlotList mySlots
Definition: signalSlot.h:160
virtual int idSize()
Definition: signalSlot.h:62
void connect(SlotTypeSP slot)
Definition: signalSlot.h:131
SignalType & myRef
Definition: signalSlot.h:202
virtual int idSize()=0
static bool compare(SlotTypeSP slot1, SlotTypeSP slot2)
Definition: signalSlot.h:155
virtual int idSize()
Definition: signalSlot.h:106
void DtDisconnect(DtSignal< SignalType > &signal, void(*slot)(SignalType))
Definition: signalSlot.h:224
SlotMemberFunction memberFunction
Definition: signalSlot.h:90
ClassType * object
Definition: signalSlot.h:89
virtual ~DtSlot()
Definition: signalSlot.h:28
DtSlot()
Definition: signalSlot.h:27
virtual void operator()(const SignalType &value)
Call function.
Definition: signalSlot.h:57
void DtConnect(DtSignal< SignalType > &signal, void(*slot)(SignalType))
Definition: signalSlot.h:208
Definition: signalSlot.h:77
Definition: signalSlot.h:46
void setAndEmit(const SignalType &newValue)
Definition: signalSlot.h:194
std::shared_ptr< SlotType > SlotTypeSP
Definition: signalSlot.h:128
Definition: signalSlot.h:164

Document ID: Generated on Thu Dec 18 15:35:09 EST 2025 from SVN revision 282494
Copyright © 2024 MAK Technologies. All Rights Reserved (www.mak.com)