]> git.cworth.org Git - vogl/blob - src/extlib/loki/src/Singleton.cpp
Initial vogl checkin
[vogl] / src / extlib / loki / src / Singleton.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // This code accompanies the book:
5 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6 //     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7 // Permission to use, copy, modify, distribute and sell this software for any
8 //     purpose is hereby granted without fee, provided that the above copyright
9 //     notice appear in all copies and that both that copyright notice and this
10 //     permission notice appear in supporting documentation.
11 // The author or Addison-Wesley Longman make no representations about the
12 //     suitability of this software for any purpose. It is provided "as is"
13 //     without express or implied warranty.
14 ////////////////////////////////////////////////////////////////////////////////
15
16 // $Id: Singleton.cpp 756 2006-10-17 20:05:42Z syntheticpp $
17
18
19 #include <loki/Singleton.h>
20
21
22 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
23 Loki::Private::TrackerArray *Loki::Private::pTrackerArray = 0;
24 #else
25 Loki::Private::TrackerArray Loki::Private::pTrackerArray = 0;
26 unsigned int Loki::Private::elements = 0;
27 #endif
28
29 ////////////////////////////////////////////////////////////////////////////////
30 // function AtExitFn
31 // Ensures proper destruction of objects with longevity
32 ////////////////////////////////////////////////////////////////////////////////
33
34 #ifdef LOKI_ENABLE_NEW_SETLONGLIVITY_HELPER_DATA_IMPL
35
36 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
37 {
38         assert(pTrackerArray!=0 && !pTrackerArray->empty());
39
40         // Pick the element at the top of the stack
41         LifetimeTracker *pTop = pTrackerArray->back();
42
43         // Remove that object off the stack _before_ deleting pTop
44         pTrackerArray->pop_back();
45
46         // Destroy the element
47         delete pTop;
48
49         // Destroy stack when it's empty _after_ deleting pTop
50         if(pTrackerArray->empty())
51         {
52                 delete pTrackerArray;
53                 pTrackerArray = 0;
54         }
55 }
56
57 #else
58
59 void LOKI_C_CALLING_CONVENTION_QUALIFIER Loki::Private::AtExitFn()
60 {
61         assert(elements > 0 && pTrackerArray != 0);
62         // Pick the element at the top of the stack
63         LifetimeTracker *pTop = pTrackerArray[elements - 1];
64         // Remove that object off the stack
65         // Don't check errors - realloc with less memory
66         //     can't fail
67         pTrackerArray = static_cast<TrackerArray>(std::realloc(
68                             pTrackerArray, sizeof(*pTrackerArray) * --elements));
69         // Destroy the element
70         delete pTop;
71 }
72
73 #endif
74