Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vshutdownregistry_h 00009 #define vshutdownregistry_h 00010 00011 #include "vtypes.h" 00012 00024 class IVShutdownHandler { 00025 public: 00026 00027 protected: 00028 00029 IVShutdownHandler(bool deleteHandlerAfterShutdown = true) : 00030 mDeleteAfterShutdown(deleteHandlerAfterShutdown) {} 00031 00032 virtual ~IVShutdownHandler() {} 00033 00040 virtual void _shutdown() = 0; 00041 00042 private: 00043 00044 bool mDeleteAfterShutdown; 00045 00046 friend class VShutdownRegistry; // give it access to _shutdown() and mDeleteAfterShutdown 00047 }; 00048 00050 typedef std::vector<IVShutdownHandler*> ShutdownHandlerList; 00051 00054 typedef void (*shutdownFunction)(); 00056 typedef std::vector<shutdownFunction> ShutdownFunctionList; 00057 00067 class VShutdownRegistry { 00068 public: 00069 00075 static VShutdownRegistry* instance(); 00076 00083 static void shutdown(); 00084 00090 void registerHandler(IVShutdownHandler* handler); 00091 00097 void registerFunction(shutdownFunction func); 00098 00099 private: 00100 00101 // Declare our constructor and destructor private so that they 00102 // can only be called via our static functions (which are thread-safe). 00103 VShutdownRegistry() : mHandlers(), mFunctions() {} 00104 ~VShutdownRegistry(); 00105 00106 static VShutdownRegistry* gInstance; 00107 00108 ShutdownHandlerList mHandlers; 00109 ShutdownFunctionList mFunctions; 00110 }; 00111 00138 template <class T> 00139 class VSingletonShutdownHandler : public IVShutdownHandler { 00140 public: 00141 00148 VSingletonShutdownHandler(bool deleteHandlerAfterShutdown = true) 00149 : IVShutdownHandler(deleteHandlerAfterShutdown) 00150 { 00151 VShutdownRegistry::instance()->registerHandler(this); 00152 } 00153 00154 virtual ~VSingletonShutdownHandler() {} 00155 00156 protected: 00157 00162 virtual void _shutdown() { T::deleteInstance(); } 00163 }; 00164 00165 #endif /* vshutdownregistry_h */