Chris@102
|
1 // Copyright (C) 2000 Stephen Cleary
|
Chris@102
|
2 // Copyright (C) 2008 Ion Gaztanaga
|
Chris@102
|
3 //
|
Chris@102
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@102
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
7 //
|
Chris@102
|
8 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@102
|
9 //
|
Chris@102
|
10 // This file is a modified file from Boost.Pool
|
Chris@102
|
11
|
Chris@102
|
12 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
13 //
|
Chris@102
|
14 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
|
Chris@102
|
15 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
16 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
17 //
|
Chris@102
|
18 // See http://www.boost.org/libs/container for documentation.
|
Chris@102
|
19 //
|
Chris@102
|
20 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
21
|
Chris@102
|
22 #ifndef BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
|
Chris@102
|
23 #define BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
|
Chris@102
|
24
|
Chris@102
|
25 #ifndef BOOST_CONFIG_HPP
|
Chris@102
|
26 # include <boost/config.hpp>
|
Chris@102
|
27 #endif
|
Chris@102
|
28
|
Chris@102
|
29 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@102
|
30 # pragma once
|
Chris@102
|
31 #endif
|
Chris@102
|
32
|
Chris@102
|
33 #include <boost/container/detail/config_begin.hpp>
|
Chris@102
|
34 #include <boost/container/detail/workaround.hpp>
|
Chris@102
|
35
|
Chris@102
|
36 //
|
Chris@102
|
37 // The following helper classes are placeholders for a generic "singleton"
|
Chris@102
|
38 // class. The classes below support usage of singletons, including use in
|
Chris@102
|
39 // program startup/shutdown code, AS LONG AS there is only one thread
|
Chris@102
|
40 // running before main() begins, and only one thread running after main()
|
Chris@102
|
41 // exits.
|
Chris@102
|
42 //
|
Chris@102
|
43 // This class is also limited in that it can only provide singleton usage for
|
Chris@102
|
44 // classes with default constructors.
|
Chris@102
|
45 //
|
Chris@102
|
46
|
Chris@102
|
47 // The design of this class is somewhat twisted, but can be followed by the
|
Chris@102
|
48 // calling inheritance. Let us assume that there is some user code that
|
Chris@102
|
49 // calls "singleton_default<T>::instance()". The following (convoluted)
|
Chris@102
|
50 // sequence ensures that the same function will be called before main():
|
Chris@102
|
51 // instance() contains a call to create_object.do_nothing()
|
Chris@102
|
52 // Thus, object_creator is implicitly instantiated, and create_object
|
Chris@102
|
53 // must exist.
|
Chris@102
|
54 // Since create_object is a static member, its constructor must be
|
Chris@102
|
55 // called before main().
|
Chris@102
|
56 // The constructor contains a call to instance(), thus ensuring that
|
Chris@102
|
57 // instance() will be called before main().
|
Chris@102
|
58 // The first time instance() is called (i.e., before main()) is the
|
Chris@102
|
59 // latest point in program execution where the object of type T
|
Chris@102
|
60 // can be created.
|
Chris@102
|
61 // Thus, any call to instance() will auto-magically result in a call to
|
Chris@102
|
62 // instance() before main(), unless already present.
|
Chris@102
|
63 // Furthermore, since the instance() function contains the object, instead
|
Chris@102
|
64 // of the singleton_default class containing a static instance of the
|
Chris@102
|
65 // object, that object is guaranteed to be constructed (at the latest) in
|
Chris@102
|
66 // the first call to instance(). This permits calls to instance() from
|
Chris@102
|
67 // static code, even if that code is called before the file-scope objects
|
Chris@102
|
68 // in this file have been initialized.
|
Chris@102
|
69
|
Chris@102
|
70 namespace boost {
|
Chris@102
|
71 namespace container {
|
Chris@102
|
72 namespace container_detail {
|
Chris@102
|
73
|
Chris@102
|
74 // T must be: no-throw default constructible and no-throw destructible
|
Chris@102
|
75 template <typename T>
|
Chris@102
|
76 struct singleton_default
|
Chris@102
|
77 {
|
Chris@102
|
78 private:
|
Chris@102
|
79 struct object_creator
|
Chris@102
|
80 {
|
Chris@102
|
81 // This constructor does nothing more than ensure that instance()
|
Chris@102
|
82 // is called before main() begins, thus creating the static
|
Chris@102
|
83 // T object before multithreading race issues can come up.
|
Chris@102
|
84 object_creator() { singleton_default<T>::instance(); }
|
Chris@102
|
85 inline void do_nothing() const { }
|
Chris@102
|
86 };
|
Chris@102
|
87 static object_creator create_object;
|
Chris@102
|
88
|
Chris@102
|
89 singleton_default();
|
Chris@102
|
90
|
Chris@102
|
91 public:
|
Chris@102
|
92 typedef T object_type;
|
Chris@102
|
93
|
Chris@102
|
94 // If, at any point (in user code), singleton_default<T>::instance()
|
Chris@102
|
95 // is called, then the following function is instantiated.
|
Chris@102
|
96 static object_type & instance()
|
Chris@102
|
97 {
|
Chris@102
|
98 // This is the object that we return a reference to.
|
Chris@102
|
99 // It is guaranteed to be created before main() begins because of
|
Chris@102
|
100 // the next line.
|
Chris@102
|
101 static object_type obj;
|
Chris@102
|
102
|
Chris@102
|
103 // The following line does nothing else than force the instantiation
|
Chris@102
|
104 // of singleton_default<T>::create_object, whose constructor is
|
Chris@102
|
105 // called before main() begins.
|
Chris@102
|
106 create_object.do_nothing();
|
Chris@102
|
107
|
Chris@102
|
108 return obj;
|
Chris@102
|
109 }
|
Chris@102
|
110 };
|
Chris@102
|
111 template <typename T>
|
Chris@102
|
112 typename singleton_default<T>::object_creator
|
Chris@102
|
113 singleton_default<T>::create_object;
|
Chris@102
|
114
|
Chris@102
|
115 } // namespace container_detail
|
Chris@102
|
116 } // namespace container
|
Chris@102
|
117 } // namespace boost
|
Chris@102
|
118
|
Chris@102
|
119 #include <boost/container/detail/config_end.hpp>
|
Chris@102
|
120
|
Chris@102
|
121 #endif //BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
|