2 * Copyright Andrey Semashev 2007 - 2014.
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 util_static_type_disp.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for the static type dispatcher.
15 #define BOOST_TEST_MODULE util_static_type_disp
19 #include <boost/mpl/vector.hpp>
20 #include <boost/test/floating_point_comparison.hpp>
21 #include <boost/test/unit_test.hpp>
22 #include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
24 namespace logging = boost::log;
28 // A simple attribute value
29 template< typename T >
34 explicit my_value(T const& value) : m_Value(value) {}
36 // The function passes the contained type into the dispatcher
37 bool dispatch(logging::type_dispatcher& dispatcher)
39 logging::type_dispatcher::callback< T > callback = dispatcher.get_callback< T >();
50 // The function tests general functionality of the type dispatcher
51 template< typename DispatcherT >
52 void test_general_functionality(DispatcherT& disp)
54 // These two attributes are supported by the dispatcher
55 my_value< std::string > val1("Hello world!");
56 disp.set_expected(val1.m_Value);
57 BOOST_CHECK(val1.dispatch(disp));
59 my_value< double > val2(1.2);
60 disp.set_expected(val2.m_Value);
61 BOOST_CHECK(val2.dispatch(disp));
64 my_value< float > val3(static_cast< float >(-4.3));
66 BOOST_CHECK(!val3.dispatch(disp));
70 // Type dispatcher for the supported types
71 struct my_dispatcher :
72 public logging::static_type_dispatcher<
73 boost::mpl::vector< int, double, std::string >
76 typedef logging::static_type_dispatcher<
77 boost::mpl::vector< int, double, std::string >
90 m_Expected(none_expected),
98 m_Expected = none_expected;
100 void set_expected(int value)
102 m_Expected = int_expected;
105 void set_expected(double value)
107 m_Expected = double_expected;
110 void set_expected(std::string const& value)
112 m_Expected = string_expected;
116 // Implement visitation logic for all supported types
117 void operator() (int const& value) const
119 BOOST_CHECK_EQUAL(m_Expected, int_expected);
120 BOOST_CHECK_EQUAL(m_Int, value);
122 void operator() (double const& value) const
124 BOOST_CHECK_EQUAL(m_Expected, double_expected);
125 BOOST_CHECK_CLOSE(m_Double, value, 0.001);
127 void operator() (std::string const& value) const
129 BOOST_CHECK_EQUAL(m_Expected, string_expected);
130 BOOST_CHECK_EQUAL(m_String, value);
134 type_expected m_Expected;
137 std::string m_String;
142 // The test checks that general functionality works
143 BOOST_AUTO_TEST_CASE(type_dispatch)
146 test_general_functionality(disp);