]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/util_static_type_disp.cpp
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/mpl/vector.hpp>
13 #include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
15 namespace logging
= boost::log
;
17 //[ example_util_static_type_dispatcher
18 // Base interface for the custom opaque value
21 virtual ~my_value_base() {}
22 virtual bool dispatch(logging::type_dispatcher
& dispatcher
) const = 0;
25 // A simple attribute value
26 template< typename T
>
32 explicit my_value(T
const& value
) : m_value(value
) {}
34 // The function passes the contained type into the dispatcher
35 bool dispatch(logging::type_dispatcher
& dispatcher
) const
37 logging::type_dispatcher::callback
< T
> cb
= dispatcher
.get_callback
< T
>();
48 // Value visitor for the supported types
51 typedef void result_type
;
53 // Implement visitation logic for all supported types
54 void operator() (int const& value
) const
56 std::cout
<< "Received int value = " << value
<< std::endl
;
58 void operator() (double const& value
) const
60 std::cout
<< "Received double value = " << value
<< std::endl
;
62 void operator() (std::string
const& value
) const
64 std::cout
<< "Received string value = " << value
<< std::endl
;
68 // Prints the supplied value
69 bool print(my_value_base
const& val
)
71 typedef boost::mpl::vector
< int, double, std::string
> types
;
73 print_visitor visitor
;
74 logging::static_type_dispatcher
< types
> disp(visitor
);
76 return val
.dispatch(disp
);
80 int main(int, char*[])
82 // These two attributes are supported by the dispatcher
83 bool res
= print(my_value
< std::string
>("Hello world!"));
86 res
= print(my_value
< double >(1.2));
90 res
= print(my_value
< float >(-4.3f
));