C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
CatalogInterface.h
Go to the documentation of this file.
1#pragma once
2
3#include "Catalog.h"
4#include <functional>
5
6namespace MTL {
8
9 //----------------------------------------------------------------------//
10 // Catalog Interface //
11 //----------------------------------------------------------------------//
12 //----------------------------------------------------------------------
13 // Control
14 template <class DataType>
16 {
17 private:
18 CVariable<DataType> & m_rVariable;
19 public:
20 // Constrution / Destruction
22 : m_rVariable(rVariable)
23 {}
24 virtual ~CControl()
25 {}
26
27 // Control
28 void RequestControl(const DataType NewValue) // Request a control with the given value.
29 {
30 m_rVariable.Write(NewValue);
31 }
32 void rRequestControl(const DataType & rNewValue) // Request a control with the given value, by reference.
33 {
34 m_rVariable.rWrite(rNewValue);
35 }
36 void operator() (const DataType & rNewValue) // Request a control with the given value, by reference.
37 {
38 m_rVariable.rWrite(rNewValue);
39 }
40 void operator= (const DataType & rNewValue) // Request a control with the given value, by reference.
41 {
42 m_rVariable.rWrite(rNewValue);
43 }
44
45 // Reading
46 DataType ReadCurrent(void) // Read the current value of the control.
47 {
48 return m_rVariable.Read();
49 }
50 DataType operator() () // Read the current value of the control.
51 {
52 return m_rVariable.Read();
53 }
54 void rReadCurrent(DataType & rReturnedValue) // Read the current value of the control, by reference.
55 {
56 m_rVariable.rRead(rReturnedValue);
57 }
58 };
59
60 //----------------------------------------------------------------------
61 // Untyped observer
62 class CObserver_untyped // Provide a generic class which is untyped
63 {
64 public:
65 // Constrution / Destruction
67 {}
68
69 // Identification
70 bool Is(const CObserver_untyped & rObs)
71 { return (this == &rObs); }
72 bool Is(const CObserver_untyped * pObs)
73 { return (this == pObs); }
74
75 // Copy manipulation
76 virtual void * Copy(void) = 0;
77 virtual void Delete(void * const pAllocatedData) = 0;
78 };
79
80 //----------------------------------------------------------------------
81 // Observer
82 template <class DataType>
84 {
85 protected:
87 public:
88 // Constrution / Destruction
90 : m_rVariable(rVariable) {}
91 virtual ~CObserver()
92 {}
93
94 // Subscription
95 void Subscribe(CObserverNotifier_untyped & rNotifier) // Subscribe to observer change. Notify that changes through the given Notifier.
96 {
97 m_rVariable.Subscribe(rNotifier, *this);
98 }
99 void Unsubscribe(CObserverNotifier_untyped & rNotifier) // Unsubscribe from observer change.
100 {
101 m_rVariable.Unsubscribe(rNotifier);
102 }
103
104 // Reading
105 DataType ReadCurrent(void) // Read the current value of the observer.
106 {
107 return m_rVariable.Read();
108 }
109 DataType operator() () // Read the current value of the control.
110 {
111 return m_rVariable.Read();
112 }
113 void rReadCurrent(DataType & rReturnedValue) // Read the current value of the observer, by reference.
114 {
115 m_rVariable.rRead(rReturnedValue);
116 }
117
118 // Copy manipulation
119 void * Copy(void) // Allocate a copy of the content of the observer and return its pointer.
120 {
121 DataType * l_pDataCopy = new DataType;
122 m_rVariable.rRead(*l_pDataCopy);
123 return l_pDataCopy;
124 }
125 void Delete(void * const pAllocatedData) // Delete the content from the allocated buffer.
126 {
127 if (NULL != pAllocatedData)
128 return;
129 delete reinterpret_cast<DataType *>(pAllocatedData);
130 }
131 };
132
133 //----------------------------------------------------------------------//
134 // Synchronous Notifications //
135 //----------------------------------------------------------------------//
136 // There is no synchronous notification class since a synchronous
137 // notifier consists in a direct call of a callback function.
138
139 //----------------------------------------------------------------------//
140 // Asynchronous Notifications //
141 //----------------------------------------------------------------------//
142 //----------------------------------------------------------------------
143 // Generic asynchronous notification (not to be used directly)
145 {
146 private:
147 CObserverNotifier_untyped & m_rNotifier;
148 protected:
150 protected:
151 // Construction / Destruction
153 : m_rNotifier(rNot), m_rObserver(rObs)
154 {}
155 public:
157 {}
158
159 // Member access
161 { return m_rNotifier; }
163 { return m_rObserver; }
164 };
165
166 //----------------------------------------------------------------------
167 // Asynchronous dynamic notification (notification whose data can change between emission and reception of this notification)
177
178 //----------------------------------------------------------------------
179 // Asynchronous snapshop notification (notification which embedds a copy of the associated observer data)
181 {
182 private:
183 void * m_pDataCopy;
184 public:
186 : CAsyncNotification(rNot, rObs), m_pDataCopy(NULL)
187 {
188 m_pDataCopy = m_rObserver.Copy();
189 }
191 {
192 m_rObserver.Delete(m_pDataCopy);
193 }
194
195 const void * DataCopy () const
196 { return m_pDataCopy; }
197 };
198
199 //----------------------------------------------------------------------//
200 // Notifiers //
201 //----------------------------------------------------------------------//
202 //----------------------------------------------------------------------
203 // Generic untyped notifier class
204 class CObserverNotifier_untyped // Provide a generic class which is untyped
205 {
206 public:
207 typedef enum NotifierType {
208 kSync, // Synchronous notifier.
209 kAsyncDynamic, // Asynchronous notifier with dynamic data.
210 kAsyncSnapshot // Asynchronous notifier including a snapshot of the data.
212 private:
213 const tNotifierType m_NotifierType;
214 public:
216 : m_NotifierType(NotifType)
217 {}
220
222 { return (this == &rNot); }
223 bool Is(const CObserverNotifier_untyped * pNot)
224 { return (this == pNot); }
226 { return m_NotifierType; }
227
228 virtual void NotifyVarUpdate(CObserver_untyped & rObs) = 0;
229 };
230
231 //----------------------------------------------------------------------
232 // Synchronous notifier
233 template <class DataType>
235 {
236 public:
237 typedef std::function<void(CObserver<DataType> & rObserver)> tCallback;
238 private:
239 tCallback m_Callback;
240 public:
241 // Constructor as: CSyncNotifier<DataType> Notifier(Function)
242 CSyncNotifier(const tCallback & rCallB)
243 : CObserverNotifier_untyped(kSync), m_Callback(rCallB)
244 {}
245 // Constructor as: CSyncNotifier<DataType> Notifier(&Class::method, &ClassObj)
246 template<class Fn, class... Args>
247 CSyncNotifier(Fn&& MemberCallback, Args&&... ClassObj)
248 : CObserverNotifier_untyped(kSync), m_Callback(std::bind(MemberCallback, ClassObj..., std::placeholders::_1))
249 {}
251 {}
252
254 {
255 // Call the callback
256 m_Callback(reinterpret_cast<CObserver<DataType> &>(rObs));
257 }
258 };
259
260 //----------------------------------------------------------------------
261 // Asynchronous notifier
262 template <class NotificationType>
264 {
265 public:
266 typedef std::function<void(CAsyncNotification * pAsyncNotification)> tPushEventCallback;
267 private:
268 tPushEventCallback m_PushNotifCallback;
269 protected:
271 : CObserverNotifier_untyped(NotifType), m_PushNotifCallback(rCallB)
272 {}
274 {}
275
277 {
278 CAsyncNotification * l_pNotif = new NotificationType(*this, rObs);
279 m_PushNotifCallback(l_pNotif);
280 }
281 };
282
283 //----------------------------------------------------------------------
284 // Asynchronous Dynamic notifier
285 class CAsyncDynamicNotifier : public CAsyncNotifier<CAsyncDynamicNotification>
286 {
287 public:
288 // Constructor as: CAsyncDynamicNotifier Notifier(Function)
291 // Constructor as: CAsyncDynamicNotifier Notifier(&Class::method, &ClassObj)
292 template<class Fn, class... Args>
293 CAsyncDynamicNotifier(Fn&& MemberCallback, Args&&... ClassObj)
294 : CAsyncNotifier<CAsyncDynamicNotification>(CObserverNotifier_untyped::kAsyncDynamic, std::bind(MemberCallback, ClassObj..., std::placeholders::_1)) {}
295 };
296
297 //----------------------------------------------------------------------
298 // Asynchronous Snapshot notifier (embedded data copy)
299 class CAsyncSnaphsotNotifier : public CAsyncNotifier<CAsyncSnapshotNotification>
300 {
301 public:
302 // Constructor as: CAsyncSnaphsotNotifier Notifier(Function)
305 // Constructor as: CAsyncSnaphsotNotifier Notifier(&Class::method, &ClassObj)
306 template<class Fn, class... Args>
307 CAsyncSnaphsotNotifier(Fn&& MemberCallback, Args&&... ClassObj)
308 : CAsyncNotifier<CAsyncSnapshotNotification>(CObserverNotifier_untyped::kAsyncSnapshot, std::bind(MemberCallback, ClassObj..., std::placeholders::_1)) {}
309 };
310
311 } // namespace InstrumentCatalogInterface;
312} // namespace MTL
void rRequestControl(const DataType &rNewValue)
void operator=(const DataType &rNewValue)
CControl(CVariable< DataType > &rVariable)
void rReadCurrent(DataType &rReturnedValue)
void RequestControl(const DataType NewValue)
void rReadCurrent(DataType &rReturnedValue)
void Unsubscribe(CObserverNotifier_untyped &rNotifier)
CObserver(CVariable< DataType > &rVariable)
void Delete(void *const pAllocatedData)
void Subscribe(CObserverNotifier_untyped &rNotifier)
virtual void Delete(void *const pAllocatedData)=0
CAsyncNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
CAsyncDynamicNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
CAsyncSnapshotNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
bool Is(const CObserverNotifier_untyped *pNot)
virtual void NotifyVarUpdate(CObserver_untyped &rObs)=0
enum MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::NotifierType tNotifierType
CSyncNotifier(Fn &&MemberCallback, Args &&... ClassObj)
std::function< void(CObserver< DataType > &rObserver)> tCallback
std::function< void(CAsyncNotification *pAsyncNotification)> tPushEventCallback
CAsyncNotifier(tNotifierType NotifType, const tPushEventCallback &rCallB)
CAsyncDynamicNotifier(Fn &&MemberCallback, Args &&... ClassObj)
CAsyncDynamicNotifier(const tPushEventCallback &rCallB)
CAsyncSnaphsotNotifier(Fn &&MemberCallback, Args &&... ClassObj)