CLI  2.9
exec_context.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
31 
32 #ifndef _CLI_EXEC_CONTEXT_H_
33 #define _CLI_EXEC_CONTEXT_H_
34 
35 #include "cli/namespace.h"
36 #include "cli/io_device.h"
37 #include "cli/tk.h"
38 
39 
41 
42  // Forward declarations.
44  class ExecutionResult;
45 
46 
49  typedef enum _STREAM_TYPE
50  {
51  ALL_STREAMS = -1,
52 
59  } STREAM_TYPE;
60 
61 
149  class ExecutionContext : public Object
150  {
151  protected:
153  explicit ExecutionContext(void);
154 
158  explicit ExecutionContext(
159  ExecutionContext& CLI_ParentContext
160  );
161 
162  public:
164  virtual ~ExecutionContext(void);
165 
166  private:
168  ExecutionContext& operator=(const ExecutionContext&);
169 
170  public:
174 
175  public:
178  const IODevice& GetInput(void) const;
179 
182  const OutputDevice& GetStream(
183  const STREAM_TYPE E_StreamType
184  ) const;
185 
194  const bool SetStream(
195  const STREAM_TYPE E_StreamType,
196  OutputDevice& CLI_Stream
197  );
198 
201  const bool StreamEnabled(
202  const STREAM_TYPE E_StreamType
203  ) const;
204 
207  const bool EnableStream(
208  const STREAM_TYPE E_StreamType,
209  const bool B_Enable
210  );
211 
212  public:
214  void SetLang(
215  const ResourceString::LANG E_Lang
216  );
217 
220  const ResourceString::LANG GetLang(void) const;
221 
222  public:
224  void SetBeep(
225  const bool B_Enable
226  );
227 
230  const bool GetBeep(void) const;
231 
233  void Beep(void);
234 
235  public:
239  void Run(
240  IODevice& CLI_IODevice
241  );
242 
246  void Run(void);
247 
250  const bool IsRunning(void) const;
251 
252  public:
255  void StopExecution(void);
256 
259  void StopAllExecutions(void);
260 
261  protected:
264  virtual const bool OnStartExecution(void) = 0;
265 
266  // Note: use of @param doxygen tag in order to avoid doxygen warnings for reimplementations in sub-classes.
269  virtual void OnKey(const KEY E_KeyCode) = 0;
270 
273  virtual const bool OnStopExecution(void) = 0;
274 
275  private:
278  const bool BeginExecution(
279  IODevice& CLI_IODevice
280  );
281 
283  void MainBlockingLoop(void);
284 
286  void ProcessKey(
287  const KEY E_KeyCode
288  );
289 
292  const bool FinishExecution(void);
293 
294  private:
296  ExecutionContextManager& m_cliManager;
297 
299  tk::Queue<ExecutionResult*> m_tkResults;
300 
301  private:
302  // ExecutionContext and NonBlockingIODevice are friends so that they can interact with non-public entry points.
303  friend class NonBlockingIODevice;
304 
305  // ExecutionContext and ExecutionResult as well.
306  friend class ExecutionResult;
307  };
308 
309 
313  class ExecutionResult : public Object
314  {
315  protected:
317  explicit ExecutionResult(void);
318 
319  public:
321  virtual ~ExecutionResult(void) = 0;
322 
323  private:
327  ExecutionResult& operator=(const ExecutionResult&);
328 
329  public:
332  void WatchResult(
333  ExecutionContext& CLI_Context
334  );
335 
336  protected:
338  virtual void OnResult(
339  const ExecutionContext& CLI_Context
340  ) = 0;
341 
342  private:
343  friend class ExecutionContext;
344  };
345 
347 
348 #endif // _CLI_EXEC_CONTEXT_H_
349 
Generic object.
Definition: object.h:42
virtual void OnResult(const ExecutionContext &CLI_Context)=0
User interface result handler.
ExecutionResult(void)
Default constructor.
Main namespace of the CLI library.
void Run(void)
Runs the execution context as a child context of the parent given by the construction.
Echo stream.
Definition: exec_context.h:55
Error stream.
Definition: exec_context.h:57
Non-blocking input device.
void SetBeep(const bool B_Enable)
Beep configuration setting.
virtual const bool OnStartExecution(void)=0
Beginning of execution handler.
const ResourceString::LANG GetLang(void) const
Language access.
Execution context manager class.
Number of streams.
Definition: exec_context.h:58
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
LANG
Languages identifiers.
const IODevice & GetInput(void) const
Input stream accessor.
const bool EnableStream(const STREAM_TYPE E_StreamType, const bool B_Enable)
Enable/disable stream.
const bool StreamEnabled(const STREAM_TYPE E_StreamType) const
Enabled/disabled stream accessor.
Output stream.
Definition: exec_context.h:56
Result interface for execution contexts.
Definition: exec_context.h:313
void StopAllExecutions(void)
Terminates execution for all execution contexts attached to the same top execution context...
ExecutionContextManager & GetContextManager(void)
Execution context manager accessor.
virtual ~ExecutionResult(void)=0
Pure virtual destructor.
Namespace management.
const bool GetBeep(void) const
Beep configuration access.
Execution context.
Definition: exec_context.h:149
void WatchResult(ExecutionContext &CLI_Context)
Start watching the execution context for a result.
void Beep(void)
Sends a beep signal.
Generic output device.
Definition: io_device.h:256
virtual ~ExecutionContext(void)
Destructor.
ExecutionContext(void)
Top context constructor.
All streams.
Definition: exec_context.h:51
enum _STREAM_TYPE STREAM_TYPE
Output stream enumeration.
virtual const bool OnStopExecution(void)=0
Execution termination handler.
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
Welcome stream. Useful for bye too.
Definition: exec_context.h:53
OutputDevice, IODevice and IOEndl classes defintion.
Prompt stream.
Definition: exec_context.h:54
_STREAM_TYPE
Output stream enumeration.
Definition: exec_context.h:49
CLI toolkit definition.
const bool SetStream(const STREAM_TYPE E_StreamType, OutputDevice &CLI_Stream)
Output stream positionning.
void StopExecution(void)
Terminates this execution context&#39;s execution.
Generic input/output device.
Definition: io_device.h:581
virtual void OnKey(const KEY E_KeyCode)=0
Handler called on character input.
const OutputDevice & GetStream(const STREAM_TYPE E_StreamType) const
Output stream accessor.
void SetLang(const ResourceString::LANG E_Lang)
Language setting.
const bool IsRunning(void) const
Tells whether this execution context is running or not.