Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:13

0001 // Copyright (C) 2000 Stephen Cleary
0002 // Copyright (C) 2008 Ion Gaztanaga
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See
0005 // accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // See http://www.boost.org for updates, documentation, and revision history.
0009 //
0010 // This file is a modified file from Boost.Pool
0011 
0012 //////////////////////////////////////////////////////////////////////////////
0013 //
0014 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
0015 // Software License, Version 1.0. (See accompanying file
0016 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0017 //
0018 // See http://www.boost.org/libs/container for documentation.
0019 //
0020 //////////////////////////////////////////////////////////////////////////////
0021 
0022 #ifndef BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
0023 #define BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP
0024 
0025 #ifndef BOOST_CONFIG_HPP
0026 #  include <boost/config.hpp>
0027 #endif
0028 
0029 #if defined(BOOST_HAS_PRAGMA_ONCE)
0030 #  pragma once
0031 #endif
0032 
0033 #include <boost/container/detail/config_begin.hpp>
0034 #include <boost/container/detail/workaround.hpp>
0035 
0036 //
0037 // The following helper classes are placeholders for a generic "singleton"
0038 //  class.  The classes below support usage of singletons, including use in
0039 //  program startup/shutdown code, AS LONG AS there is only one thread
0040 //  running before main() begins, and only one thread running after main()
0041 //  exits.
0042 //
0043 // This class is also limited in that it can only provide singleton usage for
0044 //  classes with default constructors.
0045 //
0046 
0047 // The design of this class is somewhat twisted, but can be followed by the
0048 //  calling inheritance.  Let us assume that there is some user code that
0049 //  calls "singleton_default<T>::instance()".  The following (convoluted)
0050 //  sequence ensures that the same function will be called before main():
0051 //    instance() contains a call to create_object.do_nothing()
0052 //    Thus, object_creator is implicitly instantiated, and create_object
0053 //      must exist.
0054 //    Since create_object is a static member, its constructor must be
0055 //      called before main().
0056 //    The constructor contains a call to instance(), thus ensuring that
0057 //      instance() will be called before main().
0058 //    The first time instance() is called (i.e., before main()) is the
0059 //      latest point in program execution where the object of type T
0060 //      can be created.
0061 //    Thus, any call to instance() will auto-magically result in a call to
0062 //      instance() before main(), unless already present.
0063 //  Furthermore, since the instance() function contains the object, instead
0064 //  of the singleton_default class containing a static instance of the
0065 //  object, that object is guaranteed to be constructed (at the latest) in
0066 //  the first call to instance().  This permits calls to instance() from
0067 //  static code, even if that code is called before the file-scope objects
0068 //  in this file have been initialized.
0069 
0070 namespace boost {
0071 namespace container {
0072 namespace dtl {
0073 
0074 // T must be: no-throw default constructible and no-throw destructible
0075 template <typename T>
0076 struct singleton_default
0077 {
0078   private:
0079     struct object_creator
0080     {
0081       // This constructor does nothing more than ensure that instance()
0082       //  is called before main() begins, thus creating the static
0083       //  T object before multithreading race issues can come up.
0084       object_creator() { singleton_default<T>::instance(); }
0085       inline void do_nothing() const { }
0086     };
0087     static object_creator create_object;
0088 
0089     singleton_default();
0090 
0091   public:
0092     typedef T object_type;
0093 
0094     // If, at any point (in user code), singleton_default<T>::instance()
0095     //  is called, then the following function is instantiated.
0096     static object_type & instance()
0097     {
0098       // This is the object that we return a reference to.
0099       // It is guaranteed to be created before main() begins because of
0100       //  the next line.
0101       static object_type obj;
0102 
0103       // The following line does nothing else than force the instantiation
0104       //  of singleton_default<T>::create_object, whose constructor is
0105       //  called before main() begins.
0106       create_object.do_nothing();
0107 
0108       return obj;
0109     }
0110 };
0111 template <typename T>
0112 typename singleton_default<T>::object_creator
0113 singleton_default<T>::create_object;
0114 
0115 } // namespace dtl
0116 } // namespace container
0117 } // namespace boost
0118 
0119 #include <boost/container/detail/config_end.hpp>
0120 
0121 #endif   //BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP