]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 <cstddef> | |
9 | #include <string> | |
10 | #include <iostream> | |
11 | #include <boost/mpl/vector.hpp> | |
12 | #include <boost/log/attributes/value_visitation.hpp> | |
13 | #include <boost/log/attributes/attribute_value_impl.hpp> | |
14 | #include <boost/log/utility/functional/save_result.hpp> | |
15 | ||
16 | namespace logging = boost::log; | |
17 | namespace attrs = boost::log::attributes; | |
18 | ||
19 | //[ example_attr_value_visitation | |
20 | // Our attribute value visitor | |
21 | struct print_visitor | |
22 | { | |
23 | typedef void result_type; | |
24 | ||
25 | result_type operator() (int val) const | |
26 | { | |
27 | std::cout << "Visited value is int: " << val << std::endl; | |
28 | } | |
29 | ||
30 | result_type operator() (std::string const& val) const | |
31 | { | |
32 | std::cout << "Visited value is string: " << val << std::endl; | |
33 | } | |
34 | }; | |
35 | ||
36 | void print_value(logging::attribute_value const& attr) | |
37 | { | |
38 | // Define the set of expected types of the stored value | |
39 | typedef boost::mpl::vector< int, std::string > types; | |
40 | ||
41 | // Apply our visitor | |
42 | logging::visitation_result result = logging::visit< types >(attr, print_visitor()); | |
43 | ||
44 | // Check the result | |
45 | if (result) | |
46 | std::cout << "Visitation succeeded" << std::endl; | |
47 | else | |
48 | std::cout << "Visitation failed" << std::endl; | |
49 | } | |
50 | //] | |
51 | ||
52 | //[ example_attr_value_visitation_with_retval | |
53 | struct hash_visitor | |
54 | { | |
55 | typedef std::size_t result_type; | |
56 | ||
57 | result_type operator() (int val) const | |
58 | { | |
59 | std::size_t h = val; | |
60 | h = (h << 15) + h; | |
61 | h ^= (h >> 6) + (h << 7); | |
62 | return h; | |
63 | } | |
64 | ||
65 | result_type operator() (std::string const& val) const | |
66 | { | |
67 | std::size_t h = 0; | |
68 | for (std::string::const_iterator it = val.begin(), end = val.end(); it != end; ++it) | |
69 | h += *it; | |
70 | ||
71 | h = (h << 15) + h; | |
72 | h ^= (h >> 6) + (h << 7); | |
73 | return h; | |
74 | } | |
75 | }; | |
76 | ||
77 | void hash_value(logging::attribute_value const& attr) | |
78 | { | |
79 | // Define the set of expected types of the stored value | |
80 | typedef boost::mpl::vector< int, std::string > types; | |
81 | ||
82 | // Apply our visitor | |
83 | std::size_t h = 0; | |
84 | logging::visitation_result result = logging::visit< types >(attr, logging::save_result(hash_visitor(), h)); | |
85 | ||
86 | // Check the result | |
87 | if (result) | |
88 | std::cout << "Visitation succeeded, hash value: " << h << std::endl; | |
89 | else | |
90 | std::cout << "Visitation failed" << std::endl; | |
91 | } | |
92 | //] | |
93 | ||
94 | #if 0 | |
95 | //[ example_attr_value_visitation_with_retval_rec | |
96 | void hash_value(logging::record_view const& rec, logging::attribute_name name) | |
97 | { | |
98 | // Define the set of expected types of the stored value | |
99 | typedef boost::mpl::vector< int, std::string > types; | |
100 | ||
101 | // Apply our visitor | |
102 | std::size_t h = 0; | |
103 | logging::visitation_result result = logging::visit< types >(name, rec, logging::save_result(hash_visitor(), h)); | |
104 | ||
105 | // Check the result | |
106 | if (result) | |
107 | std::cout << "Visitation succeeded, hash value: " << h << std::endl; | |
108 | else | |
109 | std::cout << "Visitation failed" << std::endl; | |
110 | } | |
111 | //] | |
112 | #endif | |
113 | ||
114 | int main(int, char*[]) | |
115 | { | |
116 | print_value(attrs::make_attribute_value(10)); | |
117 | print_value(attrs::make_attribute_value(std::string("Hello"))); | |
118 | ||
119 | hash_value(attrs::make_attribute_value(10)); | |
120 | hash_value(attrs::make_attribute_value(std::string("Hello"))); | |
121 | ||
122 | return 0; | |
123 | } |