Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // Initializers are a way to register your object to be initialized at startup time.
  9. // They're a good way to have global variables without worrying about dependent
  10. // constructors being called. They also make it so init code doesn't depend on the
  11. // global objects it's initializing.
  12. // To use initializers, just use REGISTER_INITIALIZER to register your global variable like this:
  13. // class SomeClass {....}
  14. // SomeClass *g_pSomeClassSingleton = NULL;
  15. // REGISTER_INITIALIZER(SomeClass, &g_pSomeClassSingleton);
  16. #ifndef INITIALIZER_H
  17. #define INITIALIZER_H
  18. typedef void* (*CreateInitializerObjectFn)();
  19. typedef void (*DeleteInitializerObjectFn)(void *ptr);
  20. class Initializer
  21. {
  22. public:
  23. Initializer(void **pVar, CreateInitializerObjectFn createFn, DeleteInitializerObjectFn deleteFn);
  24. // Allocates all the global objects.
  25. static bool InitializeAllObjects();
  26. // Free all the global objects.
  27. static void FreeAllObjects();
  28. private:
  29. static Initializer *s_pInitializers;
  30. void **m_pVar;
  31. CreateInitializerObjectFn m_CreateFn;
  32. DeleteInitializerObjectFn m_DeleteFn;
  33. Initializer *m_pNext;
  34. };
  35. #define REGISTER_INITIALIZER(className, varPointer) \
  36. static void* __Initializer__Create##className##Fn() {return new className;} \
  37. static void* __Initializer__Delete##className##Fn(void *ptr) {delete (className*)ptr;} \
  38. static Initializer g_Initializer_##className##(varPointer, __Initializer__Create##className##Fn, __Initializer__Delete##className##Fn);
  39. #define REGISTER_FUNCTION_INITIALIZER(functionName) \
  40. static void* __Initializer__Create##functionName##Fn() { functionName(); return 0; } \
  41. static Initializer g_Initializer_##functionName##(0, __Initializer__Create##functionName##Fn, 0);
  42. #endif