1
// Copyright (C) 2000 Stephen Cleary (shammah@voyager.net)
3
// This file can be redistributed and/or modified under the terms found
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.
8
// See http://www.boost.org for updates, documentation, and revision history.
10
#ifndef BOOST_POOL_SINGLETON_HPP
11
#define BOOST_POOL_SINGLETON_HPP
13
// The following code might be put into some Boost.Config header in a later revision
15
# pragma option push -w-inl
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()
25
// This class is also limited in that it can only provide singleton usage for
26
// classes with default constructors.
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
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
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.
57
// T must be: no-throw default constructible and no-throw destructible
59
struct singleton_default
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 { }
70
static object_creator create_object;
75
typedef T object_type;
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()
81
// This is the object that we return a reference to.
82
// It is guaranteed to be created before main() begins because of
84
static object_type obj;
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();
95
typename singleton_default<T>::object_creator
96
singleton_default<T>::create_object;
99
} // namespace details
103
// The following code might be put into some Boost.Config header in a later revision