Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cAppConsole.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cAppConsole_H
7 #define _INC_cAppConsole_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSingleton.h"
13 #include "cStream.h"
14 #include "StrArg.h"
15 #include "cUnitTestDecl.h"
16 
17 #if ! defined(UNDER_CE)
18 
19 namespace Gray
20 {
22 
24  {
31  };
33  {
36 
42  };
43 
45  : public cSingleton < cAppConsole >
46  , public cStreamOutput
47  {
51 
52  friend class cSingleton < cAppConsole >;
53 
54  public:
55  static const COUNT_t k_MAX_CONSOLE_LINES = 500;
56 
57 #if defined(_WIN32)
58  HANDLE m_hStd[AppStd_QTY];
59 #elif defined(__linux__)
60  // __iob_func
61 #endif
62 
63  private:
64  bool m_bKeyEchoMode;
65  bool m_bKeyEnterMode;
66 
67  AppCon_TYPE m_eConsoleType;
68  bool m_bConsoleParent;
69  int m_iAllocConsoleCount;
70 
71  mutable cThreadLockCount m_Lock;
72 
73  protected:
74  cAppConsole();
75  void CheckConsoleMode() noexcept;
76  bool AttachConsoleSync();
77 
78  public:
79  virtual ~cAppConsole();
80 
81  bool HasConsoleParent() noexcept
82  {
84  CheckConsoleMode();
85  return m_bConsoleParent;
86  }
88  {
89  CheckConsoleMode();
90  return m_eConsoleType;
91  }
92  bool isConsoleMode() noexcept
93  {
96  return get_ConsoleMode() != AppCon_NONE;
97  }
98 
100  bool AttachOrAllocConsole(bool bAttachElseAlloc = true);
101  void ReleaseConsole();
102 
103  HRESULT WriteStrErr(const char* pszText);
104  HRESULT WriteStrOut(const char* pszText);
105 
106  HRESULT SetKeyModes(bool bEchoMode = true, bool bEnterMode = true);
107  int get_KeyReadQty() const;
108  int ReadKeyWait();
109  int ReadKey();
110 
111  HRESULT WriteString(const char* pszStr) override
112  {
115  const HRESULT hRes = WriteStrOut(pszStr);
116  if (FAILED(hRes))
117  return hRes;
118  return 1;
119  }
120  HRESULT WriteString(const wchar_t* pszStr) override
121  {
124  const HRESULT hRes = WriteStrOut(StrArg<char>(pszStr));
125  if (FAILED(hRes))
126  return HRESULT_WIN32_C(ERROR_WRITE_FAULT);
127  return 1;
128  }
129 
132  };
133 }
134 #endif // UNDER_CE
135 #endif // _INC_cAppConsole_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cAppConsole.h:47
HRESULT WriteString(const wchar_t *pszStr) override
Definition: cAppConsole.h:120
AppCon_TYPE get_ConsoleMode() noexcept
Definition: cAppConsole.h:87
HRESULT WriteString(const char *pszStr) override
Definition: cAppConsole.h:111
bool isConsoleMode() noexcept
Definition: cAppConsole.h:92
Definition: cSingleton.h:127
Definition: cStream.h:126
Definition: cThreadLock.h:498
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
AppStd_TYPE
Definition: cAppConsole.h:24
@ AppStd_QTY
Definition: cAppConsole.h:30
@ AppStd_stdin
stdin (&__iob_func()[0]) -> GetStdHandle(STD_INPUT_HANDLE) = STDIN_FILENO
Definition: cAppConsole.h:27
@ AppStd_stdout
stdout (&__iob_func()[1]) -> GetStdHandle(STD_OUTPUT_HANDLE) = STDOUT_FILENO
Definition: cAppConsole.h:28
@ AppStd_stderr
stderr (&__iob_func()[2]) -> GetStdHandle(STD_ERROR_HANDLE) = STDERR_FILENO
Definition: cAppConsole.h:29
size_t COUNT_t
like size_t but a count of things that might NOT be bytes. ASSUME unsigned. _countof(x)
Definition: Index.h:32
AppCon_TYPE
Definition: cAppConsole.h:33
@ AppCon_NONE
Definition: cAppConsole.h:38
@ AppCon_Proc
Process was build as _CONSOLE mode. stdin,stdout already setup.
Definition: cAppConsole.h:39
@ AppCon_Attach
Attached to parent console. must call FreeConsole()
Definition: cAppConsole.h:40
@ AppCon_UNKNOWN
Definition: cAppConsole.h:37
@ AppCon_Create
Created my own console. must call FreeConsole()
Definition: cAppConsole.h:41
const char *__stdcall StrArg< char >(const char *pszStr) noexcept
Definition: StrArg.h:63