Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file singleton.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 20.04.2008
|
Chris@16
|
11 *
|
Chris@16
|
12 * \brief This header is the Boost.Log library implementation, see the library documentation
|
Chris@16
|
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20 #include <boost/log/utility/once_block.hpp>
|
Chris@16
|
21 #include <boost/log/detail/header.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
24 #pragma once
|
Chris@16
|
25 #endif
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28
|
Chris@16
|
29 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
30
|
Chris@16
|
31 namespace aux {
|
Chris@16
|
32
|
Chris@16
|
33 //! A base class for singletons, constructed on-demand
|
Chris@16
|
34 template< typename DerivedT, typename StorageT = DerivedT >
|
Chris@16
|
35 class lazy_singleton
|
Chris@16
|
36 {
|
Chris@16
|
37 public:
|
Chris@16
|
38 BOOST_DEFAULTED_FUNCTION(lazy_singleton(), {})
|
Chris@16
|
39
|
Chris@16
|
40 //! Returns the singleton instance
|
Chris@16
|
41 static StorageT& get()
|
Chris@16
|
42 {
|
Chris@16
|
43 BOOST_LOG_ONCE_BLOCK()
|
Chris@16
|
44 {
|
Chris@16
|
45 DerivedT::init_instance();
|
Chris@16
|
46 }
|
Chris@16
|
47 return get_instance();
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 //! Initializes the singleton instance
|
Chris@16
|
51 static void init_instance()
|
Chris@16
|
52 {
|
Chris@16
|
53 get_instance();
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 BOOST_DELETED_FUNCTION(lazy_singleton(lazy_singleton const&))
|
Chris@16
|
57 BOOST_DELETED_FUNCTION(lazy_singleton& operator= (lazy_singleton const&))
|
Chris@16
|
58
|
Chris@16
|
59 protected:
|
Chris@16
|
60 //! Returns the singleton instance (not thread-safe)
|
Chris@16
|
61 static StorageT& get_instance()
|
Chris@16
|
62 {
|
Chris@16
|
63 static StorageT instance;
|
Chris@16
|
64 return instance;
|
Chris@16
|
65 }
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 //! A base class for singletons, constructed on namespace scope initialization stage
|
Chris@16
|
69 template< typename DerivedT, typename StorageT = DerivedT >
|
Chris@16
|
70 class singleton :
|
Chris@16
|
71 public lazy_singleton< DerivedT, StorageT >
|
Chris@16
|
72 {
|
Chris@16
|
73 public:
|
Chris@16
|
74 static StorageT& instance;
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@16
|
77 template< typename DerivedT, typename StorageT >
|
Chris@16
|
78 StorageT& singleton< DerivedT, StorageT >::instance =
|
Chris@16
|
79 lazy_singleton< DerivedT, StorageT >::get();
|
Chris@16
|
80
|
Chris@16
|
81 } // namespace aux
|
Chris@16
|
82
|
Chris@16
|
83 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
84
|
Chris@16
|
85 } // namespace boost
|
Chris@16
|
86
|
Chris@16
|
87 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
88
|
Chris@16
|
89 #endif // BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
|