attribute.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.hpp
  9. * \author Andrey Semashev
  10. * \date 15.04.2007
  11. *
  12. * The header contains attribute interface definition.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_ATTRIBUTE_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_ATTRIBUTE_HPP_INCLUDED_
  16. #include <new>
  17. #include <boost/move/core.hpp>
  18. #include <boost/smart_ptr/intrusive_ptr.hpp>
  19. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  20. #include <boost/core/explicit_operator_bool.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. BOOST_LOG_OPEN_NAMESPACE
  28. #ifndef BOOST_LOG_DOXYGEN_PASS
  29. class attribute_value;
  30. namespace aux {
  31. //! Reference proxy object to implement \c operator[]
  32. class attribute_set_reference_proxy;
  33. } // namespace aux
  34. #endif // BOOST_LOG_DOXYGEN_PASS
  35. /*!
  36. * \brief A base class for an attribute value factory
  37. *
  38. * Every attribute is represented with a factory that is basically an attribute value generator.
  39. * The sole purpose of an attribute is to return an actual value when requested. A simplest attribute
  40. * can always return the same value that it stores internally, but more complex ones can
  41. * perform a considerable amount of work to return a value, and the returned values may differ
  42. * each time requested.
  43. *
  44. * A word about thread safety. An attribute should be prepared to be requested a value from
  45. * multiple threads concurrently.
  46. */
  47. class attribute
  48. {
  49. BOOST_COPYABLE_AND_MOVABLE(attribute)
  50. public:
  51. /*!
  52. * \brief A base class for an attribute value factory
  53. *
  54. * All attributes must derive their implementation from this class.
  55. */
  56. struct BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl :
  57. public boost::intrusive_ref_counter< impl >
  58. {
  59. /*!
  60. * \brief Virtual destructor
  61. */
  62. virtual ~impl() {}
  63. /*!
  64. * \return The actual attribute value. It shall not return empty values (exceptions
  65. * shall be used to indicate errors).
  66. */
  67. virtual attribute_value get_value() = 0;
  68. BOOST_LOG_API static void* operator new (std::size_t size);
  69. BOOST_LOG_API static void operator delete (void* p, std::size_t size) BOOST_NOEXCEPT;
  70. };
  71. private:
  72. //! Pointer to the attribute factory implementation
  73. intrusive_ptr< impl > m_pImpl;
  74. public:
  75. /*!
  76. * Default constructor. Creates an empty attribute value factory, which is not usable until
  77. * \c set_impl is called.
  78. */
  79. BOOST_DEFAULTED_FUNCTION(attribute(), {})
  80. /*!
  81. * Copy constructor
  82. */
  83. attribute(attribute const& that) BOOST_NOEXCEPT : m_pImpl(that.m_pImpl) {}
  84. /*!
  85. * Move constructor
  86. */
  87. attribute(BOOST_RV_REF(attribute) that) BOOST_NOEXCEPT { m_pImpl.swap(that.m_pImpl); }
  88. /*!
  89. * Initializing constructor
  90. *
  91. * \param p Pointer to the implementation. Must not be \c NULL.
  92. */
  93. explicit attribute(intrusive_ptr< impl > p) BOOST_NOEXCEPT { m_pImpl.swap(p); }
  94. /*!
  95. * Copy assignment
  96. */
  97. attribute& operator= (BOOST_COPY_ASSIGN_REF(attribute) that) BOOST_NOEXCEPT
  98. {
  99. m_pImpl = that.m_pImpl;
  100. return *this;
  101. }
  102. /*!
  103. * Move assignment
  104. */
  105. attribute& operator= (BOOST_RV_REF(attribute) that) BOOST_NOEXCEPT
  106. {
  107. m_pImpl.swap(that.m_pImpl);
  108. return *this;
  109. }
  110. #ifndef BOOST_LOG_DOXYGEN_PASS
  111. attribute& operator= (aux::attribute_set_reference_proxy const& that) BOOST_NOEXCEPT;
  112. #endif
  113. /*!
  114. * Verifies that the factory is not in empty state
  115. */
  116. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  117. /*!
  118. * Verifies that the factory is in empty state
  119. */
  120. bool operator! () const BOOST_NOEXCEPT { return !m_pImpl; }
  121. /*!
  122. * \return The actual attribute value. It shall not return empty values (exceptions
  123. * shall be used to indicate errors).
  124. */
  125. attribute_value get_value() const;
  126. /*!
  127. * The method swaps two factories (i.e. their implementations).
  128. */
  129. void swap(attribute& that) BOOST_NOEXCEPT { m_pImpl.swap(that.m_pImpl); }
  130. protected:
  131. /*!
  132. * \returns The pointer to the implementation
  133. */
  134. impl* get_impl() const BOOST_NOEXCEPT { return m_pImpl.get(); }
  135. /*!
  136. * Sets the pointer to the factory implementation.
  137. *
  138. * \param p Pointer to the implementation. Must not be \c NULL.
  139. */
  140. void set_impl(intrusive_ptr< impl > p) BOOST_NOEXCEPT { m_pImpl.swap(p); }
  141. template< typename T >
  142. friend T attribute_cast(attribute const&);
  143. };
  144. /*!
  145. * The function swaps two attribute value factories
  146. */
  147. inline void swap(attribute& left, attribute& right) BOOST_NOEXCEPT
  148. {
  149. left.swap(right);
  150. }
  151. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  152. } // namespace boost
  153. #include <boost/log/detail/footer.hpp>
  154. #if defined(BOOST_LOG_ATTRIBUTES_ATTRIBUTE_VALUE_HPP_INCLUDED_)
  155. #include <boost/log/detail/attribute_get_value_impl.hpp>
  156. #endif
  157. #endif // BOOST_LOG_ATTRIBUTES_ATTRIBUTE_HPP_INCLUDED_