attribute_value_impl.hpp 3.8 KB

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