C++ Instrument Catalog
Catalog.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Synchronization.h"
4 #include <map>
5 
6 namespace MTL {
7  namespace InstrumentCatalogInterface {
8 
9  using namespace MTL::Synchronization;
10  template <class DataType> class CControl;
11  template <class DataType> class CObserver;
12  template <class DataType> class CObserverNotifier;
14 
15  //----------------------------------------------------------------------//
16  // Catalog variable //
17  //----------------------------------------------------------------------//
18  template <class DataType>
19  class CVariable
20  {
21  friend class CControl<DataType>;
22  friend class CObserver<DataType>;
23 
24  //------------------------------------------//
25  // Attributes //
26  //------------------------------------------//
27  private:
28  typedef CObserverNotifier_untyped * tpObserverNotifier; // Observer notifier pointer type
29  typedef CObserver<DataType> * tpObserver; // Observer pointer type
30  std::map<tpObserverNotifier, tpObserver> m_Subscribers; // List of notification subscribers
31  protected:
32  DataType m_Value; // Current value of the variable.
33  CRecursiveMutex m_Lock; // Lock to the current object.
34 
35  public:
36  //------------------------------------------//
37  // Constructors / Desctructors //
38  //------------------------------------------//
40  {}
41  CVariable(DataType InitialValue)
42  : m_Value(InitialValue)
43  {}
44  virtual ~CVariable()
45  {}
46 
47  protected:
48  //------------------------------------------//
49  // Value access //
50  //------------------------------------------//
51  void rWrite(const DataType & rNewValue)
52  {
53  m_Lock.lock();
54  m_Value = rNewValue;
55  m_Lock.unlock();
56  l_NotifyChange();
57  }
58  void Write(const DataType NewValue)
59  {
60  rWrite(NewValue);
61  }
62  void rRead(DataType & rReturnedValue)
63  {
64  m_Lock.lock();
65  rReturnedValue = m_Value;
66  m_Lock.unlock();
67  }
68  DataType Read(void)
69  {
70  DataType l_Value;
71  rRead(l_Value);
72  return l_Value;
73  }
74 
75  //------------------------------------------//
76  // Event handling //
77  //------------------------------------------//
79  {
80  m_Lock.lock();
81  m_Subscribers.insert(std::pair<tpObserverNotifier, tpObserver>(&rNotifier, &rObs));
82  m_Lock.unlock();
83  }
85  {
86  m_Lock.lock();
87  m_Subscribers.erase(&rNotifier); //TODO: Check return
88  m_Lock.unlock();
89  }
90  private:
91  void l_NotifyChange(void)
92  {
93  m_Lock.lock();
94  // For all subscribers
95  for (auto l_it = m_Subscribers.begin();
96  l_it != m_Subscribers.end();
97  l_it++)
98  {
99  // Notify!
100  (l_it->first)->NotifyVarUpdate(*(l_it->second));
101  }
102  m_Lock.unlock();
103  }
104  };
105 
106  } // namespace InstrumentCatalogInterface;
107 } // namespace MTL
Synchronization.h
Synchronization primitives.
MTL::InstrumentCatalogInterface::CObserver
Definition: Catalog.h:11
MTL::InstrumentCatalogInterface::CVariable
Definition: Catalog.h:19
MTL::InstrumentCatalogInterface::CVariable::Write
void Write(const DataType NewValue)
Definition: Catalog.h:58
MTL::Synchronization
Definition: Synchronization.h:12
MTL::InstrumentCatalogInterface::CObserverNotifier
Definition: Catalog.h:12
MTL::InstrumentCatalogInterface::CVariable::Subscribe
void Subscribe(CObserverNotifier_untyped &rNotifier, CObserver< DataType > &rObs)
Definition: Catalog.h:78
MTL::InstrumentCatalogInterface::CControl
Definition: Catalog.h:10
MTL::InstrumentCatalogInterface::CVariable::Read
DataType Read(void)
Definition: Catalog.h:68
MTL
Definition: CPT2026PeripheralROM.h:19
MTL::InstrumentCatalogInterface::CVariable::CVariable
CVariable()
Definition: Catalog.h:39
MTL::InstrumentCatalogInterface::CVariable::CVariable
CVariable(DataType InitialValue)
Definition: Catalog.h:41
MTL::InstrumentCatalogInterface::CVariable::m_Value
DataType m_Value
Definition: Catalog.h:32
MTL::InstrumentCatalogInterface::CVariable::~CVariable
virtual ~CVariable()
Definition: Catalog.h:44
MTL::InstrumentCatalogInterface::CVariable::rWrite
void rWrite(const DataType &rNewValue)
Definition: Catalog.h:51
MTL::InstrumentCatalogInterface::CVariable::Unsubscribe
void Unsubscribe(CObserverNotifier_untyped &rNotifier)
Definition: Catalog.h:84
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped
Definition: CatalogInterface.h:204
MTL::InstrumentCatalogInterface::CVariable::m_Lock
CRecursiveMutex m_Lock
Definition: Catalog.h:33
MTL::InstrumentCatalogInterface::CVariable::rRead
void rRead(DataType &rReturnedValue)
Definition: Catalog.h:62
MTL::Synchronization::CRecursiveMutex
std::recursive_mutex CRecursiveMutex
Recursive Mutex.
Definition: Synchronization.h:20