1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/pool/detail/singleton.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,107 @@
1.4 +// Copyright (C) 2000 Stephen Cleary
1.5 +//
1.6 +// Distributed under the Boost Software License, Version 1.0. (See
1.7 +// accompanying file LICENSE_1_0.txt or copy at
1.8 +// http://www.boost.org/LICENSE_1_0.txt)
1.9 +//
1.10 +// See http://www.boost.org for updates, documentation, and revision history.
1.11 +
1.12 +#ifndef BOOST_POOL_SINGLETON_HPP
1.13 +#define BOOST_POOL_SINGLETON_HPP
1.14 +
1.15 +// The following code might be put into some Boost.Config header in a later revision
1.16 +#ifdef __BORLANDC__
1.17 +# pragma option push -w-inl
1.18 +#endif
1.19 +
1.20 +//
1.21 +// The following helper classes are placeholders for a generic "singleton"
1.22 +// class. The classes below support usage of singletons, including use in
1.23 +// program startup/shutdown code, AS LONG AS there is only one thread
1.24 +// running before main() begins, and only one thread running after main()
1.25 +// exits.
1.26 +//
1.27 +// This class is also limited in that it can only provide singleton usage for
1.28 +// classes with default constructors.
1.29 +//
1.30 +
1.31 +// The design of this class is somewhat twisted, but can be followed by the
1.32 +// calling inheritance. Let us assume that there is some user code that
1.33 +// calls "singleton_default<T>::instance()". The following (convoluted)
1.34 +// sequence ensures that the same function will be called before main():
1.35 +// instance() contains a call to create_object.do_nothing()
1.36 +// Thus, object_creator is implicitly instantiated, and create_object
1.37 +// must exist.
1.38 +// Since create_object is a static member, its constructor must be
1.39 +// called before main().
1.40 +// The constructor contains a call to instance(), thus ensuring that
1.41 +// instance() will be called before main().
1.42 +// The first time instance() is called (i.e., before main()) is the
1.43 +// latest point in program execution where the object of type T
1.44 +// can be created.
1.45 +// Thus, any call to instance() will auto-magically result in a call to
1.46 +// instance() before main(), unless already present.
1.47 +// Furthermore, since the instance() function contains the object, instead
1.48 +// of the singleton_default class containing a static instance of the
1.49 +// object, that object is guaranteed to be constructed (at the latest) in
1.50 +// the first call to instance(). This permits calls to instance() from
1.51 +// static code, even if that code is called before the file-scope objects
1.52 +// in this file have been initialized.
1.53 +
1.54 +namespace boost {
1.55 +
1.56 +namespace details {
1.57 +namespace pool {
1.58 +
1.59 +// T must be: no-throw default constructible and no-throw destructible
1.60 +template <typename T>
1.61 +struct singleton_default
1.62 +{
1.63 + private:
1.64 + struct object_creator
1.65 + {
1.66 + // This constructor does nothing more than ensure that instance()
1.67 + // is called before main() begins, thus creating the static
1.68 + // T object before multithreading race issues can come up.
1.69 + object_creator() { singleton_default<T>::instance(); }
1.70 + inline void do_nothing() const { }
1.71 + };
1.72 + static object_creator create_object;
1.73 +
1.74 + singleton_default();
1.75 +
1.76 + public:
1.77 + typedef T object_type;
1.78 +
1.79 + // If, at any point (in user code), singleton_default<T>::instance()
1.80 + // is called, then the following function is instantiated.
1.81 + static object_type & instance()
1.82 + {
1.83 + // This is the object that we return a reference to.
1.84 + // It is guaranteed to be created before main() begins because of
1.85 + // the next line.
1.86 + static object_type obj;
1.87 +
1.88 + // The following line does nothing else than force the instantiation
1.89 + // of singleton_default<T>::create_object, whose constructor is
1.90 + // called before main() begins.
1.91 + create_object.do_nothing();
1.92 +
1.93 + return obj;
1.94 + }
1.95 +};
1.96 +template <typename T>
1.97 +typename singleton_default<T>::object_creator
1.98 +singleton_default<T>::create_object;
1.99 +
1.100 +} // namespace pool
1.101 +} // namespace details
1.102 +
1.103 +} // namespace boost
1.104 +
1.105 +// The following code might be put into some Boost.Config header in a later revision
1.106 +#ifdef __BORLANDC__
1.107 +# pragma option pop
1.108 +#endif
1.109 +
1.110 +#endif