add_value.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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/type_traits/remove_cv.hpp>
  17. #include <boost/type_traits/remove_reference.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/embedded_string_type.hpp>
  20. #include <boost/log/attributes/attribute_name.hpp>
  21. #include <boost/log/attributes/attribute_value_impl.hpp>
  22. #include <boost/log/expressions/keyword_fwd.hpp>
  23. #include <boost/log/sources/record_ostream.hpp>
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. #ifdef _MSC_VER
  29. #pragma warning(push)
  30. // '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
  31. // 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.
  32. #pragma warning(disable: 4413)
  33. // returning address of local variable or temporary
  34. // 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.
  35. #pragma warning(disable: 4172)
  36. #endif
  37. namespace boost {
  38. BOOST_LOG_OPEN_NAMESPACE
  39. //! Attribute value manipulator
  40. template< typename RefT >
  41. class add_value_manip
  42. {
  43. public:
  44. //! Stored reference type
  45. typedef RefT reference_type;
  46. //! Attribute value type
  47. typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type;
  48. private:
  49. // The stored reference type is always an lvalue reference since apparently different compilers (GCC and MSVC) have different quirks when rvalue references are stored as members
  50. typedef typename remove_reference< reference_type >::type& stored_reference_type;
  51. private:
  52. //! Attribute value
  53. stored_reference_type m_value;
  54. //! Attribute name
  55. attribute_name m_name;
  56. public:
  57. //! Initializing constructor
  58. add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< stored_reference_type >(value)), m_name(name)
  59. {
  60. }
  61. //! Returns attribute name
  62. attribute_name get_name() const { return m_name; }
  63. //! Returns attribute value
  64. reference_type get_value() const { return static_cast< reference_type >(m_value); }
  65. };
  66. //! The operator attaches an attribute value to the log record
  67. template< typename CharT, typename RefT >
  68. inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip)
  69. {
  70. typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type;
  71. attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value()));
  72. strm.get_record().attribute_values().insert(manip.get_name(), value);
  73. return strm;
  74. }
  75. //! The function creates a manipulator that attaches an attribute value to a log record
  76. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1600)
  77. template< typename T >
  78. inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value)
  79. {
  80. return add_value_manip< T&& >(name, static_cast< T&& >(value));
  81. }
  82. //! \overload
  83. template< typename DescriptorT, template< typename > class ActorT >
  84. inline add_value_manip< typename DescriptorT::value_type&& >
  85. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value)
  86. {
  87. typedef typename DescriptorT::value_type value_type;
  88. return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value));
  89. }
  90. //! \overload
  91. template< typename DescriptorT, template< typename > class ActorT >
  92. inline add_value_manip< typename DescriptorT::value_type& >
  93. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value)
  94. {
  95. return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value);
  96. }
  97. #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1600)
  98. template< typename T >
  99. inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value)
  100. {
  101. return add_value_manip< T const& >(name, value);
  102. }
  103. template< typename DescriptorT, template< typename > class ActorT >
  104. inline add_value_manip< typename DescriptorT::value_type const& >
  105. add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value)
  106. {
  107. return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value);
  108. }
  109. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1600)
  110. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  111. } // namespace boost
  112. #ifdef _MSC_VER
  113. #pragma warning(pop)
  114. #endif
  115. #include <boost/log/detail/footer.hpp>
  116. #endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_