]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/log/detail/singleton.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / log / detail / singleton.hpp
1 /*
2  *          Copyright Andrey Semashev 2007 - 2010.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   singleton.hpp
9  * \author Andrey Semashev
10  * \date   20.04.2008
11  *
12  * \brief  This header is the Boost.Log library implementation, see the library documentation
13  *         at http://www.boost.org/libs/log/doc/log.html.
14  */
15
16 #ifndef BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
18
19 #include <boost/noncopyable.hpp>
20 #include <boost/log/detail/prologue.hpp>
21 #if !defined(BOOST_LOG_NO_THREADS)
22 #include <boost/thread/once.hpp>
23 #else
24 #include <boost/log/utility/no_unused_warnings.hpp>
25 #endif
26
27 namespace boost {
28
29 namespace BOOST_LOG_NAMESPACE {
30
31 namespace aux {
32
33 //! A base class for singletons, constructed on-demand
34 template< typename DerivedT, typename StorageT = DerivedT >
35 class lazy_singleton : noncopyable
36 {
37 public:
38     //! Returns the singleton instance
39     static StorageT& get()
40     {
41 #if !defined(BOOST_LOG_NO_THREADS)
42         static once_flag flag = BOOST_ONCE_INIT;
43         boost::call_once(flag, &DerivedT::init_instance);
44 #else
45         static const bool initialized = (DerivedT::init_instance(), true);
46         BOOST_LOG_NO_UNUSED_WARNINGS(initialized);
47 #endif
48         return get_instance();
49     }
50
51     //! Initializes the singleton instance
52     static void init_instance()
53     {
54         get_instance();
55     }
56
57 protected:
58     //! Returns the singleton instance (not thread-safe)
59     static StorageT& get_instance()
60     {
61         static StorageT instance;
62         return instance;
63     }
64 };
65
66 //! A base class for singletons, constructed on namespace scope initialization stage
67 template< typename DerivedT, typename StorageT = DerivedT >
68 class singleton :
69     public lazy_singleton< DerivedT, StorageT >
70 {
71 public:
72     static StorageT& instance;
73 };
74
75 template< typename DerivedT, typename StorageT >
76 StorageT& singleton< DerivedT, StorageT >::instance =
77     lazy_singleton< DerivedT, StorageT >::get();
78
79 } // namespace aux
80
81 } // namespace log
82
83 } // namespace boost
84
85 #endif // BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_