Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cAppState.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cAppState_H
7 #define _INC_cAppState_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSingleton.h"
13 #include "cString.h"
14 #include "cObject.h"
15 #include "cArrayString.h"
16 #include "cFilePath.h"
17 #include "cThreadLocalSys.h"
18 #include "cThreadLock.h"
19 #include "cBits.h"
20 #include "cOSProcess.h"
21 #include "cUnitTestDecl.h"
22 
23 namespace Gray
24 {
25  UNITTEST2_PREDEF(cAppState);
26 
28  {
37  };
38 
39  typedef const FILECHAR_t* const* APP_ARGS_t;
40 
42  {
47 
48  private:
49  cStringF m_sArguments;
50  public:
52 
53  private:
54  void InitArgsInt(ITERATE_t argc, APP_ARGS_t ppszArgs);
55 
56  public:
57  static inline bool IsArgSwitch(wchar_t ch) noexcept
58  {
60  return ((ch) == '-' || (ch) == '/');
61  }
62 
63  cStringF get_ArgsStr() const noexcept;
64  ITERATE_t get_ArgsQty() const noexcept;
65  cStringF GetArgsEnum(ITERATE_t i) const;
66 
67  void InitArgsF(const FILECHAR_t* pszCommandArgs, const FILECHAR_t* pszSep = nullptr);
68  void InitArgs2(int argc, APP_ARGS_t argv);
69 
70  ITERATE_t FindCommandArg(const FILECHAR_t* pszCommandArg, bool bRegex = true, bool bIgnoreCase = true) const;
71  ITERATE_t _cdecl FindCommandArgs(bool bIgnoreCase, const FILECHAR_t* pszCommandArgFind, ...) const;
72 
73  bool HasCommandArg(const FILECHAR_t* pszCommandArg, bool bRegex = true, bool bIgnoreCase = true) const
74  {
76  ITERATE_t iRet = FindCommandArg(pszCommandArg, bRegex, bIgnoreCase);
77  return (iRet >= 0);
78  }
79  };
80 
81  class GRAYCORE_LINK cAppState : public cSingleton<cAppState>
82  {
90 
91  friend class cSingleton < cAppState >;
92  friend class cAppImpl;
93  friend class cAppStateMain;
94  friend class cUnitTestAppState;
95 
96  public:
101  static HMODULE sm_hInstance;
102 
103  protected:
104  THREADID_t m_nMainThreadId;
106 
110 
111  protected:
112  cAppState();
113  virtual ~cAppState();
114 
115  public:
116  static cStringF GRAYCALL get_AppFilePath();
117  static cStringF GRAYCALL get_AppFileTitle();
118  static cStringF GRAYCALL get_AppFileDir();
119 
120  static HRESULT inline CheckValidSignature(UINT32 nGrayCoreVer, size_t nSizeofThis)
121  {
127 
128  const cAppState* const pApp = get_SingleU();
129  if (!cMem::IsValidApp(pApp))
130  {
131  // Something is wrong. No idea.
132  return HRESULT_WIN32_C(ERROR_INTERNAL_ERROR);
133  }
134  if (nGrayCoreVer != _INC_GrayCore_H || !pApp->m_Sig.IsValidSignature(nGrayCoreVer, nSizeofThis))
135  {
136  // My *Core DLL is not the correct version or packing is incorrect!
137  return HRESULT_WIN32_C(ERROR_PRODUCT_VERSION);
138  }
139  return S_OK; // Things seem good.
140  }
141 
142  APPSTATE_TYPE_ get_AppState() const noexcept
143  {
145  return m_eAppState;
146  }
147  static APPSTATE_TYPE_ GRAYCALL GetAppState();
148  void put_AppState(APPSTATE_TYPE_ eAppState) noexcept
149  {
152  m_eAppState = eAppState;
153  }
154  void InitAppState() noexcept;
155 
156  THREADID_t get_MainThreadId() const noexcept
157  {
159  return m_nMainThreadId;
160  }
161 
162  static bool GRAYCALL isInCInit();
163  static bool GRAYCALL isAppRunning();
164  static bool GRAYCALL isAppStateRun();
165  static bool GRAYCALL isInCExit();
166 
167  static bool GRAYCALL isDebuggerPresent();
168  static bool GRAYCALL isRemoteSession();
169  static void GRAYCALL SetExecutionState(bool bActiveCPU, bool bActiveGUI);
170 
171  static bool GRAYCALL isCurrentUserAdmin();
172  static cString GRAYCALL GetCurrentUserName(bool bForce = false);
173  static cStringF GRAYCALL GetCurrentUserDir(const FILECHAR_t* pszSubFolder = nullptr, bool bCreate = true);
174 
175  static PROCESSID_t GRAYCALL get_CurrentProcessId()
176  {
178 #ifdef _WIN32
179  return ::GetCurrentProcessId();
180 #elif defined(__linux__)
181  return ::getpid();
182 #else
183 #error NOOS
184  return PROCESSID_BAD;
185 #endif
186  }
187 
188  static HMODULE GRAYCALL get_HModule();
189 
190  static UINT GRAYCALL get_LibVersion();
191 
192  static StrLen_t GRAYCALL GetEnvironStr(const FILECHAR_t* pszVarName, FILECHAR_t* pszValue, StrLen_t iLenMax) noexcept;
193  static cStringF GRAYCALL GetEnvironStr(const FILECHAR_t* pszVarName) noexcept;
194  static ITERATE_t GRAYCALL GetEnvironArray(cArrayString<FILECHAR_t>& a);
195  static bool GRAYCALL SetEnvironStr(const FILECHAR_t* pszVarName, const FILECHAR_t* pszVal);
196 
198  static StrLen_t GRAYCALL GetCurrentDir(FILECHAR_t* pszDir, StrLen_t iSizeMax);
199  static cStringF GRAYCALL get_CurrentDir();
200  static bool GRAYCALL SetCurrentDir(const FILECHAR_t* pszDir);
201 
202  // cStringF GetUserHomeDir();
203  // cStringF GetUserHomeAppDir();
204 
205  cStringF get_TempDir();
206  cStringF GetTempFile(const FILECHAR_t* pszFileTitle);
207  cStringF GetTempDir(const FILECHAR_t* pszFileDir, bool bCreate = true);
208 
209  void SetArgValid(ITERATE_t i);
210  cStringF get_InvalidArgs() const;
211 
212  void InitArgsF(const FILECHAR_t* pszCommandArgs);
213  void InitArgs2(int argc, APP_ARGS_t argv);
214 
215  static void GRAYCALL AbortApp(APP_EXITCODE_t uExitCode = APP_EXITCODE_ABORT);
216 
219  };
220 
222  {
227  public:
229 
230  public:
231 #if defined(_WIN32)
232  cAppStateMain(HINSTANCE hInstance, const FILECHAR_t* pszCommandArgs);
233 #endif
234  cAppStateMain(int argc, APP_ARGS_t argv);
235  ~cAppStateMain() noexcept
236  {
237  m_AppState.put_AppState(APPSTATE_Exit); // destructors should be called next.
238  }
239  };
240 
242  {
247  public:
249  {
250  cAppState& I = cAppState::I();
253  }
255  {
256  cAppState& I = cAppState::I();
259  }
260  };
261 
262  class GRAYCORE_LINK cAppExitCatcher : public cSingletonStatic < cAppExitCatcher >
263  {
267  private:
268  static void __cdecl ExitCatchProc();
269  protected:
270  virtual void ExitCatch();
271  public:
272  cAppExitCatcher();
273  ~cAppExitCatcher();
274  };
275 }
276 #endif // _INC_cAppState_H
#define _INC_GrayCore_H
0.0.3 Version stamp the API. Especially important to the Variant and Archive types.
Definition: GrayCore.h:7
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#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: cAppState.h:42
cArrayString< FILECHAR_t > m_asArgs
Array of parsed m_sArguments. [0]=appname.exe, [1]=first arg. NOT nullptr terminated like APP_ARGS_t....
Definition: cAppState.h:51
static bool IsArgSwitch(wchar_t ch) noexcept
Definition: cAppState.h:57
Definition: cAppState.h:263
Definition: cAppImpl.h:21
Definition: cAppState.h:222
cAppState & m_AppState
Definition: cAppState.h:228
~cAppStateMain() noexcept
Definition: cAppState.h:235
Definition: cAppState.h:242
cAppStateModuleLoad()
Definition: cAppState.h:248
~cAppStateModuleLoad()
Definition: cAppState.h:254
Definition: cAppState.h:82
THREADID_t m_nMainThreadId
The thread we started with. main().
Definition: cAppState.h:104
APPSTATE_TYPE_ get_AppState() const noexcept
Definition: cAppState.h:142
APPSTATE_TYPE_ m_eAppState
The main state of the application. use isInCInit() for loading DLL's.
Definition: cAppState.h:105
static PROCESSID_t __stdcall get_CurrentProcessId()
Definition: cAppState.h:175
void put_AppState(APPSTATE_TYPE_ eAppState) noexcept
Definition: cAppState.h:148
const cObjectSignature m_Sig
Used to check for compatible build/compile config and alignments. (_INC_GrayCore_H,...
Definition: cAppState.h:97
bool m_bTempDirWritable
I have test written to the temp directory. its good.
Definition: cAppState.h:109
static HRESULT CheckValidSignature(UINT32 nGrayCoreVer, size_t nSizeofThis)
Definition: cAppState.h:120
cAppArgs m_Args
Application Command line arguments. [0] = app name.
Definition: cAppState.h:99
cStringF m_sTempDir
Cache my temporary files directory path.
Definition: cAppState.h:108
cString m_sUserName
Applications assigned login/user name. Cache/Read this just once.
Definition: cAppState.h:107
cBitmask m_ArgsValid
Track which command line args are valid/used in m_Args. assume any left over are not.
Definition: cAppState.h:100
static HMODULE sm_hInstance
the current applications HINSTANCE handle/base address. _IMAGE_DOS_HEADER, HMODULE=HINSTANCE
Definition: cAppState.h:101
cThreadLocalSysT< bool > m_ThreadModuleLoading
any thread is currently loading a DLL/SO? isInCInit(). use cAppStateModuleLoad
Definition: cAppState.h:98
Definition: cBits.h:312
Definition: cObject.h:22
bool IsValidSignature(UINT32 nVer, size_t nSizeofThis) const
Definition: cObject.h:40
Definition: cSingleton.h:30
Definition: cSingleton.h:127
static cAppState &__stdcall I()
Definition: cSingleton.h:199
TYPE GetData() const noexcept
Definition: cThreadLocalSys.h:167
bool PutData(TYPE nData) noexcept
Definition: cThreadLocalSys.h:171
Definition: cUnitTest.h:123
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
constexpr PROCESSID_t PROCESSID_BAD
Invalid process id.
Definition: cOSProcess.h:46
APPSTATE_TYPE_
Definition: cAppState.h:28
@ APPSTATE_RunInit
not static init but still init. In main() but not main loop yet. InitInstance()
Definition: cAppState.h:32
@ APPSTATE_QTY
Definition: cAppState.h:36
@ APPSTATE_Exit
static classes are being cleaned up. destructors called.
Definition: cAppState.h:35
@ APPSTATE_Run
we are in main() main loop. Run() and OnTickApp()
Definition: cAppState.h:33
@ APPSTATE_Init
static class init time. constructors called for static stuff. maybe set for single thread loading DLL...
Definition: cAppState.h:31
@ APPSTATE_RunExit
classes are being cleaned up. destructors called. Trying to exit. ExitInstance().
Definition: cAppState.h:34
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
const FILECHAR_t *const * APP_ARGS_t
the args passed to main() nullptr terminated array.
Definition: cAppState.h:39
@ APP_EXITCODE_ABORT
3=Default error returned if "abort()" used (arbitrary?) App closed.
Definition: cOSProcess.h:56
static bool IsValidApp(const void *pData) noexcept
Definition: cMem.h:42