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)
12 #include <boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp>
14 namespace logging
= boost::log
;
16 // Base interface for the custom opaque value
19 virtual ~my_value_base() {}
20 virtual bool dispatch(logging::type_dispatcher
& dispatcher
) const = 0;
23 // A simple attribute value
24 template< typename T
>
30 explicit my_value(T
const& value
) : m_value(value
) {}
32 // The function passes the contained type into the dispatcher
33 bool dispatch(logging::type_dispatcher
& dispatcher
) const
35 logging::type_dispatcher::callback
< T
> cb
= dispatcher
.get_callback
< T
>();
46 //[ example_util_dynamic_type_dispatcher
47 // Visitor functions for the supported types
48 void on_int(int const& value
)
50 std::cout
<< "Received int value = " << value
<< std::endl
;
53 void on_double(double const& value
)
55 std::cout
<< "Received double value = " << value
<< std::endl
;
58 void on_string(std::string
const& value
)
60 std::cout
<< "Received string value = " << value
<< std::endl
;
63 logging::dynamic_type_dispatcher disp
;
65 // The function initializes the dispatcher object
68 // Register type visitors
69 disp
.register_type
< int >(&on_int
);
70 disp
.register_type
< double >(&on_double
);
71 disp
.register_type
< std::string
>(&on_string
);
74 // Prints the supplied value
75 bool print(my_value_base
const& val
)
77 return val
.dispatch(disp
);
81 int main(int, char*[])
85 // These two attributes are supported by the dispatcher
86 bool res
= print(my_value
< std::string
>("Hello world!"));
89 res
= print(my_value
< double >(1.2));
93 res
= print(my_value
< float >(-4.3f
));