nux-0.9.46

NuxCore/GlobalInitializer.h

Go to the documentation of this file.
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 #ifdef _WIN32
00027 #define NUX_GLOBAL_OBJECT_INIT_SEQUENCE()                       \
00028         NUX_GLOBAL_OBJECT_VARIABLE(NGlobalData);                \
00029         NUX_GLOBAL_OBJECT_VARIABLE(NCPU);                       \
00030         NUX_GLOBAL_OBJECT_VARIABLE(NProcess);                   \
00031         NUX_GLOBAL_OBJECT_VARIABLE(NullOutput);                \
00032         NUX_GLOBAL_OBJECT_VARIABLE(UniqueIndex);                \
00033         NUX_GLOBAL_OBJECT_VARIABLE(NFileManagerWindows);        \
00034         NUX_GLOBAL_OBJECT_VARIABLE(VisualOutputConsole);        \
00035         NUX_GLOBAL_OBJECT_VARIABLE(PrintfOutputConsole);        \
00036         NUX_GLOBAL_OBJECT_VARIABLE(LogFileOutput);             \
00037         NUX_GLOBAL_OBJECT_VARIABLE(LogOutputRedirector);        \
00038         NUX_GLOBAL_OBJECT_VARIABLE(ObjectStats);
00039 
00040 //NUX_GLOBAL_OBJECT_VARIABLE(NDefaultMemoryAllocator);
00041 //NUX_GLOBAL_OBJECT_VARIABLE(MemHook);
00042 
00043 #elif (defined NUX_OS_LINUX)
00044 #define NUX_GLOBAL_OBJECT_INIT_SEQUENCE()                       \
00045         NUX_GLOBAL_OBJECT_VARIABLE(NGlobalData);                \
00046         NUX_GLOBAL_OBJECT_VARIABLE(NullOutput);                \
00047         NUX_GLOBAL_OBJECT_VARIABLE(UniqueIndex);                \
00048         NUX_GLOBAL_OBJECT_VARIABLE(NFileManagerGNU);            \
00049         NUX_GLOBAL_OBJECT_VARIABLE(PrintfOutputConsole);        \
00050         NUX_GLOBAL_OBJECT_VARIABLE(LogFileOutput);             \
00051         NUX_GLOBAL_OBJECT_VARIABLE(LogOutputRedirector);        \
00052         NUX_GLOBAL_OBJECT_VARIABLE(ObjectStats);
00053 
00054 //NUX_GLOBAL_OBJECT_VARIABLE(NDefaultMemoryAllocator);
00055 //NUX_GLOBAL_OBJECT_VARIABLE(MemHook);
00056 #endif
00057 
00058 namespace nux
00059 {
00060 
00061 // This class initialize all inalogic singleton (global objects) in order. It also initialize memory allocators.
00062 // Therefore, do not call new GlobalSingletonInitializer as it will try to call inalogic memory allocator and fail.
00063 // You may use the global placement new operator(it is not overwritten by inalogic) to create GlobalSingletonInitializer
00064 // inside the application data space by calling SystemInitializer(). At shutdown, call SystemShutdown()
00065 //
00066 // You may also create GlobalSingletonInitializer in this way:
00067 //      main()
00068 //      {
00069 //          GlobalSingletonInitializer GlobalInitializer;
00070 //      }
00071 //
00072 //
00073 
00074   class GlobalSingletonInitializer
00075   {
00076     NUX_DISABLE_OBJECT_COPY (GlobalSingletonInitializer);
00077     GlobalSingletonInitializer *operator & ();
00078     const GlobalSingletonInitializer *operator & () const;
00079 
00080   public:
00081     GlobalSingletonInitializer();
00082     ~GlobalSingletonInitializer();
00083 
00084     static bool Ready();
00085   private:
00086     static bool m_bGlobalObjectsReady;
00087 
00088     NUX_GLOBAL_OBJECT_INIT_SEQUENCE();
00089   };
00090 
00091 // Hide these functions
00092 // extern NUX_DLL_ENTRY void SystemStart();
00093 // extern NUX_DLL_ENTRY void SystemShutdown();
00094 
00095 
00096 
00097 // Nifty Counter idiom. See http://www-d0.fnal.gov/KAI/doc/tutorials/static_initialization.html
00098   class GlobalInitializer
00099   {
00100   public:
00101     GlobalInitializer();
00102     ~GlobalInitializer();
00103   private:
00104     static int m_Count;
00105   };
00106 
00107 // Every compilation unit that includes this file will have its own instance of sGlobalInitializer. sGlobalInitializer is initialized
00108 // before the main function of the program is called. The first time sGlobalInitializer is initialized, it calls SystemStart() to create
00109 // our global object singleton. In SystemStart() we have a change to create our singletons in any order we like.
00110 // When the program exits, every instance of sGlobalInitializer will be destroyed. The last instance destroyed will call SystemShutdown().
00111 // In SystemShutdown() we can destroy our global objects in any order we like.
00112 
00113   static GlobalInitializer sGlobalInitializer;
00114 
00115 
00116 }
00117 
00118 #endif // NGLOBALINITIALIZER_H
00119