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)
8 * \file type_dispatcher.hpp
9 * \author Andrey Semashev
12 * The header contains definition of generic type dispatcher interfaces.
15 #ifndef BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_
16 #define BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_
18 #include <boost/type_index.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/core/explicit_operator_bool.hpp>
21 #include <boost/log/detail/config.hpp>
22 #include <boost/log/detail/header.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
30 BOOST_LOG_OPEN_NAMESPACE
33 * \brief A type dispatcher interface
35 * All type dispatchers support this interface. It is used to acquire the
36 * visitor interface for the requested type.
42 #ifndef BOOST_LOG_DOXYGEN_PASS
44 //! The base class for type dispatcher callbacks
52 explicit callback_base(void* visitor = 0, void* tramp = 0) BOOST_NOEXCEPT :
57 template< typename ValueT >
58 explicit callback_base(void* visitor, void (*tramp)(void*, ValueT const&)) BOOST_NOEXCEPT :
61 typedef void (*trampoline_t)(void*, ValueT const&);
62 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");
66 trampoline_t as_trampoline;
69 caster.as_trampoline = tramp;
70 m_pTrampoline = caster.as_pvoid;
73 template< typename VisitorT, typename T >
74 static void trampoline(void* visitor, T const& value)
76 (*static_cast< VisitorT* >(visitor))(value);
80 //! An interface to the callback for the concrete type visitor
81 template< typename T >
86 //! Type of the trampoline method
87 typedef void (*trampoline_t)(void*, T const&);
90 //! The type, which the visitor is able to consume
91 typedef T supported_type;
94 callback() BOOST_NOEXCEPT : callback_base()
97 explicit callback(callback_base const& base) BOOST_NOEXCEPT : callback_base(base)
101 void operator() (T const& value) const
103 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");
107 trampoline_t as_trampoline;
110 caster.as_pvoid = this->m_pTrampoline;
111 (caster.as_trampoline)(this->m_pVisitor, value);
114 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
116 bool operator! () const BOOST_NOEXCEPT { return (this->m_pVisitor == 0); }
119 #else // BOOST_LOG_DOXYGEN_PASS
122 * This interface is used by type dispatchers to consume the dispatched value.
124 template< typename T >
129 * The operator invokes the visitor-specific logic with the given value
131 * \param value The dispatched value
133 void operator() (T const& value) const;
136 * The operator checks if the visitor is attached to a receiver
138 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
141 * The operator checks if the visitor is not attached to a receiver
143 bool operator! () const BOOST_NOEXCEPT;
146 #endif // BOOST_LOG_DOXYGEN_PASS
149 //! Pointer to the callback acquisition method
150 typedef callback_base (*get_callback_impl_type)(type_dispatcher*, typeindex::type_index);
153 //! Pointer to the callback acquisition method
154 get_callback_impl_type m_get_callback_impl;
158 * Initializing constructor
160 explicit type_dispatcher(get_callback_impl_type get_callback_impl) BOOST_NOEXCEPT : m_get_callback_impl(get_callback_impl)
164 // Destructor and copying can only be called from the derived classes
165 BOOST_DEFAULTED_FUNCTION(~type_dispatcher(), {})
166 BOOST_DEFAULTED_FUNCTION(type_dispatcher(type_dispatcher const& that), : m_get_callback_impl(that.m_get_callback_impl) {})
167 BOOST_DEFAULTED_FUNCTION(type_dispatcher& operator= (type_dispatcher const& that), { m_get_callback_impl = that.m_get_callback_impl; return *this; })
171 * The method requests a callback for the value of type \c T
173 * \return The type-specific callback or an empty value, if the type is not supported
175 template< typename T >
176 callback< T > get_callback()
178 return callback< T >((this->m_get_callback_impl)(this, typeindex::type_id< T >()));
182 BOOST_LOG_CLOSE_NAMESPACE // namespace log
186 #include <boost/log/detail/footer.hpp>
188 #endif // BOOST_LOG_TYPE_DISPATCHER_HPP_INCLUDED_