type_dispatcher.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 type_dispatcher.hpp
  9. * \author Andrey Semashev
  10. * \date 15.04.2007
  11. *
  12. * The header contains definition of generic type dispatcher interfaces.
  13. */
  14. #ifndef BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_
  15. #define BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_
  16. #include <boost/type_index.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/core/explicit_operator_bool.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. /*!
  27. * \brief A type dispatcher interface
  28. *
  29. * All type dispatchers support this interface. It is used to acquire the
  30. * visitor interface for the requested type.
  31. */
  32. class type_dispatcher
  33. {
  34. public:
  35. #ifndef BOOST_LOG_DOXYGEN_PASS
  36. //! The base class for type dispatcher callbacks
  37. class callback_base
  38. {
  39. protected:
  40. void* m_pVisitor;
  41. void* m_pTrampoline;
  42. public:
  43. explicit callback_base(void* visitor = 0, void* tramp = 0) BOOST_NOEXCEPT :
  44. m_pVisitor(visitor),
  45. m_pTrampoline(tramp)
  46. {
  47. }
  48. template< typename ValueT >
  49. explicit callback_base(void* visitor, void (*tramp)(void*, ValueT const&)) BOOST_NOEXCEPT :
  50. m_pVisitor(visitor)
  51. {
  52. typedef void (*trampoline_t)(void*, ValueT const&);
  53. BOOST_STATIC_ASSERT_MSG(sizeof(trampoline_t) == sizeof(void*), "Boost.Log: Unsupported platform, the size of a function pointer differs from the size of a pointer");
  54. union
  55. {
  56. void* as_pvoid;
  57. trampoline_t as_trampoline;
  58. }
  59. caster;
  60. caster.as_trampoline = tramp;
  61. m_pTrampoline = caster.as_pvoid;
  62. }
  63. template< typename VisitorT, typename T >
  64. static void trampoline(void* visitor, T const& value)
  65. {
  66. (*static_cast< VisitorT* >(visitor))(value);
  67. }
  68. };
  69. //! An interface to the callback for the concrete type visitor
  70. template< typename T >
  71. class callback :
  72. private callback_base
  73. {
  74. private:
  75. //! Type of the trampoline method
  76. typedef void (*trampoline_t)(void*, T const&);
  77. public:
  78. //! The type, which the visitor is able to consume
  79. typedef T supported_type;
  80. public:
  81. callback() BOOST_NOEXCEPT : callback_base()
  82. {
  83. }
  84. explicit callback(callback_base const& base) BOOST_NOEXCEPT : callback_base(base)
  85. {
  86. }
  87. void operator() (T const& value) const
  88. {
  89. BOOST_STATIC_ASSERT_MSG(sizeof(trampoline_t) == sizeof(void*), "Boost.Log: Unsupported platform, the size of a function pointer differs from the size of a pointer");
  90. union
  91. {
  92. void* as_pvoid;
  93. trampoline_t as_trampoline;
  94. }
  95. caster;
  96. caster.as_pvoid = this->m_pTrampoline;
  97. (caster.as_trampoline)(this->m_pVisitor, value);
  98. }
  99. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  100. bool operator! () const BOOST_NOEXCEPT { return (this->m_pVisitor == 0); }
  101. };
  102. #else // BOOST_LOG_DOXYGEN_PASS
  103. /*!
  104. * This interface is used by type dispatchers to consume the dispatched value.
  105. */
  106. template< typename T >
  107. class callback
  108. {
  109. public:
  110. /*!
  111. * The operator invokes the visitor-specific logic with the given value
  112. *
  113. * \param value The dispatched value
  114. */
  115. void operator() (T const& value) const;
  116. /*!
  117. * The operator checks if the visitor is attached to a receiver
  118. */
  119. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  120. /*!
  121. * The operator checks if the visitor is not attached to a receiver
  122. */
  123. bool operator! () const BOOST_NOEXCEPT;
  124. };
  125. #endif // BOOST_LOG_DOXYGEN_PASS
  126. protected:
  127. //! Pointer to the callback acquisition method
  128. typedef callback_base (*get_callback_impl_type)(type_dispatcher*, typeindex::type_index);
  129. private:
  130. //! Pointer to the callback acquisition method
  131. get_callback_impl_type m_get_callback_impl;
  132. protected:
  133. /*!
  134. * Initializing constructor
  135. */
  136. explicit type_dispatcher(get_callback_impl_type get_callback_impl) BOOST_NOEXCEPT : m_get_callback_impl(get_callback_impl)
  137. {
  138. }
  139. // Destructor and copying can only be called from the derived classes
  140. BOOST_DEFAULTED_FUNCTION(~type_dispatcher(), {})
  141. BOOST_DEFAULTED_FUNCTION(type_dispatcher(type_dispatcher const& that), : m_get_callback_impl(that.m_get_callback_impl) {})
  142. BOOST_DEFAULTED_FUNCTION(type_dispatcher& operator= (type_dispatcher const& that), { m_get_callback_impl = that.m_get_callback_impl; return *this; })
  143. public:
  144. /*!
  145. * The method requests a callback for the value of type \c T
  146. *
  147. * \return The type-specific callback or an empty value, if the type is not supported
  148. */
  149. template< typename T >
  150. callback< T > get_callback()
  151. {
  152. return callback< T >((this->m_get_callback_impl)(this, typeindex::type_id< T >()));
  153. }
  154. };
  155. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  156. } // namespace boost
  157. #include <boost/log/detail/footer.hpp>
  158. #endif // BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_