]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/util_dynamic_type_disp.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / example / doc / util_dynamic_type_disp.cpp
1 /*
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)
6 */
7
8 #include <cassert>
9 #include <cstddef>
10 #include <string>
11 #include <iostream>
12 #include <boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp>
13
14 namespace logging = boost::log;
15
16 // Base interface for the custom opaque value
17 struct my_value_base
18 {
19 virtual ~my_value_base() {}
20 virtual bool dispatch(logging::type_dispatcher& dispatcher) const = 0;
21 };
22
23 // A simple attribute value
24 template< typename T >
25 struct my_value :
26 public my_value_base
27 {
28 T m_value;
29
30 explicit my_value(T const& value) : m_value(value) {}
31
32 // The function passes the contained type into the dispatcher
33 bool dispatch(logging::type_dispatcher& dispatcher) const
34 {
35 logging::type_dispatcher::callback< T > cb = dispatcher.get_callback< T >();
36 if (cb)
37 {
38 cb(m_value);
39 return true;
40 }
41 else
42 return false;
43 }
44 };
45
46 //[ example_util_dynamic_type_dispatcher
47 // Visitor functions for the supported types
48 void on_int(int const& value)
49 {
50 std::cout << "Received int value = " << value << std::endl;
51 }
52
53 void on_double(double const& value)
54 {
55 std::cout << "Received double value = " << value << std::endl;
56 }
57
58 void on_string(std::string const& value)
59 {
60 std::cout << "Received string value = " << value << std::endl;
61 }
62
63 logging::dynamic_type_dispatcher disp;
64
65 // The function initializes the dispatcher object
66 void init_disp()
67 {
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);
72 }
73
74 // Prints the supplied value
75 bool print(my_value_base const& val)
76 {
77 return val.dispatch(disp);
78 }
79 //]
80
81 int main(int, char*[])
82 {
83 init_disp();
84
85 // These two attributes are supported by the dispatcher
86 bool res = print(my_value< std::string >("Hello world!"));
87 assert(res);
88
89 res = print(my_value< double >(1.2));
90 assert(res);
91
92 // This one is not
93 res = print(my_value< float >(-4.3f));
94 assert(!res);
95
96 return 0;
97 }