• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
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   attribute_value_impl.hpp
9  * \author Andrey Semashev
10  * \date   24.06.2007
11  *
12  * The header contains an implementation of a basic attribute value implementation class.
13  */
14 
15 #ifndef BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_
17 
18 #include <boost/type_index.hpp>
19 #include <boost/move/core.hpp>
20 #include <boost/move/utility_core.hpp>
21 #include <boost/type_traits/remove_cv.hpp>
22 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
23 #include <boost/log/detail/config.hpp>
24 #include <boost/log/attributes/attribute_value.hpp>
25 #include <boost/log/utility/type_dispatch/type_dispatcher.hpp>
26 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
27 #include <boost/type_traits/remove_reference.hpp>
28 #endif
29 #include <boost/log/detail/header.hpp>
30 
31 #ifdef BOOST_HAS_PRAGMA_ONCE
32 #pragma once
33 #endif
34 
35 namespace boost {
36 
37 BOOST_LOG_OPEN_NAMESPACE
38 
39 namespace attributes {
40 
41 /*!
42  * \brief Basic attribute value implementation class
43  *
44  * This class can be used as a boilerplate for simple attribute values. The class implements all needed
45  * interfaces of attribute values and allows to store a single value of the type specified as a template parameter.
46  * The stored value can be dispatched with type dispatching mechanism.
47  */
48 template< typename T >
49 class attribute_value_impl :
50     public attribute_value::impl
51 {
52 public:
53     //! Value type
54     typedef T value_type;
55 
56 private:
57     //! Attribute value
58     const value_type m_value;
59 
60 public:
61     /*!
62      * Constructor with initialization of the stored value
63      */
attribute_value_impl(value_type const & v)64     explicit attribute_value_impl(value_type const& v) : m_value(v) {}
65     /*!
66      * Constructor with initialization of the stored value
67      */
BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible<value_type>::value)68     explicit attribute_value_impl(BOOST_RV_REF(value_type) v) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< value_type >::value) :
69         m_value(boost::move(v))
70     {
71     }
72 
73     /*!
74      * Attribute value dispatching method.
75      *
76      * \param dispatcher The dispatcher that receives the stored value
77      *
78      * \return \c true if the value has been dispatched, \c false otherwise
79      */
dispatch(type_dispatcher & dispatcher)80     bool dispatch(type_dispatcher& dispatcher) BOOST_OVERRIDE
81     {
82         type_dispatcher::callback< value_type > callback = dispatcher.get_callback< value_type >();
83         if (callback)
84         {
85             callback(m_value);
86             return true;
87         }
88         else
89             return false;
90     }
91 
92     /*!
93      * \return The attribute value type
94      */
get_type() const95     typeindex::type_index get_type() const BOOST_OVERRIDE { return typeindex::type_id< value_type >(); }
96 
97     /*!
98      * \return Reference to the contained value.
99      */
get() const100     value_type const& get() const { return m_value; }
101 };
102 
103 /*!
104  * The function creates an attribute value from the specified object.
105  */
106 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
107 
108 template< typename T >
make_attribute_value(T && v)109 inline attribute_value make_attribute_value(T&& v)
110 {
111     typedef typename remove_cv< typename remove_reference< T >::type >::type value_type;
112     return attribute_value(new attribute_value_impl< value_type >(boost::forward< T >(v)));
113 }
114 
115 #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
116 
117 template< typename T >
make_attribute_value(T const & v)118 inline attribute_value make_attribute_value(T const& v)
119 {
120     typedef typename remove_cv< T >::type value_type;
121     return attribute_value(new attribute_value_impl< value_type >(v));
122 }
123 
124 template< typename T >
make_attribute_value(rv<T> const & v)125 inline attribute_value make_attribute_value(rv< T > const& v)
126 {
127     typedef typename remove_cv< T >::type value_type;
128     return attribute_value(new attribute_value_impl< value_type >(v));
129 }
130 
131 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
132 
133 } // namespace attributes
134 
135 BOOST_LOG_CLOSE_NAMESPACE // namespace log
136 
137 } // namespace boost
138 
139 #include <boost/log/detail/footer.hpp>
140 
141 #endif // BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_
142