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-Welsey 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 // Last update: June 20, 2001
17 
18 #include "Singleton.h"
19 
20 using namespace Loki::Private;
21 
22 Loki::Private::TrackerArray Loki::Private::pTrackerArray = 0;
23 unsigned int Loki::Private::elements = 0;
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 // function AtExitFn
27 // Ensures proper destruction of objects with longevity
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 void Loki::Private::AtExitFn()
31 {
32     assert(elements > 0 && pTrackerArray != 0);
33     // Pick the element at the top of the stack
34     LifetimeTracker* pTop = pTrackerArray[elements - 1];
35     // Remove that object off the stack
36     // Don't check errors - realloc with less memory 
37     //     can't fail
38     pTrackerArray = static_cast<TrackerArray>(std::realloc(
39         pTrackerArray, --elements));
40     // Destroy the element
41     delete pTop;
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 // Change log:
46 // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
47 ////////////////////////////////////////////////////////////////////////////////
posted @ 2012-10-31 15:18  crazylhf  阅读(163)  评论(0)    收藏  举报