Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file add_value.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 26.11.2012 Chris@16: * Chris@16: * This header contains the \c add_value manipulator. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_ Chris@16: Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma warning(push) Chris@16: // 'boost::log::v2s_mt_nt6::add_value_manip::m_value' : reference member is initialized to a temporary that doesn't persist after the constructor exits Chris@16: // 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: #pragma warning(disable: 4413) Chris@16: // returning address of local variable or temporary Chris@16: // This warning refers to add_value_manip::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: #pragma warning(disable: 4172) Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: //! Attribute value manipulator Chris@16: template< typename RefT > Chris@16: class add_value_manip Chris@16: { Chris@16: public: Chris@16: //! Stored reference type Chris@16: typedef RefT reference_type; Chris@16: //! Attribute value type Chris@16: typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type; Chris@16: Chris@16: private: Chris@101: // 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: // 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: // To work around this problem we save the value inside the manipulator in this case. Chris@101: typedef typename remove_reference< reference_type >::type& lvalue_reference_type; Chris@101: Chris@101: typedef typename mpl::if_< Chris@101: is_scalar< value_type >, Chris@101: value_type, Chris@101: lvalue_reference_type Chris@101: >::type stored_type; Chris@101: Chris@101: typedef typename mpl::if_< Chris@101: is_scalar< value_type >, Chris@101: value_type, Chris@101: reference_type Chris@101: >::type get_value_result_type; Chris@16: Chris@16: private: Chris@16: //! Attribute value Chris@101: stored_type m_value; Chris@16: //! Attribute name Chris@16: attribute_name m_name; Chris@16: Chris@16: public: Chris@16: //! Initializing constructor Chris@101: add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< lvalue_reference_type >(value)), m_name(name) Chris@16: { Chris@16: } Chris@16: Chris@16: //! Returns attribute name Chris@16: attribute_name get_name() const { return m_name; } Chris@16: //! Returns attribute value Chris@101: get_value_result_type get_value() const { return static_cast< get_value_result_type >(m_value); } Chris@16: }; Chris@16: Chris@16: //! The operator attaches an attribute value to the log record Chris@16: template< typename CharT, typename RefT > Chris@16: inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip) Chris@16: { Chris@16: typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type; Chris@16: attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value())); Chris@16: strm.get_record().attribute_values().insert(manip.get_name(), value); Chris@16: return strm; Chris@16: } Chris@16: Chris@16: //! The function creates a manipulator that attaches an attribute value to a log record Chris@101: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: Chris@16: template< typename T > Chris@16: inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value) Chris@16: { Chris@16: return add_value_manip< T&& >(name, static_cast< T&& >(value)); Chris@16: } Chris@16: Chris@16: //! \overload Chris@16: template< typename DescriptorT, template< typename > class ActorT > Chris@16: inline add_value_manip< typename DescriptorT::value_type&& > Chris@16: add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value) Chris@16: { Chris@16: typedef typename DescriptorT::value_type value_type; Chris@16: return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value)); Chris@16: } Chris@16: Chris@16: //! \overload Chris@16: template< typename DescriptorT, template< typename > class ActorT > Chris@16: inline add_value_manip< typename DescriptorT::value_type& > Chris@16: add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value) Chris@16: { Chris@16: return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value); Chris@16: } Chris@16: Chris@101: //! \overload Chris@101: template< typename DescriptorT, template< typename > class ActorT > Chris@101: inline add_value_manip< typename DescriptorT::value_type const& > Chris@101: add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value) Chris@101: { Chris@101: return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value); Chris@101: } Chris@101: Chris@101: #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: Chris@16: template< typename T > Chris@16: inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value) Chris@16: { Chris@16: return add_value_manip< T const& >(name, value); Chris@16: } Chris@16: Chris@16: template< typename DescriptorT, template< typename > class ActorT > Chris@16: inline add_value_manip< typename DescriptorT::value_type const& > Chris@16: add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value) Chris@16: { Chris@16: return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value); Chris@16: } Chris@16: Chris@101: #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_