diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /game/client/initializer.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'game/client/initializer.h')
| -rw-r--r-- | game/client/initializer.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/game/client/initializer.h b/game/client/initializer.h new file mode 100644 index 0000000..949368c --- /dev/null +++ b/game/client/initializer.h @@ -0,0 +1,58 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// + +// Initializers are a way to register your object to be initialized at startup time. +// They're a good way to have global variables without worrying about dependent +// constructors being called. They also make it so init code doesn't depend on the +// global objects it's initializing. + +// To use initializers, just use REGISTER_INITIALIZER to register your global variable like this: +// class SomeClass {....} +// SomeClass *g_pSomeClassSingleton = NULL; +// REGISTER_INITIALIZER(SomeClass, &g_pSomeClassSingleton); + +#ifndef INITIALIZER_H +#define INITIALIZER_H + + +typedef void* (*CreateInitializerObjectFn)(); +typedef void (*DeleteInitializerObjectFn)(void *ptr); + +class Initializer +{ +public: + Initializer(void **pVar, CreateInitializerObjectFn createFn, DeleteInitializerObjectFn deleteFn); + + // Allocates all the global objects. + static bool InitializeAllObjects(); + + // Free all the global objects. + static void FreeAllObjects(); + + +private: + static Initializer *s_pInitializers; + + void **m_pVar; + CreateInitializerObjectFn m_CreateFn; + DeleteInitializerObjectFn m_DeleteFn; + Initializer *m_pNext; +}; + + +#define REGISTER_INITIALIZER(className, varPointer) \ + static void* __Initializer__Create##className##Fn() {return new className;} \ + static void* __Initializer__Delete##className##Fn(void *ptr) {delete (className*)ptr;} \ + static Initializer g_Initializer_##className##(varPointer, __Initializer__Create##className##Fn, __Initializer__Delete##className##Fn); + +#define REGISTER_FUNCTION_INITIALIZER(functionName) \ + static void* __Initializer__Create##functionName##Fn() { functionName(); return 0; } \ + static Initializer g_Initializer_##functionName##(0, __Initializer__Create##functionName##Fn, 0); + +#endif + |