Back to index

nux  3.0.0
GlobalInitializer.cpp
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 #include "NuxCore.h"
00024 
00025 namespace nux
00026 {
00027 
00028   static GlobalSingletonInitializer      *GGlobalInitializer = 0;
00029 
00030   static void SystemStart()
00031   {
00032     static unsigned char StaticBuffer [sizeof (GlobalSingletonInitializer)];
00033     // Placement new in our reserved buffer.
00034     GGlobalInitializer =  new (StaticBuffer) GlobalSingletonInitializer ();
00035 
00036 #if defined (NUX_OS_WINDOWS)
00037     GLogDevice.AddOutputDevice (&NUX_GLOBAL_OBJECT_INSTANCE (LogFileOutput));
00038     GLogDevice.AddOutputDevice (&NUX_GLOBAL_OBJECT_INSTANCE (VisualOutputConsole));
00039     GLogDevice.AddOutputDevice (&NUX_GLOBAL_OBJECT_INSTANCE (PrintfOutputConsole));
00040 #else
00041     GLogDevice.AddOutputDevice (&NUX_GLOBAL_OBJECT_INSTANCE (LogFileOutput));
00042     GLogDevice.AddOutputDevice (&NUX_GLOBAL_OBJECT_INSTANCE (PrintfOutputConsole));
00043 #endif
00044   }
00045 
00046   static void SystemShutdown()
00047   {
00048     // Manually destroy initializer
00049     if (GGlobalInitializer)
00050       GGlobalInitializer->~GlobalSingletonInitializer();
00051 
00052     GGlobalInitializer = 0;
00053 
00054   }
00055 
00056   bool GlobalSingletonInitializer::m_bGlobalObjectsReady = false;
00057   GlobalSingletonInitializer::GlobalSingletonInitializer ()
00058   {
00059     m_bGlobalObjectsReady = true;
00060   }
00061 
00062   GlobalSingletonInitializer::~GlobalSingletonInitializer()
00063   {
00064     m_bGlobalObjectsReady = false;
00065   }
00066 
00067   bool GlobalSingletonInitializer::Ready()
00068   {
00069     return m_bGlobalObjectsReady;
00070   }
00071 
00072   int GlobalInitializer::m_Count = 0;
00073   GlobalInitializer::GlobalInitializer()
00074   {
00075     if (m_Count++ == 0)
00076     {
00077       SystemStart();
00078     }
00079   }
00080 
00081   GlobalInitializer::~GlobalInitializer()
00082   {
00083     if (--m_Count == 0)
00084     {
00085       SystemShutdown();
00086     }
00087   }
00088 
00089 void GlobalInitializer::ForceShutdown()
00090 {
00091   SystemShutdown();
00092 }
00093 
00094 }