~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/pool/detail/singleton.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000 Stephen Cleary (shammah@voyager.net)
 
2
//
 
3
// This file can be redistributed and/or modified under the terms found
 
4
//  in "copyright.html"
 
5
// This software and its documentation is provided "as is" without express or
 
6
//  implied warranty, and with no claim as to its suitability for any purpose.
 
7
//
 
8
// See http://www.boost.org for updates, documentation, and revision history.
 
9
 
 
10
#ifndef BOOST_POOL_SINGLETON_HPP
 
11
#define BOOST_POOL_SINGLETON_HPP
 
12
 
 
13
// The following code might be put into some Boost.Config header in a later revision
 
14
#ifdef __BORLANDC__
 
15
# pragma option push -w-inl
 
16
#endif
 
17
 
 
18
//
 
19
// The following helper classes are placeholders for a generic "singleton"
 
20
//  class.  The classes below support usage of singletons, including use in
 
21
//  program startup/shutdown code, AS LONG AS there is only one thread
 
22
//  running before main() begins, and only one thread running after main()
 
23
//  exits.
 
24
//
 
25
// This class is also limited in that it can only provide singleton usage for
 
26
//  classes with default constructors.
 
27
//
 
28
 
 
29
// The design of this class is somewhat twisted, but can be followed by the
 
30
//  calling inheritance.  Let us assume that there is some user code that
 
31
//  calls "singleton_default<T>::instance()".  The following (convoluted)
 
32
//  sequence ensures that the same function will be called before main():
 
33
//    instance() contains a call to create_object.do_nothing()
 
34
//    Thus, object_creator is implicitly instantiated, and create_object
 
35
//      must exist.
 
36
//    Since create_object is a static member, its constructor must be
 
37
//      called before main().
 
38
//    The constructor contains a call to instance(), thus ensuring that
 
39
//      instance() will be called before main().
 
40
//    The first time instance() is called (i.e., before main()) is the
 
41
//      latest point in program execution where the object of type T
 
42
//      can be created.
 
43
//    Thus, any call to instance() will auto-magically result in a call to
 
44
//      instance() before main(), unless already present.
 
45
//  Furthermore, since the instance() function contains the object, instead
 
46
//  of the singleton_default class containing a static instance of the
 
47
//  object, that object is guaranteed to be constructed (at the latest) in
 
48
//  the first call to instance().  This permits calls to instance() from
 
49
//  static code, even if that code is called before the file-scope objects
 
50
//  in this file have been initialized.
 
51
 
 
52
namespace boost {
 
53
 
 
54
namespace details {
 
55
namespace pool {
 
56
 
 
57
// T must be: no-throw default constructible and no-throw destructible
 
58
template <typename T>
 
59
struct singleton_default
 
60
{
 
61
  private:
 
62
    struct object_creator
 
63
    {
 
64
      // This constructor does nothing more than ensure that instance()
 
65
      //  is called before main() begins, thus creating the static
 
66
      //  T object before multithreading race issues can come up.
 
67
      object_creator() { singleton_default<T>::instance(); }
 
68
      inline void do_nothing() const { }
 
69
    };
 
70
    static object_creator create_object;
 
71
 
 
72
    singleton_default();
 
73
 
 
74
  public:
 
75
    typedef T object_type;
 
76
 
 
77
    // If, at any point (in user code), singleton_default<T>::instance()
 
78
    //  is called, then the following function is instantiated.
 
79
    static object_type & instance()
 
80
    {
 
81
      // This is the object that we return a reference to.
 
82
      // It is guaranteed to be created before main() begins because of
 
83
      //  the next line.
 
84
      static object_type obj;
 
85
 
 
86
      // The following line does nothing else than force the instantiation
 
87
      //  of singleton_default<T>::create_object, whose constructor is
 
88
      //  called before main() begins.
 
89
      create_object.do_nothing();
 
90
 
 
91
      return obj;
 
92
    }
 
93
};
 
94
template <typename T>
 
95
typename singleton_default<T>::object_creator
 
96
singleton_default<T>::create_object;
 
97
 
 
98
} // namespace pool
 
99
} // namespace details
 
100
 
 
101
} // namespace boost
 
102
 
 
103
// The following code might be put into some Boost.Config header in a later revision
 
104
#ifdef __BORLANDC__
 
105
# pragma option pop
 
106
#endif
 
107
 
 
108
#endif