Singleton.cpp

00001 
00002 // The Loki Library
00003 // Copyright (c) 2001 by Andrei Alexandrescu
00004 // This code accompanies the book:
00005 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
00006 //     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
00007 // Permission to use, copy, modify, distribute and sell this software for any 
00008 //     purpose is hereby granted without fee, provided that the above copyright 
00009 //     notice appear in all copies and that both that copyright notice and this 
00010 //     permission notice appear in supporting documentation.
00011 // The author or Addison-Wesley Longman make no representations about the 
00012 //     suitability of this software for any purpose. It is provided "as is" 
00013 //     without express or implied warranty.
00015 
00016 // $Id: Singleton.cpp 756 2006-10-17 20:05:42Z syntheticpp $
00017 
00018 
00019 #include <loki/Singleton.h>
00020 
00021 
00022 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
00023 Loki::Private::TrackerArray* Loki::Private::pTrackerArray = 0;
00024 #else
00025 Loki::Private::TrackerArray Loki::Private::pTrackerArray = 0;
00026 unsigned int Loki::Private::elements = 0;
00027 #endif
00028 
00030 // function AtExitFn
00031 // Ensures proper destruction of objects with longevity
00033 
00034 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
00035 
00036 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
00037 {
00038     assert(pTrackerArray!=0 && !pTrackerArray->empty());
00039     
00040     // Pick the element at the top of the stack
00041     LifetimeTracker* pTop = pTrackerArray->back();
00042     
00043     // Remove that object off the stack _before_ deleting pTop
00044     pTrackerArray->pop_back();
00045     
00046     // Destroy the element
00047     delete pTop;
00048     
00049     // Destroy stack when it's empty _after_ deleting pTop
00050     if(pTrackerArray->empty())
00051     {
00052         delete pTrackerArray;
00053         pTrackerArray = 0;
00054     }
00055 }
00056 
00057 #else
00058 
00059 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
00060 {
00061     assert(elements > 0 && pTrackerArray != 0);
00062     // Pick the element at the top of the stack
00063     LifetimeTracker* pTop = pTrackerArray[elements - 1];
00064     // Remove that object off the stack
00065     // Don't check errors - realloc with less memory 
00066     //     can't fail
00067     pTrackerArray = static_cast<TrackerArray>(std::realloc(
00068         pTrackerArray, sizeof(*pTrackerArray) * --elements));
00069     // Destroy the element
00070     delete pTop;
00071 }
00072 
00073 #endif 
00074 

Generated on Sun Feb 25 16:52:27 2007 for Loki by  doxygen 1.5.1-p1