constant.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 constant.hpp
  9. * \author Andrey Semashev
  10. * \date 15.04.2007
  11. *
  12. * The header contains implementation of a constant attribute.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_
  16. #include <boost/move/core.hpp>
  17. #include <boost/move/utility_core.hpp>
  18. #include <boost/type_traits/remove_reference.hpp>
  19. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  20. #include <boost/log/detail/config.hpp>
  21. #include <boost/log/detail/embedded_string_type.hpp>
  22. #include <boost/log/attributes/attribute.hpp>
  23. #include <boost/log/attributes/attribute_cast.hpp>
  24. #include <boost/log/attributes/attribute_value_impl.hpp>
  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 A class of an attribute that holds a single constant value
  34. *
  35. * The constant is a simplest and one of the most frequently used types of attributes.
  36. * It stores a constant value, which it eventually returns as its value each time
  37. * requested.
  38. */
  39. template< typename T >
  40. class constant :
  41. public attribute
  42. {
  43. public:
  44. //! Attribute value type
  45. typedef T value_type;
  46. protected:
  47. //! Factory implementation
  48. class BOOST_SYMBOL_VISIBLE impl :
  49. public attribute_value_impl< value_type >
  50. {
  51. //! Base type
  52. typedef attribute_value_impl< value_type > base_type;
  53. public:
  54. /*!
  55. * Constructor with the stored value initialization
  56. */
  57. explicit impl(value_type const& value) : base_type(value) {}
  58. /*!
  59. * Constructor with the stored value initialization
  60. */
  61. explicit impl(BOOST_RV_REF(value_type) value) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< value_type >::value) :
  62. base_type(boost::move(value))
  63. {
  64. }
  65. };
  66. public:
  67. /*!
  68. * Constructor with the stored value initialization
  69. */
  70. explicit constant(value_type const& value) : attribute(new impl(value)) {}
  71. /*!
  72. * Constructor with the stored value initialization
  73. */
  74. explicit constant(BOOST_RV_REF(value_type) value) : attribute(new impl(boost::move(value))) {}
  75. /*!
  76. * Constructor for casting support
  77. */
  78. explicit constant(cast_source const& source) : attribute(source.as< impl >())
  79. {
  80. }
  81. /*!
  82. * \return Reference to the contained value.
  83. */
  84. value_type const& get() const
  85. {
  86. return static_cast< impl* >(this->get_impl())->get();
  87. }
  88. };
  89. /*!
  90. * The function constructs a \c constant attribute containing the provided value.
  91. * The function automatically converts C string arguments to \c std::basic_string objects.
  92. */
  93. template< typename T >
  94. inline constant<
  95. typename boost::log::aux::make_embedded_string_type<
  96. typename remove_reference< T >::type
  97. >::type
  98. > make_constant(BOOST_FWD_REF(T) val)
  99. {
  100. typedef typename boost::log::aux::make_embedded_string_type<
  101. typename remove_reference< T >::type
  102. >::type value_type;
  103. return constant< value_type >(boost::forward< T >(val));
  104. }
  105. } // namespace attributes
  106. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  107. } // namespace boost
  108. #include <boost/log/detail/footer.hpp>
  109. #endif // BOOST_LOG_ATTRIBUTES_CONSTANT_HPP_INCLUDED_