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)
9 * \author Andrey Semashev
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.
16 #ifndef BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
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>
24 #include <boost/log/utility/no_unused_warnings.hpp>
29 namespace BOOST_LOG_NAMESPACE {
33 //! A base class for singletons, constructed on-demand
34 template< typename DerivedT, typename StorageT = DerivedT >
35 class lazy_singleton : noncopyable
38 //! Returns the singleton instance
39 static StorageT& get()
41 #if !defined(BOOST_LOG_NO_THREADS)
42 static once_flag flag = BOOST_ONCE_INIT;
43 boost::call_once(flag, &DerivedT::init_instance);
45 static const bool initialized = (DerivedT::init_instance(), true);
46 BOOST_LOG_NO_UNUSED_WARNINGS(initialized);
48 return get_instance();
51 //! Initializes the singleton instance
52 static void init_instance()
58 //! Returns the singleton instance (not thread-safe)
59 static StorageT& get_instance()
61 static StorageT instance;
66 //! A base class for singletons, constructed on namespace scope initialization stage
67 template< typename DerivedT, typename StorageT = DerivedT >
69 public lazy_singleton< DerivedT, StorageT >
72 static StorageT& instance;
75 template< typename DerivedT, typename StorageT >
76 StorageT& singleton< DerivedT, StorageT >::instance =
77 lazy_singleton< DerivedT, StorageT >::get();
85 #endif // BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_