attribute_value_impl.hpp 3.8 KB

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