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