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