attribute_value_impl.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 attribute_value_impl.hpp
  9. * \author Andrey Semashev
  10. * \date 24.06.2007
  11. *
  12. * The header contains an implementation of a basic attribute value implementation class.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_
  16. #include <boost/type_index.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/type_traits/remove_cv.hpp>
  20. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/attributes/attribute_value.hpp>
  23. #include <boost/log/utility/type_dispatch/type_dispatcher.hpp>
  24. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  25. #include <boost/type_traits/remove_reference.hpp>
  26. #endif
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace attributes {
  34. /*!
  35. * \brief Basic attribute value implementation class
  36. *
  37. * This class can be used as a boilerplate for simple attribute values. The class implements all needed
  38. * interfaces of attribute values and allows to store a single value of the type specified as a template parameter.
  39. * The stored value can be dispatched with type dispatching mechanism.
  40. */
  41. template< typename T >
  42. class attribute_value_impl :
  43. public attribute_value::impl
  44. {
  45. public:
  46. //! Value type
  47. typedef T value_type;
  48. private:
  49. //! Attribute value
  50. const value_type m_value;
  51. public:
  52. /*!
  53. * Constructor with initialization of the stored value
  54. */
  55. explicit attribute_value_impl(value_type const& v) : m_value(v) {}
  56. /*!
  57. * Constructor with initialization of the stored value
  58. */
  59. explicit attribute_value_impl(BOOST_RV_REF(value_type) v) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< value_type >::value) :
  60. m_value(boost::move(v))
  61. {
  62. }
  63. /*!
  64. * Attribute value dispatching method.
  65. *
  66. * \param dispatcher The dispatcher that receives the stored value
  67. *
  68. * \return \c true if the value has been dispatched, \c false otherwise
  69. */
  70. bool dispatch(type_dispatcher& dispatcher) BOOST_OVERRIDE
  71. {
  72. type_dispatcher::callback< value_type > callback = dispatcher.get_callback< value_type >();
  73. if (callback)
  74. {
  75. callback(m_value);
  76. return true;
  77. }
  78. else
  79. return false;
  80. }
  81. /*!
  82. * \return The attribute value type
  83. */
  84. typeindex::type_index get_type() const BOOST_OVERRIDE { return typeindex::type_id< value_type >(); }
  85. /*!
  86. * \return Reference to the contained value.
  87. */
  88. value_type const& get() const { return m_value; }
  89. };
  90. /*!
  91. * The function creates an attribute value from the specified object.
  92. */
  93. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  94. template< typename T >
  95. inline attribute_value make_attribute_value(T&& v)
  96. {
  97. typedef typename remove_cv< typename remove_reference< T >::type >::type value_type;
  98. return attribute_value(new attribute_value_impl< value_type >(boost::forward< T >(v)));
  99. }
  100. #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  101. template< typename T >
  102. inline attribute_value make_attribute_value(T const& v)
  103. {
  104. typedef typename remove_cv< T >::type value_type;
  105. return attribute_value(new attribute_value_impl< value_type >(v));
  106. }
  107. template< typename T >
  108. inline attribute_value make_attribute_value(rv< T > const& v)
  109. {
  110. typedef typename remove_cv< T >::type value_type;
  111. return attribute_value(new attribute_value_impl< value_type >(v));
  112. }
  113. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  114. } // namespace attributes
  115. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  116. } // namespace boost
  117. #include <boost/log/detail/footer.hpp>
  118. #endif // BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_IMPL_HPP_INCLUDED_