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.

65 lines
1.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "initializer.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. Initializer *Initializer::s_pInitializers = 0;
  13. Initializer::Initializer(void **pVar, CreateInitializerObjectFn createFn, DeleteInitializerObjectFn deleteFn)
  14. {
  15. m_pVar = pVar;
  16. m_CreateFn = createFn;
  17. m_DeleteFn = deleteFn;
  18. m_pNext = s_pInitializers;
  19. s_pInitializers = this;
  20. }
  21. bool Initializer::InitializeAllObjects()
  22. {
  23. for(Initializer *pCur=s_pInitializers; pCur; pCur=pCur->m_pNext)
  24. {
  25. if(void *ptr = pCur->m_CreateFn())
  26. {
  27. *pCur->m_pVar = ptr;
  28. }
  29. else
  30. {
  31. // Don't worry if we're not actually trying to initialize a global
  32. if (pCur->m_pVar)
  33. {
  34. FreeAllObjects();
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. void Initializer::FreeAllObjects()
  42. {
  43. for(Initializer *pCur=s_pInitializers; pCur; pCur=pCur->m_pNext)
  44. {
  45. if (pCur->m_pVar)
  46. {
  47. pCur->m_DeleteFn(*pCur->m_pVar);
  48. *pCur->m_pVar = 0;
  49. }
  50. }
  51. }