C++ Instrument Catalog
MFCCatalogExample.cpp
Go to the documentation of this file.
1 //
8 //
9 
10 #include <iostream>
11 #include <queue>
12 #include <fstream> // ifstream to read files
13 #include <windows.h> // GetModuleFileName
14 
15 #include "InstrumentCatalog.h"
16 #include "MFCManager.h"
17 
18 using namespace MTL;
19 
21 {
22 private:
24  CAsyncSnaphsotNotifier m_ASNotifier;
25  std::mutex m_QLock;
26  // This queue is the heart of the interchange mechanism.
27  // The catalog being "interchange-agnostic", it is up to you to
28  // select which type of communication mechanism you want to implement
29  // in your application. In this specific and simple example, we
30  // aimed for a queue.
31  std::queue< CAsyncNotification *> m_Q;
32 
33 private:
34  void l_PushNewAsyncNotification(CAsyncNotification * pANot)
35  {
36  std::lock_guard<std::mutex> l_LG(m_QLock);
37  m_Q.push(pANot);
38  }
39  CAsyncNotification * l_GetNotification()
40  {
41  std::lock_guard<std::mutex> l_LG(m_QLock);
42  if (m_Q.empty())
43  return nullptr;
44 
45  CAsyncNotification * l_pNot = m_Q.front();
46  m_Q.pop();
47  return l_pNot;
48  }
49 public:
51  : m_Cat(rCat), m_ASNotifier(&CMyApplication::l_PushNewAsyncNotification, this)
52  {
53  }
54  void SyncControls()
55  {
56  // We do not need to sync anything with the catalog for this application
57  }
59  {
60  // We do not need to sync anything with the catalog for this application
61  }
62  void Subscribe()
63  {
64  // Proceed to the subscription to each variable of interrest.
65  m_Cat.oInstrumentList.Subscribe(m_ASNotifier);
66  m_Cat.oProbeArrayInformation.Subscribe(m_ASNotifier);
67  m_Cat.oSearchResult.Subscribe(m_ASNotifier);
68  m_Cat.oInstrumentStatus.Subscribe(m_ASNotifier);
69  m_Cat.oError.Subscribe(m_ASNotifier);
70  }
71 
72  void DoJob()
73  {
74  bool l_Operate = true;
75  while (l_Operate)
76  {
77  // Get the current notification.
78  CAsyncNotification * l_pNot = l_GetNotification();
79  if (nullptr == l_pNot)
80  {
81  // No notification currently in the queue.
82  // Sleep for a while.
83  std::this_thread::sleep_for(std::chrono::milliseconds(100));
84  }
85  else
86  {
87  // Since we have a notification...
88  CObserver_untyped & rVar = l_pNot->Observer();
89  const CAsyncSnapshotNotification * l_pASNot = static_cast<CAsyncSnapshotNotification *>(l_pNot);
90 
91  // try determining which variable spawned it.
92  if (rVar.Is(m_Cat.oInstrumentList))
93  {
94  std::cout << "New Instrument List" << std::endl;
95  const CInstrumentCatalog::tInstrumentList * l_pVal = static_cast<const CInstrumentCatalog::tInstrumentList *>(l_pASNot->DataCopy());
96  if (!l_pVal->empty())
97  m_Cat.cInstrumentCurrent(l_pVal->at(0));
98  }
99  else if (rVar.Is(m_Cat.oProbeArrayInformation))
100  {
101  std::cout << "New Probe-Array Information" << std::endl;
104  {
109  l_NewState.FreqMin = l_pVal->FreqMin;
110  l_NewState.FreqMax = l_pVal->FreqMax;
111  l_NewState.NbAveragedMeasurements = 0;
112  m_Cat.cInstrumentState(l_NewState);
113  }
114  }
115  else if (rVar.Is(m_Cat.oSearchResult))
116  {
117  const CInstrumentCatalog::sSearchResult * l_pVal = static_cast<const CInstrumentCatalog::sSearchResult *>(l_pASNot->DataCopy());
118  std::cout << "New Search Result :" << CInstrumentCatalog::l_Dump(*l_pVal);
119  l_Operate = false;
120  }
121  else if (rVar.Is(m_Cat.oInstrumentStatus))
122  {
123  const CInstrumentCatalog::sInstrumentStatus * l_pVal = static_cast<const CInstrumentCatalog::sInstrumentStatus *>(l_pASNot->DataCopy());
124  std::cout << CInstrumentCatalog::l_Dump(*l_pVal);
125  }
126  else if (rVar.Is(m_Cat.oError))
127  {
128  const CInstrumentCatalog::sError * l_pVal = static_cast<const CInstrumentCatalog::sError *>(l_pASNot->DataCopy());
129  std::cout << CInstrumentCatalog::l_Dump(*l_pVal);
130  l_Operate = false;
131  }
132 
133  // Delete notification (and its content)
134  delete l_pNot;
135  }
136  }
137  }
138 };
139 
140 std::string GetApplicationDir()
141 {
142 #ifdef _WIN32
143  wchar_t l_ExePath[MAX_PATH];
144  GetModuleFileName(NULL, l_ExePath, MAX_PATH); // Get executable path
145  std::wstring l_AppPath(l_ExePath);
146  std::string l_AppDir(l_AppPath.begin(), l_AppPath.end());
147  return l_AppDir.substr(0, l_AppDir.find_last_of('\\')); // Remove application name
148 #endif
149 }
150 
151 int main()
152 {
153  std::string l_AppDir = GetApplicationDir();
154 
155  CInstrumentCatalog l_Catalog;
156  CMFCManager l_MFCMan(l_Catalog, l_AppDir + "/../../../../InstrumentCatalog/MFC/ExternalFiles/DefaultCommandScript.scs", l_AppDir + "/../../../../InstrumentCatalog/MFC/ExternalFiles");
157  CMyApplication l_MyApp(l_Catalog);
158 
159 
160  // Synchronize catalog controls
161  l_MFCMan.SyncControls();
162  l_MyApp.SyncControls();
163  // Synchronize catalog observers
164  l_MFCMan.SyncObservers();
165  l_MyApp.SyncObservers();
166  // Subscribe to variables
167  l_MFCMan.Subscribe();
168  l_MyApp.Subscribe();
169  // Operate
170  l_MFCMan.Operate();
171 
172  l_MyApp.DoJob();
173 
174  // Terminate manager operation
175  l_MFCMan.Terminate();
176 
177  return 0;
178 }
main
int main()
Definition: MFCCatalogExample.cpp:151
CMyApplication::CMyApplication
CMyApplication(CInstrumentCatalog &rCat)
Definition: MFCCatalogExample.cpp:50
MTL::CInstrumentCatalog::l_Dump
static std::string l_Dump(const tInstrumentList &rInsList)
Definition: InstrumentCatalog.h:336
MTL::CGUICatalogInterface::oProbeArrayInformation
CObserver< CInstrumentCatalog::sProbeArrayInformation > oProbeArrayInformation
Definition: InstrumentCatalog.h:566
MTL::CInstrumentCatalog::tInstrumentList
std::vector< sInstrumentConnection > tInstrumentList
Definition: InstrumentCatalog.h:54
MTL::CInstrumentCatalog::sInstrumentState::ProbeSelection
sMeasureProbeSelection ProbeSelection
Definition: InstrumentCatalog.h:215
MTL::CInstrumentCatalog::sInstrumentState::RepeatMode
eRepeatMode RepeatMode
Definition: InstrumentCatalog.h:214
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification
Definition: CatalogInterface.h:180
CMyApplication
Definition: MFCCatalogExample.cpp:20
MTL::InstrumentCatalogInterface::CAsyncSnaphsotNotifier
Definition: CatalogInterface.h:299
MTL::CInstrumentCatalog::sInstrumentStatus
Definition: InstrumentCatalog.h:229
GetApplicationDir
std::string GetApplicationDir()
Definition: MFCCatalogExample.cpp:140
MTL::CGUICatalogInterface::cInstrumentState
CControl< CInstrumentCatalog::sInstrumentState > cInstrumentState
Definition: InstrumentCatalog.h:570
MTL::CMFCManager::SyncObservers
void SyncObservers()
Definition: MFCManager.cpp:820
MTL::CInstrumentCatalog::eProbeArrayType::kUnknown
@ kUnknown
MTL::CInstrumentCatalog::sSearchResult
Definition: InstrumentCatalog.h:250
MTL::CInstrumentCatalog::sProbeArrayInformation::FreqMin
F64 FreqMin
Definition: InstrumentCatalog.h:150
MTL::CGUICatalogInterface::oSearchResult
CObserver< CInstrumentCatalog::sSearchResult > oSearchResult
Definition: InstrumentCatalog.h:576
CMyApplication::SyncControls
void SyncControls()
Definition: MFCCatalogExample.cpp:54
CMyApplication::SyncObservers
void SyncObservers()
Definition: MFCCatalogExample.cpp:58
MTL::CInstrumentCatalog::sProbeArrayInformation::Type
eProbeArrayType Type
Definition: InstrumentCatalog.h:146
MTL::CMFCManager::Subscribe
void Subscribe()
Definition: MFCManager.cpp:832
MTL::CMFCManager::SyncControls
void SyncControls()
Definition: MFCManager.cpp:805
MFCManager.h
MTL::CInstrumentCatalog::sError
Definition: InstrumentCatalog.h:294
MTL::CInstrumentCatalog::sMeasureProbeSelection
Definition: InstrumentCatalog.h:197
MTL::CInstrumentCatalog::sInstrumentState
Definition: InstrumentCatalog.h:212
MTL::CGUICatalogInterface
Definition: InstrumentCatalog.h:551
MTL::CInstrumentCatalog::sInstrumentState::FreqMax
F64 FreqMax
Definition: InstrumentCatalog.h:217
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification::DataCopy
const void * DataCopy() const
Definition: CatalogInterface.h:195
MTL
Definition: CPT2026PeripheralROM.h:19
MTL::CGUICatalogInterface::cInstrumentCurrent
CControl< CInstrumentCatalog::sInstrumentConnection > cInstrumentCurrent
Definition: InstrumentCatalog.h:560
MTL::InstrumentCatalogInterface::CObserver::Subscribe
void Subscribe(CObserverNotifier_untyped &rNotifier)
Definition: CatalogInterface.h:95
MTL::CInstrumentCatalog::eRepeatMode::kSingle
@ kSingle
MTL::CInstrumentCatalog::sProbeArrayInformation
Definition: InstrumentCatalog.h:145
MTL::InstrumentCatalogInterface::CObserver_untyped
Definition: CatalogInterface.h:62
MTL::CGUICatalogInterface::oError
CObserver< CInstrumentCatalog::sError > oError
Definition: InstrumentCatalog.h:582
InstrumentCatalog.h
MTL::CMFCManager::Terminate
void Terminate()
Definition: MFCManager.cpp:878
MTL::InstrumentCatalogInterface::CAsyncNotification
Definition: CatalogInterface.h:144
MTL::CGUICatalogInterface::oInstrumentStatus
CObserver< CInstrumentCatalog::sInstrumentStatus > oInstrumentStatus
Definition: InstrumentCatalog.h:571
MTL::CInstrumentCatalog::sInstrumentState::NbAveragedMeasurements
U32 NbAveragedMeasurements
Definition: InstrumentCatalog.h:218
CMyApplication::DoJob
void DoJob()
Definition: MFCCatalogExample.cpp:72
MTL::CInstrumentCatalog::sInstrumentState::OperatingMode
eOperatingMode OperatingMode
Definition: InstrumentCatalog.h:213
MTL::CMFCManager::Operate
void Operate()
Definition: MFCManager.cpp:858
MTL::CMFCManager
Definition: MFCManager.h:39
MTL::CInstrumentCatalog::eOperatingMode::kSearch
@ kSearch
MTL::CInstrumentCatalog::sInstrumentState::FreqMin
F64 FreqMin
Definition: InstrumentCatalog.h:216
MTL::CInstrumentCatalog
Definition: InstrumentCatalog.h:22
MTL::InstrumentCatalogInterface::CObserver_untyped::Is
bool Is(const CObserver_untyped &rObs)
Definition: CatalogInterface.h:70
CMyApplication::Subscribe
void Subscribe()
Definition: MFCCatalogExample.cpp:62
MTL::CGUICatalogInterface::oInstrumentList
CObserver< CInstrumentCatalog::tInstrumentList > oInstrumentList
Definition: InstrumentCatalog.h:559
MTL::InstrumentCatalogInterface::CAsyncNotification::Observer
CObserver_untyped & Observer() const
Definition: CatalogInterface.h:162
MTL::CInstrumentCatalog::sProbeArrayInformation::FreqMax
F64 FreqMax
Definition: InstrumentCatalog.h:151