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 add_value.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 26.11.2012
|
Chris@16
|
11 *
|
Chris@16
|
12 * This header contains the \c add_value manipulator.
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
|
Chris@16
|
16 #define BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
|
Chris@16
|
17
|
Chris@101
|
18 #include <boost/mpl/if.hpp>
|
Chris@101
|
19 #include <boost/type_traits/is_scalar.hpp>
|
Chris@16
|
20 #include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
21 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
22 #include <boost/log/detail/config.hpp>
|
Chris@16
|
23 #include <boost/log/detail/embedded_string_type.hpp>
|
Chris@16
|
24 #include <boost/log/attributes/attribute_name.hpp>
|
Chris@16
|
25 #include <boost/log/attributes/attribute_value_impl.hpp>
|
Chris@16
|
26 #include <boost/log/expressions/keyword_fwd.hpp>
|
Chris@16
|
27 #include <boost/log/sources/record_ostream.hpp>
|
Chris@16
|
28 #include <boost/log/detail/header.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
31 #pragma once
|
Chris@16
|
32 #endif
|
Chris@16
|
33
|
Chris@16
|
34 #ifdef _MSC_VER
|
Chris@16
|
35 #pragma warning(push)
|
Chris@16
|
36 // 'boost::log::v2s_mt_nt6::add_value_manip<RefT>::m_value' : reference member is initialized to a temporary that doesn't persist after the constructor exits
|
Chris@16
|
37 // This is intentional since the manipulator can be used with a temporary, which will be used before the streaming expression ends and it is destroyed.
|
Chris@16
|
38 #pragma warning(disable: 4413)
|
Chris@16
|
39 // returning address of local variable or temporary
|
Chris@16
|
40 // This warning refers to add_value_manip<RefT>::get_value() when RefT is an rvalue reference. We store the reference in the manipulator and we intend to return it as is.
|
Chris@16
|
41 #pragma warning(disable: 4172)
|
Chris@16
|
42 #endif
|
Chris@16
|
43
|
Chris@16
|
44 namespace boost {
|
Chris@16
|
45
|
Chris@16
|
46 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
47
|
Chris@16
|
48 //! Attribute value manipulator
|
Chris@16
|
49 template< typename RefT >
|
Chris@16
|
50 class add_value_manip
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 //! Stored reference type
|
Chris@16
|
54 typedef RefT reference_type;
|
Chris@16
|
55 //! Attribute value type
|
Chris@16
|
56 typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type;
|
Chris@16
|
57
|
Chris@16
|
58 private:
|
Chris@101
|
59 // The stored reference type is an lvalue reference since apparently different compilers (GCC and MSVC) have different quirks when rvalue references are stored as members.
|
Chris@101
|
60 // Additionally, MSVC (at least 11.0) has a bug which causes a dangling reference to be stored in the manipulator, if a scalar rvalue is passed to the add_value generator.
|
Chris@101
|
61 // To work around this problem we save the value inside the manipulator in this case.
|
Chris@101
|
62 typedef typename remove_reference< reference_type >::type& lvalue_reference_type;
|
Chris@101
|
63
|
Chris@101
|
64 typedef typename mpl::if_<
|
Chris@101
|
65 is_scalar< value_type >,
|
Chris@101
|
66 value_type,
|
Chris@101
|
67 lvalue_reference_type
|
Chris@101
|
68 >::type stored_type;
|
Chris@101
|
69
|
Chris@101
|
70 typedef typename mpl::if_<
|
Chris@101
|
71 is_scalar< value_type >,
|
Chris@101
|
72 value_type,
|
Chris@101
|
73 reference_type
|
Chris@101
|
74 >::type get_value_result_type;
|
Chris@16
|
75
|
Chris@16
|
76 private:
|
Chris@16
|
77 //! Attribute value
|
Chris@101
|
78 stored_type m_value;
|
Chris@16
|
79 //! Attribute name
|
Chris@16
|
80 attribute_name m_name;
|
Chris@16
|
81
|
Chris@16
|
82 public:
|
Chris@16
|
83 //! Initializing constructor
|
Chris@101
|
84 add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< lvalue_reference_type >(value)), m_name(name)
|
Chris@16
|
85 {
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 //! Returns attribute name
|
Chris@16
|
89 attribute_name get_name() const { return m_name; }
|
Chris@16
|
90 //! Returns attribute value
|
Chris@101
|
91 get_value_result_type get_value() const { return static_cast< get_value_result_type >(m_value); }
|
Chris@16
|
92 };
|
Chris@16
|
93
|
Chris@16
|
94 //! The operator attaches an attribute value to the log record
|
Chris@16
|
95 template< typename CharT, typename RefT >
|
Chris@16
|
96 inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip)
|
Chris@16
|
97 {
|
Chris@16
|
98 typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type;
|
Chris@16
|
99 attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value()));
|
Chris@16
|
100 strm.get_record().attribute_values().insert(manip.get_name(), value);
|
Chris@16
|
101 return strm;
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 //! The function creates a manipulator that attaches an attribute value to a log record
|
Chris@101
|
105 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@16
|
106
|
Chris@16
|
107 template< typename T >
|
Chris@16
|
108 inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value)
|
Chris@16
|
109 {
|
Chris@16
|
110 return add_value_manip< T&& >(name, static_cast< T&& >(value));
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 //! \overload
|
Chris@16
|
114 template< typename DescriptorT, template< typename > class ActorT >
|
Chris@16
|
115 inline add_value_manip< typename DescriptorT::value_type&& >
|
Chris@16
|
116 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value)
|
Chris@16
|
117 {
|
Chris@16
|
118 typedef typename DescriptorT::value_type value_type;
|
Chris@16
|
119 return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value));
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 //! \overload
|
Chris@16
|
123 template< typename DescriptorT, template< typename > class ActorT >
|
Chris@16
|
124 inline add_value_manip< typename DescriptorT::value_type& >
|
Chris@16
|
125 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value)
|
Chris@16
|
126 {
|
Chris@16
|
127 return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value);
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@101
|
130 //! \overload
|
Chris@101
|
131 template< typename DescriptorT, template< typename > class ActorT >
|
Chris@101
|
132 inline add_value_manip< typename DescriptorT::value_type const& >
|
Chris@101
|
133 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
|
Chris@101
|
134 {
|
Chris@101
|
135 return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
|
Chris@101
|
136 }
|
Chris@101
|
137
|
Chris@101
|
138 #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@16
|
139
|
Chris@16
|
140 template< typename T >
|
Chris@16
|
141 inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value)
|
Chris@16
|
142 {
|
Chris@16
|
143 return add_value_manip< T const& >(name, value);
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 template< typename DescriptorT, template< typename > class ActorT >
|
Chris@16
|
147 inline add_value_manip< typename DescriptorT::value_type const& >
|
Chris@16
|
148 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
|
Chris@16
|
149 {
|
Chris@16
|
150 return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@101
|
153 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@16
|
154
|
Chris@16
|
155 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
156
|
Chris@16
|
157 } // namespace boost
|
Chris@16
|
158
|
Chris@16
|
159 #ifdef _MSC_VER
|
Chris@16
|
160 #pragma warning(pop)
|
Chris@16
|
161 #endif
|
Chris@16
|
162
|
Chris@16
|
163 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
164
|
Chris@16
|
165 #endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
|