Back to index

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