ADTF
ucom3/include/adtfucom3/test_runtime.h
Go to the documentation of this file.
1 
7 #ifndef _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
8 #define _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
9 
10 namespace adtf
11 {
12 namespace ucom
13 {
14 namespace testing
15 {
16 namespace ant
17 {
21 typedef enum : int8_t
22 {
23  RL_Shutdown = IRuntime::RL_Shutdown,
24  RL_System = 1,
25  RL_Application = 2,
26  RL_Running = 3
27 } tTestRL;
28 
32 class cTestRuntime : public adtf::ucom::ant::runtime<tTestRL::RL_Running>
33 {
36 
37  public:
40  {
41  }
42 };
43 
50 const adtf_util::cFilename find_plugin(const adtf::util::cString& strPluginName);
51 
61 const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName, int8_t ui8Level, ucom::ant::IRuntime& oRuntime);
62 
70 const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName, int8_t ui8Level);
71 
78 const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName);
89 void load_service(const adtf_util::cString& strPluginName, const adtf_util::cString& strClassId,
90  const adtf_util::cString& strObjectId,
91  int8_t nPluginLevel,
92  int8_t nServiceLevel);
93 
103 void load_service(const adtf_util::cString& strPluginName, const adtf_util::cString& strClassId,
104  const adtf_util::cString& strObjectId,
105  int8_t nServiceLevel);
106 
111 void clean_up();
112 
117 {
118  private:
119  std::thread m_oThread;
120  std::mutex m_oActionMutex;
121  typedef std::function<void()> action_type;
122  action_type m_fnAction;
123  bool m_bQuitThread = false;
124  std::condition_variable m_oNewAction;
125 
126  public:
128  {
129  m_oThread = std::thread([&]
130  {
131  std::unique_lock<std::mutex> oLocker(m_oActionMutex);
132  for (;;)
133  {
134  m_oNewAction.wait(oLocker, [&]
135  {
136  return m_fnAction || m_bQuitThread;
137  });
138 
139  if (m_bQuitThread)
140  {
141  return;
142  }
143 
144  m_fnAction();
145  m_fnAction = action_type();
146  }
147  });
148  }
149 
150  ~cExecutorThread()
151  {
152  {
153  std::lock_guard<std::mutex> oLocker(m_oActionMutex);
154  m_bQuitThread = true;
155  }
156 
157  m_oNewAction.notify_one();
158  m_oThread.join();
159  }
160 
161  tResult Execute(std::function<tResult()> fnAction)
162  {
163  std::promise<tResult> oPromisedResult;
164  auto oResult = oPromisedResult.get_future();
165 
166  {
167  std::lock_guard<std::mutex> oLocker(m_oActionMutex);
168  m_fnAction = [&]
169  {
170  oPromisedResult.set_value(fnAction());
171  };
172  }
173 
174  m_oNewAction.notify_one();
175  return oResult.get();
176  }
177 };
178 
183 template<typename RuntimeClass>
185 {
186  private:
187  std::unique_ptr<RuntimeClass> m_pRuntime;
188 
189  public:
190  exec_forward():
191  m_pRuntime(new RuntimeClass)
192  {
193  _runtime = m_pRuntime.get();
194  }
195 
196  ~exec_forward()
197  {
198  m_pRuntime->SetRunLevel(adtf::ucom::ant::IRuntime::RL_Shutdown, true);
199  _runtime = nullptr;
200  }
201 
202  tResult Exec(std::function<void()> fnStartup)
203  {
204  return m_pRuntime->Exec(adtf::ucom::ant::IRuntime::RL_Shutdown + 1,
205  fnStartup);
206  }
207 };
208 
213 template<typename ExecuteForward>
215 {
216  private:
217  std::unique_ptr<ExecuteForward> m_pExecuteForward;
218  std::thread m_oExecWaitThread;
219 
220  public:
222  {
223  if (m_oExecWaitThread.joinable())
224  {
225  m_oExecWaitThread.join();
226  }
227  }
228 
229  tResult Create()
230  {
231  return Execute([&]() -> tResult
232  {
233  m_pExecuteForward.reset(new ExecuteForward());
235  });
236  }
237 
238  tResult Start()
239  {
240  if (m_oExecWaitThread.joinable())
241  {
242  RETURN_ERROR(ERR_INVALID_STATE);
243  }
244 
245  std::promise<tResult> oExecLoopStarted;
246  auto oStarted = oExecLoopStarted.get_future();
247  m_oExecWaitThread = std::thread([&]
248  {
249  Execute([&]() -> tResult
250  {
251  auto nExecResult = m_pExecuteForward->Exec([&]
252  {
253  oExecLoopStarted.set_value(ERR_NOERROR);
254  });
255 
256  if (IS_FAILED(nExecResult))
257  {
258  oExecLoopStarted.set_value(nExecResult);
259  }
261  });
262  });
263 
264  return oStarted.get();
265  }
266 };
267 
268 
269 }//ns ant
270 
271 using ant::tTestRL;
272 using ant::cTestRuntime;
273 
274 using ant::find_plugin;
275 using ant::load_plugin;
276 using ant::load_service;
277 using ant::clean_up;
278 
279 using ant::exec_forward;
281 
282 }//ns testing
283 }//ns ucom
284 }//ns adtf
285 
286 #endif // _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
The IRuntime interface controls global system startup and shutdown.
Definition: runtime_intf.h:111
@ RL_Shutdown
The system is shut down.
Definition: runtime_intf.h:124
Definition of a common level based runtime.
Definition: runtime.h:202
This is a helper class that executes calls in a thread.
A testing runtime definition for own applications.
adtf::ucom::ant::runtime< tTestRL::RL_Running > base_type
base type for the testing runtime
This template makes will forward the runtime_executor function calls to the RuntimeClass.
This template makes sure to create and execute an instance of a runtime in a separate thread.
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2784
void load_service(const adtf_util::cString &strPluginName, const adtf_util::cString &strClassId, const adtf_util::cString &strObjectId, int8_t nPluginLevel, int8_t nServiceLevel)
load plugin and create a service.
const adtf_util::cFilename find_plugin(const adtf::util::cString &strPluginName)
find plugin helper.
tTestRL
A testing runtime runlevel definition for own applications.
void clean_up()
cleans up the current _runtime returns void.
const adtf_util::cFilename load_plugin(const adtf::util::cString &strPluginName, int8_t ui8Level, ucom::ant::IRuntime &oRuntime)
load plugin helper.
Namespace for entire ADTF SDK.
adtf::ucom::IRuntime * _runtime
Global Runtime Pointer to reference to the current runtime.