add_value.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 add_value.hpp
  9. * \author Andrey Semashev
  10. * \date 26.11.2012
  11. *
  12. * This header contains the \c add_value manipulator.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/type_traits/is_scalar.hpp>
  18. #include <boost/type_traits/remove_cv.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/log/detail/config.hpp>
  21. #include <boost/log/detail/embedded_string_type.hpp>
  22. #include <boost/log/attributes/attribute_name.hpp>
  23. #include <boost/log/attributes/attribute_value_impl.hpp>
  24. #include <boost/log/expressions/keyword_fwd.hpp>
  25. #include <boost/log/sources/record_ostream.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. #ifdef _MSC_VER
  31. #pragma warning(push)
  32. // '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
  33. // 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.
  34. #pragma warning(disable: 4413)
  35. // returning address of local variable or temporary
  36. // 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.
  37. #pragma warning(disable: 4172)
  38. #endif
  39. namespace boost {
  40. BOOST_LOG_OPEN_NAMESPACE
  41. //! Attribute value manipulator
  42. template< typename RefT >
  43. class add_value_manip
  44. {
  45. public:
  46. //! Stored reference type
  47. typedef RefT reference_type;
  48. //! Attribute value type
  49. typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type;
  50. private:
  51. // 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.
  52. // 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.
  53. // To work around this problem we save the value inside the manipulator in this case.
  54. typedef typename remove_reference< reference_type >::type& lvalue_reference_type;
  55. typedef typename mpl::if_<
  56. is_scalar< value_type >,
  57. value_type,
  58. lvalue_reference_type
  59. >::type stored_type;
  60. typedef typename mpl::if_<
  61. is_scalar< value_type >,
  62. value_type,
  63. reference_type
  64. >::type get_value_result_type;
  65. private:
  66. //! Attribute value
  67. stored_type m_value;
  68. //! Attribute name
  69. attribute_name m_name;
  70. public:
  71. //! Initializing constructor
  72. add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< lvalue_reference_type >(value)), m_name(name)
  73. {
  74. }
  75. //! Returns attribute name
  76. attribute_name get_name() const { return m_name; }
  77. //! Returns attribute value
  78. get_value_result_type get_value() const { return static_cast< get_value_result_type >(m_value); }
  79. };
  80. //! The operator attaches an attribute value to the log record
  81. template< typename CharT, typename RefT >
  82. inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip)
  83. {
  84. typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type;
  85. attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value()));
  86. strm.get_record().attribute_values().insert(manip.get_name(), value);
  87. return strm;
  88. }
  89. //! The function creates a manipulator that attaches an attribute value to a log record
  90. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  91. template< typename T >
  92. inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value)
  93. {
  94. return add_value_manip< T&& >(name, static_cast< T&& >(value));
  95. }
  96. //! \overload
  97. template< typename DescriptorT, template< typename > class ActorT >
  98. inline add_value_manip< typename DescriptorT::value_type&& >
  99. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value)
  100. {
  101. typedef typename DescriptorT::value_type value_type;
  102. return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value));
  103. }
  104. //! \overload
  105. template< typename DescriptorT, template< typename > class ActorT >
  106. inline add_value_manip< typename DescriptorT::value_type& >
  107. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value)
  108. {
  109. return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value);
  110. }
  111. //! \overload
  112. template< typename DescriptorT, template< typename > class ActorT >
  113. inline add_value_manip< typename DescriptorT::value_type const& >
  114. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
  115. {
  116. return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
  117. }
  118. #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  119. template< typename T >
  120. inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value)
  121. {
  122. return add_value_manip< T const& >(name, value);
  123. }
  124. template< typename DescriptorT, template< typename > class ActorT >
  125. inline add_value_manip< typename DescriptorT::value_type const& >
  126. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
  127. {
  128. return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
  129. }
  130. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  131. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  132. } // namespace boost
  133. #ifdef _MSC_VER
  134. #pragma warning(pop)
  135. #endif
  136. #include <boost/log/detail/footer.hpp>
  137. #endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_