nux-1.14.0
GlobalInitializer.h
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef NGLOBALINITIALIZER_H
00024 #define NGLOBALINITIALIZER_H
00025 
00026 #include "Macros.h"
00027 #include "System.h"
00028 
00029 #ifdef _WIN32
00030 #define NUX_GLOBAL_OBJECT_INIT_SEQUENCE()                       \
00031         NUX_GLOBAL_OBJECT_VARIABLE(NGlobalData);                \
00032         NUX_GLOBAL_OBJECT_VARIABLE(NCPU);                       \
00033         NUX_GLOBAL_OBJECT_VARIABLE(NProcess);                   \
00034         NUX_GLOBAL_OBJECT_VARIABLE(NullOutput);                \
00035         NUX_GLOBAL_OBJECT_VARIABLE(UniqueIndex);                \
00036         NUX_GLOBAL_OBJECT_VARIABLE(NFileManagerWindows);        \
00037         NUX_GLOBAL_OBJECT_VARIABLE(VisualOutputConsole);        \
00038         NUX_GLOBAL_OBJECT_VARIABLE(PrintfOutputConsole);        \
00039         NUX_GLOBAL_OBJECT_VARIABLE(LogFileOutput);             \
00040         NUX_GLOBAL_OBJECT_VARIABLE(LogOutputRedirector);        \
00041         NUX_GLOBAL_OBJECT_VARIABLE(ObjectStats);
00042 
00043 //NUX_GLOBAL_OBJECT_VARIABLE(NDefaultMemoryAllocator);
00044 //NUX_GLOBAL_OBJECT_VARIABLE(MemHook);
00045 
00046 #elif (defined NUX_OS_LINUX)
00047 #define NUX_GLOBAL_OBJECT_INIT_SEQUENCE()                       \
00048         NUX_GLOBAL_OBJECT_VARIABLE(NGlobalData);                \
00049         NUX_GLOBAL_OBJECT_VARIABLE(NullOutput);                \
00050         NUX_GLOBAL_OBJECT_VARIABLE(UniqueIndex);                \
00051         NUX_GLOBAL_OBJECT_VARIABLE(NFileManagerGNU);            \
00052         NUX_GLOBAL_OBJECT_VARIABLE(PrintfOutputConsole);        \
00053         NUX_GLOBAL_OBJECT_VARIABLE(LogFileOutput);             \
00054         NUX_GLOBAL_OBJECT_VARIABLE(LogOutputRedirector);        \
00055         NUX_GLOBAL_OBJECT_VARIABLE(ObjectStats);
00056 
00057 //NUX_GLOBAL_OBJECT_VARIABLE(NDefaultMemoryAllocator);
00058 //NUX_GLOBAL_OBJECT_VARIABLE(MemHook);
00059 #endif
00060 
00061 namespace nux
00062 {
00063 
00064 // This class initialize all inalogic singleton (global objects) in order. It also initialize memory allocators.
00065 // Therefore, do not call new GlobalSingletonInitializer as it will try to call inalogic memory allocator and fail.
00066 // You may use the global placement new operator(it is not overwritten by inalogic) to create GlobalSingletonInitializer
00067 // inside the application data space by calling SystemInitializer(). At shutdown, call SystemShutdown()
00068 //
00069 // You may also create GlobalSingletonInitializer in this way:
00070 //      main()
00071 //      {
00072 //          GlobalSingletonInitializer GlobalInitializer;
00073 //      }
00074 //
00075 //
00076 
00077   class GlobalSingletonInitializer
00078   {
00079     NUX_DISABLE_OBJECT_COPY (GlobalSingletonInitializer);
00080     GlobalSingletonInitializer *operator & ();
00081     const GlobalSingletonInitializer *operator & () const;
00082 
00083   public:
00084     GlobalSingletonInitializer();
00085     ~GlobalSingletonInitializer();
00086 
00087     static bool Ready();
00088   private:
00089     static bool m_bGlobalObjectsReady;
00090 
00091     NUX_GLOBAL_OBJECT_INIT_SEQUENCE();
00092   };
00093 
00094 // Hide these functions
00095 // extern NUX_DLL_ENTRY void SystemStart();
00096 // extern NUX_DLL_ENTRY void SystemShutdown();
00097 
00098 
00099 
00100 // Nifty Counter idiom. See http://www-d0.fnal.gov/KAI/doc/tutorials/static_initialization.html
00101   class GlobalInitializer
00102   {
00103   public:
00104     GlobalInitializer();
00105     ~GlobalInitializer();
00106 
00107     static void ForceShutdown();
00108   private:
00109     static int m_Count;
00110   };
00111 
00112 // Every compilation unit that includes this file will have its own instance of sGlobalInitializer. sGlobalInitializer is initialized
00113 // before the main function of the program is called. The first time sGlobalInitializer is initialized, it calls SystemStart() to create
00114 // our global object singleton. In SystemStart() we have a change to create our singletons in any order we like.
00115 // When the program exits, every instance of sGlobalInitializer will be destroyed. The last instance destroyed will call SystemShutdown().
00116 // In SystemShutdown() we can destroy our global objects in any order we like.
00117 
00118   static GlobalInitializer sGlobalInitializer;
00119 
00120 
00121 }
00122 
00123 #endif // NGLOBALINITIALIZER_H
00124 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends