Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / log / test / run / util_static_type_disp.cpp
1 /*
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)
6  */
7 /*!
8  * \file   util_static_type_disp.cpp
9  * \author Andrey Semashev
10  * \date   09.01.2009
11  *
12  * \brief  This header contains tests for the static type dispatcher.
13  */
14
15 #define BOOST_TEST_MODULE util_static_type_disp
16
17 #include <string>
18 #include <typeinfo>
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>
23
24 namespace logging = boost::log;
25
26 namespace {
27
28     // A simple attribute value
29     template< typename T >
30     struct my_value
31     {
32         T m_Value;
33
34         explicit my_value(T const& value) : m_Value(value) {}
35
36         // The function passes the contained type into the dispatcher
37         bool dispatch(logging::type_dispatcher& dispatcher)
38         {
39             logging::type_dispatcher::callback< T > callback = dispatcher.get_callback< T >();
40             if (callback)
41             {
42                 callback(m_Value);
43                 return true;
44             }
45             else
46                 return false;
47         }
48     };
49
50     // The function tests general functionality of the type dispatcher
51     template< typename DispatcherT >
52     void test_general_functionality(DispatcherT& disp)
53     {
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));
58
59         my_value< double > val2(1.2);
60         disp.set_expected(val2.m_Value);
61         BOOST_CHECK(val2.dispatch(disp));
62
63         // This one is not
64         my_value< float > val3(static_cast< float >(-4.3));
65         disp.set_expected();
66         BOOST_CHECK(!val3.dispatch(disp));
67     }
68
69
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 >
74         >
75     {
76         typedef logging::static_type_dispatcher<
77             boost::mpl::vector< int, double, std::string >
78         > base_type;
79
80         enum type_expected
81         {
82             none_expected,
83             int_expected,
84             double_expected,
85             string_expected
86         };
87
88         my_dispatcher() :
89             base_type(*this),
90             m_Expected(none_expected),
91             m_Int(0),
92             m_Double(0.0)
93         {
94         }
95
96         void set_expected()
97         {
98             m_Expected = none_expected;
99         }
100         void set_expected(int value)
101         {
102             m_Expected = int_expected;
103             m_Int = value;
104         }
105         void set_expected(double value)
106         {
107             m_Expected = double_expected;
108             m_Double = value;
109         }
110         void set_expected(std::string const& value)
111         {
112             m_Expected = string_expected;
113             m_String = value;
114         }
115
116         // Implement visitation logic for all supported types
117         void operator() (int const& value) const
118         {
119             BOOST_CHECK_EQUAL(m_Expected, int_expected);
120             BOOST_CHECK_EQUAL(m_Int, value);
121         }
122         void operator() (double const& value) const
123         {
124             BOOST_CHECK_EQUAL(m_Expected, double_expected);
125             BOOST_CHECK_CLOSE(m_Double, value, 0.001);
126         }
127         void operator() (std::string const& value) const
128         {
129             BOOST_CHECK_EQUAL(m_Expected, string_expected);
130             BOOST_CHECK_EQUAL(m_String, value);
131         }
132
133     private:
134         type_expected m_Expected;
135         int m_Int;
136         double m_Double;
137         std::string m_String;
138     };
139
140 } // namespace
141
142 // The test checks that general functionality works
143 BOOST_AUTO_TEST_CASE(type_dispatch)
144 {
145     my_dispatcher disp;
146     test_general_functionality(disp);
147 }