C++ Instrument Catalog
CatalogInterface.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Catalog.h"
4 #include <functional>
5 
6 namespace MTL {
7  namespace InstrumentCatalogInterface {
8 
9  //----------------------------------------------------------------------//
10  // Catalog Interface //
11  //----------------------------------------------------------------------//
12  //----------------------------------------------------------------------
13  // Control
14  template <class DataType>
15  class CControl
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>
83  class CObserver : public CObserver_untyped
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)
169  {
170  public:
172  : CAsyncNotification(rNot, rObs)
173  {}
175  {}
176  };
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.
211  } tNotifierType;
212  private:
213  const tNotifierType m_NotifierType;
214  public:
216  : m_NotifierType(NotifType)
217  {}
219  {}
220 
222  { return (this == &rNot); }
223  bool Is(const CObserverNotifier_untyped * pNot)
224  { return (this == pNot); }
225  const tNotifierType & Type()
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  {}
250  virtual ~CSyncNotifier()
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  {}
273  virtual ~CAsyncNotifier()
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
MTL::InstrumentCatalogInterface::CAsyncNotifier::tPushEventCallback
std::function< void(CAsyncNotification *pAsyncNotification)> tPushEventCallback
Definition: CatalogInterface.h:266
MTL::InstrumentCatalogInterface::CAsyncNotifier::~CAsyncNotifier
virtual ~CAsyncNotifier()
Definition: CatalogInterface.h:273
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification::CAsyncSnapshotNotification
CAsyncSnapshotNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
Definition: CatalogInterface.h:185
MTL::InstrumentCatalogInterface::CObserver
Definition: Catalog.h:11
MTL::InstrumentCatalogInterface::CSyncNotifier::CSyncNotifier
CSyncNotifier(Fn &&MemberCallback, Args &&... ClassObj)
Definition: CatalogInterface.h:247
MTL::InstrumentCatalogInterface::CVariable
Definition: Catalog.h:19
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::kAsyncDynamic
@ kAsyncDynamic
Definition: CatalogInterface.h:209
MTL::InstrumentCatalogInterface::CObserver::CObserver
CObserver(CVariable< DataType > &rVariable)
Definition: CatalogInterface.h:89
MTL::InstrumentCatalogInterface::CSyncNotifier
Definition: CatalogInterface.h:234
MTL::InstrumentCatalogInterface::CSyncNotifier::NotifyVarUpdate
void NotifyVarUpdate(CObserver_untyped &rObs)
Definition: CatalogInterface.h:253
MTL::InstrumentCatalogInterface::CSyncNotifier::~CSyncNotifier
virtual ~CSyncNotifier()
Definition: CatalogInterface.h:250
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification
Definition: CatalogInterface.h:180
MTL::InstrumentCatalogInterface::CAsyncSnaphsotNotifier
Definition: CatalogInterface.h:299
MTL::InstrumentCatalogInterface::CObserver::Copy
void * Copy(void)
Definition: CatalogInterface.h:119
MTL::InstrumentCatalogInterface::CObserver_untyped::Copy
virtual void * Copy(void)=0
MTL::InstrumentCatalogInterface::CObserver::rReadCurrent
void rReadCurrent(DataType &rReturnedValue)
Definition: CatalogInterface.h:113
MTL::InstrumentCatalogInterface::CAsyncNotifier::CAsyncNotifier
CAsyncNotifier(tNotifierType NotifType, const tPushEventCallback &rCallB)
Definition: CatalogInterface.h:270
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::Type
const tNotifierType & Type()
Definition: CatalogInterface.h:225
MTL::InstrumentCatalogInterface::CAsyncNotification::~CAsyncNotification
virtual ~CAsyncNotification()
Definition: CatalogInterface.h:156
MTL::InstrumentCatalogInterface::CControl::rReadCurrent
void rReadCurrent(DataType &rReturnedValue)
Definition: CatalogInterface.h:54
MTL::InstrumentCatalogInterface::CObserver::~CObserver
virtual ~CObserver()
Definition: CatalogInterface.h:91
MTL::InstrumentCatalogInterface::CObserver::ReadCurrent
DataType ReadCurrent(void)
Definition: CatalogInterface.h:105
MTL::InstrumentCatalogInterface::CObserver_untyped::Is
bool Is(const CObserver_untyped *pObs)
Definition: CatalogInterface.h:72
MTL::InstrumentCatalogInterface::CAsyncSnaphsotNotifier::CAsyncSnaphsotNotifier
CAsyncSnaphsotNotifier(Fn &&MemberCallback, Args &&... ClassObj)
Definition: CatalogInterface.h:307
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::NotifyVarUpdate
virtual void NotifyVarUpdate(CObserver_untyped &rObs)=0
MTL::InstrumentCatalogInterface::CControl::operator()
DataType operator()()
Definition: CatalogInterface.h:50
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification::~CAsyncSnapshotNotification
virtual ~CAsyncSnapshotNotification()
Definition: CatalogInterface.h:190
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::tNotifierType
enum MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::NotifierType tNotifierType
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification::DataCopy
const void * DataCopy() const
Definition: CatalogInterface.h:195
MTL
Definition: CPT2026PeripheralROM.h:19
MTL::InstrumentCatalogInterface::CAsyncSnaphsotNotifier::CAsyncSnaphsotNotifier
CAsyncSnaphsotNotifier(const tPushEventCallback &rCallB)
Definition: CatalogInterface.h:303
MTL::InstrumentCatalogInterface::CObserver::operator()
DataType operator()()
Definition: CatalogInterface.h:109
MTL::InstrumentCatalogInterface::CAsyncNotification::CAsyncNotification
CAsyncNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
Definition: CatalogInterface.h:152
MTL::InstrumentCatalogInterface::CControl::CControl
CControl(CVariable< DataType > &rVariable)
Definition: CatalogInterface.h:21
MTL::InstrumentCatalogInterface::CObserver::Subscribe
void Subscribe(CObserverNotifier_untyped &rNotifier)
Definition: CatalogInterface.h:95
MTL::InstrumentCatalogInterface::CObserver::Unsubscribe
void Unsubscribe(CObserverNotifier_untyped &rNotifier)
Definition: CatalogInterface.h:99
MTL::InstrumentCatalogInterface::CControl::RequestControl
void RequestControl(const DataType NewValue)
Definition: CatalogInterface.h:28
MTL::InstrumentCatalogInterface::CObserver::Delete
void Delete(void *const pAllocatedData)
Definition: CatalogInterface.h:125
MTL::InstrumentCatalogInterface::CAsyncNotification::Notifier
CObserverNotifier_untyped & Notifier() const
Definition: CatalogInterface.h:160
MTL::InstrumentCatalogInterface::CControl::ReadCurrent
DataType ReadCurrent(void)
Definition: CatalogInterface.h:46
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::Is
bool Is(const CObserverNotifier_untyped *pNot)
Definition: CatalogInterface.h:223
MTL::InstrumentCatalogInterface::CObserver_untyped
Definition: CatalogInterface.h:62
MTL::InstrumentCatalogInterface::CObserver_untyped::Delete
virtual void Delete(void *const pAllocatedData)=0
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::~CObserverNotifier_untyped
virtual ~CObserverNotifier_untyped()
Definition: CatalogInterface.h:218
MTL::InstrumentCatalogInterface::CAsyncDynamicNotifier::CAsyncDynamicNotifier
CAsyncDynamicNotifier(const tPushEventCallback &rCallB)
Definition: CatalogInterface.h:289
MTL::InstrumentCatalogInterface::CControl::rRequestControl
void rRequestControl(const DataType &rNewValue)
Definition: CatalogInterface.h:32
MTL::InstrumentCatalogInterface::CAsyncNotifier::NotifyVarUpdate
void NotifyVarUpdate(CObserver_untyped &rObs)
Definition: CatalogInterface.h:276
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped
Definition: CatalogInterface.h:204
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::Is
bool Is(CObserverNotifier_untyped &rNot)
Definition: CatalogInterface.h:221
MTL::InstrumentCatalogInterface::CAsyncDynamicNotification
Definition: CatalogInterface.h:168
MTL::InstrumentCatalogInterface::CAsyncNotification
Definition: CatalogInterface.h:144
MTL::InstrumentCatalogInterface::CObserver_untyped::~CObserver_untyped
virtual ~CObserver_untyped()
Definition: CatalogInterface.h:66
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::kAsyncSnapshot
@ kAsyncSnapshot
Definition: CatalogInterface.h:210
MTL::InstrumentCatalogInterface::CAsyncDynamicNotifier
Definition: CatalogInterface.h:285
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::NotifierType
NotifierType
Definition: CatalogInterface.h:207
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::kSync
@ kSync
Definition: CatalogInterface.h:208
MTL::InstrumentCatalogInterface::CControl::operator=
void operator=(const DataType &rNewValue)
Definition: CatalogInterface.h:40
MTL::InstrumentCatalogInterface::CAsyncDynamicNotification::CAsyncDynamicNotification
CAsyncDynamicNotification(CObserverNotifier_untyped &rNot, CObserver_untyped &rObs)
Definition: CatalogInterface.h:171
MTL::InstrumentCatalogInterface::CAsyncNotification::m_rObserver
CObserver_untyped & m_rObserver
Definition: CatalogInterface.h:149
MTL::InstrumentCatalogInterface::CSyncNotifier::tCallback
std::function< void(CObserver< DataType > &rObserver)> tCallback
Definition: CatalogInterface.h:237
MTL::InstrumentCatalogInterface::CAsyncNotifier
Definition: CatalogInterface.h:263
Catalog.h
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped::CObserverNotifier_untyped
CObserverNotifier_untyped(tNotifierType NotifType)
Definition: CatalogInterface.h:215
MTL::InstrumentCatalogInterface::CSyncNotifier::CSyncNotifier
CSyncNotifier(const tCallback &rCallB)
Definition: CatalogInterface.h:242
MTL::InstrumentCatalogInterface::CControl::~CControl
virtual ~CControl()
Definition: CatalogInterface.h:24
MTL::InstrumentCatalogInterface::CAsyncDynamicNotifier::CAsyncDynamicNotifier
CAsyncDynamicNotifier(Fn &&MemberCallback, Args &&... ClassObj)
Definition: CatalogInterface.h:293
MTL::InstrumentCatalogInterface::CObserver::m_rVariable
CVariable< DataType > & m_rVariable
Definition: CatalogInterface.h:86
MTL::InstrumentCatalogInterface::CAsyncDynamicNotification::~CAsyncDynamicNotification
virtual ~CAsyncDynamicNotification()
Definition: CatalogInterface.h:174
MTL::InstrumentCatalogInterface::CObserver_untyped::Is
bool Is(const CObserver_untyped &rObs)
Definition: CatalogInterface.h:70
MTL::InstrumentCatalogInterface::CAsyncNotification::Observer
CObserver_untyped & Observer() const
Definition: CatalogInterface.h:162