C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
Catalog.h
Go to the documentation of this file.
1#pragma once
2
3#include "Synchronization.h"
4#include <map>
5
6namespace MTL {
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>
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 primitives.
std::recursive_mutex CRecursiveMutex
Recursive Mutex.
void rWrite(const DataType &rNewValue)
Definition Catalog.h:51
void Write(const DataType NewValue)
Definition Catalog.h:58
void rRead(DataType &rReturnedValue)
Definition Catalog.h:62
void Subscribe(CObserverNotifier_untyped &rNotifier, CObserver< DataType > &rObs)
Definition Catalog.h:78
void Unsubscribe(CObserverNotifier_untyped &rNotifier)
Definition Catalog.h:84