Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:27

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   add_value.hpp
0009  * \author Andrey Semashev
0010  * \date   26.11.2012
0011  *
0012  * This header contains the \c add_value manipulator.
0013  */
0014 
0015 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
0016 #define BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
0017 
0018 #include <boost/type_traits/is_scalar.hpp>
0019 #include <boost/type_traits/remove_cv.hpp>
0020 #include <boost/type_traits/remove_reference.hpp>
0021 #include <boost/type_traits/conditional.hpp>
0022 #include <boost/log/detail/config.hpp>
0023 #include <boost/log/detail/embedded_string_type.hpp>
0024 #include <boost/log/attributes/attribute_name.hpp>
0025 #include <boost/log/attributes/attribute_value_impl.hpp>
0026 #include <boost/log/expressions/keyword_fwd.hpp>
0027 #include <boost/log/sources/record_ostream.hpp>
0028 #include <boost/log/detail/header.hpp>
0029 
0030 #ifdef BOOST_HAS_PRAGMA_ONCE
0031 #pragma once
0032 #endif
0033 
0034 #ifdef _MSC_VER
0035 #pragma warning(push)
0036 // '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
0037 // 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.
0038 #pragma warning(disable: 4413)
0039 // returning address of local variable or temporary
0040 // 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.
0041 #pragma warning(disable: 4172)
0042 #endif
0043 
0044 namespace boost {
0045 
0046 BOOST_LOG_OPEN_NAMESPACE
0047 
0048 //! Attribute value manipulator
0049 template< typename RefT >
0050 class add_value_manip
0051 {
0052 public:
0053     //! Stored reference type
0054     typedef RefT reference_type;
0055     //! Attribute value type
0056     typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type;
0057 
0058 private:
0059     //  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.
0060     //  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.
0061     //  To work around this problem we save the value inside the manipulator in this case.
0062     typedef typename remove_reference< reference_type >::type& lvalue_reference_type;
0063 
0064     typedef typename conditional<
0065         is_scalar< value_type >::value,
0066         value_type,
0067         lvalue_reference_type
0068     >::type stored_type;
0069 
0070     typedef typename conditional<
0071         is_scalar< value_type >::value,
0072         value_type,
0073         reference_type
0074     >::type get_value_result_type;
0075 
0076 private:
0077     //! Attribute value
0078     stored_type m_value;
0079     //! Attribute name
0080     attribute_name m_name;
0081 
0082 public:
0083     //! Initializing constructor
0084     add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< lvalue_reference_type >(value)), m_name(name)
0085     {
0086     }
0087 
0088     //! Returns attribute name
0089     attribute_name get_name() const { return m_name; }
0090     //! Returns attribute value
0091     get_value_result_type get_value() const { return static_cast< get_value_result_type >(m_value); }
0092 };
0093 
0094 //! The operator attaches an attribute value to the log record
0095 template< typename CharT, typename RefT >
0096 inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip)
0097 {
0098     typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type;
0099     attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value()));
0100     strm.get_record().attribute_values().insert(manip.get_name(), value);
0101     return strm;
0102 }
0103 
0104 //! The function creates a manipulator that attaches an attribute value to a log record
0105 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0106 
0107 template< typename T >
0108 inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value)
0109 {
0110     return add_value_manip< T&& >(name, static_cast< T&& >(value));
0111 }
0112 
0113 //! \overload
0114 template< typename DescriptorT, template< typename > class ActorT >
0115 inline add_value_manip< typename DescriptorT::value_type&& >
0116 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value)
0117 {
0118     typedef typename DescriptorT::value_type value_type;
0119     return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value));
0120 }
0121 
0122 //! \overload
0123 template< typename DescriptorT, template< typename > class ActorT >
0124 inline add_value_manip< typename DescriptorT::value_type& >
0125 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value)
0126 {
0127     return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value);
0128 }
0129 
0130 //! \overload
0131 template< typename DescriptorT, template< typename > class ActorT >
0132 inline add_value_manip< typename DescriptorT::value_type const& >
0133 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
0134 {
0135     return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
0136 }
0137 
0138 #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0139 
0140 template< typename T >
0141 inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value)
0142 {
0143     return add_value_manip< T const& >(name, value);
0144 }
0145 
0146 template< typename DescriptorT, template< typename > class ActorT >
0147 inline add_value_manip< typename DescriptorT::value_type const& >
0148 add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
0149 {
0150     return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
0151 }
0152 
0153 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0154 
0155 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0156 
0157 } // namespace boost
0158 
0159 #ifdef _MSC_VER
0160 #pragma warning(pop)
0161 #endif
0162 
0163 #include <boost/log/detail/footer.hpp>
0164 
0165 #endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_