static_type_dispatcher.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 static_type_dispatcher.hpp
  9. * \author Andrey Semashev
  10. * \date 15.04.2007
  11. *
  12. * The header contains implementation of a compile-time type dispatcher.
  13. */
  14. #ifndef BOOST_LOG_STATIC_TYPE_DISPATCHER_HPP_INCLUDED_
  15. #define BOOST_LOG_STATIC_TYPE_DISPATCHER_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <utility>
  18. #include <iterator>
  19. #include <algorithm>
  20. #include <boost/array.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/type_index.hpp>
  23. #include <boost/mpl/if.hpp>
  24. #include <boost/mpl/size.hpp>
  25. #include <boost/mpl/begin.hpp>
  26. #include <boost/mpl/end.hpp>
  27. #include <boost/mpl/deref.hpp>
  28. #include <boost/mpl/next.hpp>
  29. #include <boost/mpl/is_sequence.hpp>
  30. #include <boost/core/addressof.hpp>
  31. #include <boost/log/detail/config.hpp>
  32. #include <boost/log/utility/once_block.hpp>
  33. #include <boost/log/utility/type_dispatch/type_dispatcher.hpp>
  34. #include <boost/log/detail/header.hpp>
  35. #ifdef BOOST_HAS_PRAGMA_ONCE
  36. #pragma once
  37. #endif
  38. namespace boost {
  39. BOOST_LOG_OPEN_NAMESPACE
  40. namespace aux {
  41. //! Ordering predicate for type dispatching map
  42. struct dispatching_map_order
  43. {
  44. typedef bool result_type;
  45. typedef std::pair< typeindex::type_index, void* > first_argument_type, second_argument_type;
  46. result_type operator() (first_argument_type const& left, second_argument_type const& right) const
  47. {
  48. return (left.first < right.first);
  49. }
  50. };
  51. //! Dispatching map filler
  52. template< typename VisitorT >
  53. struct dispatching_map_initializer
  54. {
  55. template< typename IteratorT >
  56. static BOOST_FORCEINLINE void init(IteratorT*, IteratorT*, std::pair< typeindex::type_index, void* >*)
  57. {
  58. }
  59. template< typename BeginIteratorT, typename EndIteratorT >
  60. static BOOST_FORCEINLINE void init(BeginIteratorT*, EndIteratorT* end, std::pair< typeindex::type_index, void* >* p)
  61. {
  62. typedef typename mpl::deref< BeginIteratorT >::type type;
  63. do_init(static_cast< type* >(0), p);
  64. typedef typename mpl::next< BeginIteratorT >::type next_iterator_type;
  65. init(static_cast< next_iterator_type* >(0), end, p + 1);
  66. }
  67. private:
  68. template< typename T >
  69. static BOOST_FORCEINLINE void do_init(T*, std::pair< typeindex::type_index, void* >* p)
  70. {
  71. p->first = typeindex::type_id< T >();
  72. typedef void (*trampoline_t)(void*, T const&);
  73. 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");
  74. union
  75. {
  76. void* as_pvoid;
  77. trampoline_t as_trampoline;
  78. }
  79. caster;
  80. caster.as_trampoline = (trampoline_t)&type_dispatcher::callback_base::trampoline< VisitorT, T >;
  81. p->second = caster.as_pvoid;
  82. }
  83. };
  84. //! A base class for a dispatcher that supports a sequence of types
  85. class type_sequence_dispatcher_base :
  86. public type_dispatcher
  87. {
  88. private:
  89. //! Dispatching map element type
  90. typedef std::pair< typeindex::type_index, void* > dispatching_map_element_type;
  91. private:
  92. //! Dispatching map
  93. const dispatching_map_element_type* m_dispatching_map_begin;
  94. //! Dispatching map size
  95. std::size_t m_dispatching_map_size;
  96. //! Pointer to the receiver function
  97. void* m_visitor;
  98. protected:
  99. //! Initializing constructor
  100. type_sequence_dispatcher_base(const dispatching_map_element_type* disp_map, std::size_t disp_map_size, void* visitor) BOOST_NOEXCEPT :
  101. type_dispatcher(&type_sequence_dispatcher_base::get_callback),
  102. m_dispatching_map_begin(disp_map),
  103. m_dispatching_map_size(disp_map_size),
  104. m_visitor(visitor)
  105. {
  106. }
  107. private:
  108. //! The get_callback method implementation
  109. static callback_base get_callback(type_dispatcher* p, typeindex::type_index type)
  110. {
  111. type_sequence_dispatcher_base* const self = static_cast< type_sequence_dispatcher_base* >(p);
  112. const dispatching_map_element_type* begin = self->m_dispatching_map_begin;
  113. const dispatching_map_element_type* end = begin + self->m_dispatching_map_size;
  114. const dispatching_map_element_type* it = std::lower_bound
  115. (
  116. begin,
  117. end,
  118. dispatching_map_element_type(type, (void*)0),
  119. dispatching_map_order()
  120. );
  121. if (it != end && it->first == type)
  122. return callback_base(self->m_visitor, it->second);
  123. else
  124. return callback_base();
  125. }
  126. // Copying and assignment closed
  127. BOOST_DELETED_FUNCTION(type_sequence_dispatcher_base(type_sequence_dispatcher_base const&))
  128. BOOST_DELETED_FUNCTION(type_sequence_dispatcher_base& operator= (type_sequence_dispatcher_base const&))
  129. };
  130. //! A dispatcher that supports a sequence of types
  131. template< typename TypeSequenceT >
  132. class type_sequence_dispatcher :
  133. public type_sequence_dispatcher_base
  134. {
  135. public:
  136. //! Type sequence of the supported types
  137. typedef TypeSequenceT supported_types;
  138. private:
  139. //! The dispatching map
  140. typedef array<
  141. std::pair< typeindex::type_index, void* >,
  142. mpl::size< supported_types >::value
  143. > dispatching_map;
  144. public:
  145. /*!
  146. * Constructor. Initializes the dispatcher internals.
  147. */
  148. template< typename VisitorT >
  149. explicit type_sequence_dispatcher(VisitorT& visitor) :
  150. type_sequence_dispatcher_base(get_dispatching_map< VisitorT >().data(), dispatching_map::static_size, (void*)boost::addressof(visitor))
  151. {
  152. }
  153. private:
  154. //! The method returns the dispatching map instance
  155. template< typename VisitorT >
  156. static dispatching_map const& get_dispatching_map()
  157. {
  158. static const dispatching_map* pinstance = NULL;
  159. BOOST_LOG_ONCE_BLOCK()
  160. {
  161. static dispatching_map instance;
  162. typename dispatching_map::value_type* p = &*instance.begin();
  163. typedef typename mpl::begin< supported_types >::type begin_iterator_type;
  164. typedef typename mpl::end< supported_types >::type end_iterator_type;
  165. typedef dispatching_map_initializer< VisitorT > initializer;
  166. initializer::init(static_cast< begin_iterator_type* >(0), static_cast< end_iterator_type* >(0), p);
  167. std::sort(instance.begin(), instance.end(), dispatching_map_order());
  168. pinstance = &instance;
  169. }
  170. return *pinstance;
  171. }
  172. // Copying and assignment closed
  173. BOOST_DELETED_FUNCTION(type_sequence_dispatcher(type_sequence_dispatcher const&))
  174. BOOST_DELETED_FUNCTION(type_sequence_dispatcher& operator= (type_sequence_dispatcher const&))
  175. };
  176. //! A base class for a single-type dispatcher
  177. class single_type_dispatcher_base :
  178. public type_dispatcher
  179. {
  180. private:
  181. //! The type to match against
  182. typeindex::type_index m_type;
  183. //! A callback for the supported type
  184. callback_base m_callback;
  185. protected:
  186. //! Initializing constructor
  187. single_type_dispatcher_base(typeindex::type_index type, callback_base const& cb) BOOST_NOEXCEPT :
  188. type_dispatcher(&single_type_dispatcher_base::get_callback),
  189. m_type(type),
  190. m_callback(cb)
  191. {
  192. }
  193. private:
  194. //! The get_callback method implementation
  195. static callback_base get_callback(type_dispatcher* p, typeindex::type_index type)
  196. {
  197. single_type_dispatcher_base* const self = static_cast< single_type_dispatcher_base* >(p);
  198. if (type == self->m_type)
  199. return self->m_callback;
  200. else
  201. return callback_base();
  202. }
  203. // Copying and assignment closed
  204. BOOST_DELETED_FUNCTION(single_type_dispatcher_base(single_type_dispatcher_base const&))
  205. BOOST_DELETED_FUNCTION(single_type_dispatcher_base& operator= (single_type_dispatcher_base const&))
  206. };
  207. //! A simple dispatcher that only supports one type
  208. template< typename T >
  209. class single_type_dispatcher :
  210. public single_type_dispatcher_base
  211. {
  212. private:
  213. typedef void (*trampoline_t)(void*, T const&);
  214. public:
  215. //! Constructor
  216. template< typename VisitorT >
  217. explicit single_type_dispatcher(VisitorT& visitor) BOOST_NOEXCEPT :
  218. single_type_dispatcher_base(typeindex::type_id< T >(), callback_base((void*)boost::addressof(visitor), (trampoline_t)&callback_base::trampoline< VisitorT, T >))
  219. {
  220. }
  221. // Copying and assignment closed
  222. BOOST_DELETED_FUNCTION(single_type_dispatcher(single_type_dispatcher const&))
  223. BOOST_DELETED_FUNCTION(single_type_dispatcher& operator= (single_type_dispatcher const&))
  224. };
  225. } // namespace aux
  226. /*!
  227. * \brief A static type dispatcher class
  228. *
  229. * The type dispatcher can be used to pass objects of arbitrary types from one
  230. * component to another. With regard to the library, the type dispatcher
  231. * can be used to extract attribute values.
  232. *
  233. * Static type dispatchers allow to specify one or several supported types at compile
  234. * time.
  235. */
  236. template< typename T >
  237. class static_type_dispatcher
  238. #ifndef BOOST_LOG_DOXYGEN_PASS
  239. :
  240. public mpl::if_<
  241. mpl::is_sequence< T >,
  242. boost::log::aux::type_sequence_dispatcher< T >,
  243. boost::log::aux::single_type_dispatcher< T >
  244. >::type
  245. #endif
  246. {
  247. private:
  248. //! Base type
  249. typedef typename mpl::if_<
  250. mpl::is_sequence< T >,
  251. boost::log::aux::type_sequence_dispatcher< T >,
  252. boost::log::aux::single_type_dispatcher< T >
  253. >::type base_type;
  254. public:
  255. /*!
  256. * Constructor. Initializes the dispatcher internals.
  257. *
  258. * The \a receiver object is not copied inside the dispatcher, but references to
  259. * it may be kept by the dispatcher after construction. The receiver object must remain
  260. * valid until the dispatcher is destroyed.
  261. *
  262. * \param receiver Unary function object that will be called on a dispatched value. The receiver
  263. * must be callable with an argument of any of the supported types of the dispatcher.
  264. */
  265. template< typename ReceiverT >
  266. explicit static_type_dispatcher(ReceiverT& receiver) :
  267. base_type(receiver)
  268. {
  269. }
  270. // Copying and assignment prohibited
  271. BOOST_DELETED_FUNCTION(static_type_dispatcher(static_type_dispatcher const&))
  272. BOOST_DELETED_FUNCTION(static_type_dispatcher& operator= (static_type_dispatcher const&))
  273. };
  274. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  275. } // namespace boost
  276. #include <boost/log/detail/footer.hpp>
  277. #endif // BOOST_LOG_STATIC_TYPE_DISPATCHER_HPP_INCLUDED_