]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/attr_value_visitation.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)
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>
16 namespace logging
= boost::log
;
17 namespace attrs
= boost::log::attributes
;
19 //[ example_attr_value_visitation
20 // Our attribute value visitor
23 typedef void result_type
;
25 result_type
operator() (int val
) const
27 std::cout
<< "Visited value is int: " << val
<< std::endl
;
30 result_type
operator() (std::string
const& val
) const
32 std::cout
<< "Visited value is string: " << val
<< std::endl
;
36 void print_value(logging::attribute_value
const& attr
)
38 // Define the set of expected types of the stored value
39 typedef boost::mpl::vector
< int, std::string
> types
;
42 logging::visitation_result result
= logging::visit
< types
>(attr
, print_visitor());
46 std::cout
<< "Visitation succeeded" << std::endl
;
48 std::cout
<< "Visitation failed" << std::endl
;
52 //[ example_attr_value_visitation_with_retval
55 typedef std::size_t result_type
;
57 result_type
operator() (int val
) const
61 h
^= (h
>> 6) + (h
<< 7);
65 result_type
operator() (std::string
const& val
) const
68 for (std::string::const_iterator it
= val
.begin(), end
= val
.end(); it
!= end
; ++it
)
72 h
^= (h
>> 6) + (h
<< 7);
77 void hash_value(logging::attribute_value
const& attr
)
79 // Define the set of expected types of the stored value
80 typedef boost::mpl::vector
< int, std::string
> types
;
84 logging::visitation_result result
= logging::visit
< types
>(attr
, logging::save_result(hash_visitor(), h
));
88 std::cout
<< "Visitation succeeded, hash value: " << h
<< std::endl
;
90 std::cout
<< "Visitation failed" << std::endl
;
95 //[ example_attr_value_visitation_with_retval_rec
96 void hash_value(logging::record_view
const& rec
, logging::attribute_name name
)
98 // Define the set of expected types of the stored value
99 typedef boost::mpl::vector
< int, std::string
> types
;
103 logging::visitation_result result
= logging::visit
< types
>(name
, rec
, logging::save_result(hash_visitor(), h
));
107 std::cout
<< "Visitation succeeded, hash value: " << h
<< std::endl
;
109 std::cout
<< "Visitation failed" << std::endl
;
114 int main(int, char*[])
116 print_value(attrs::make_attribute_value(10));
117 print_value(attrs::make_attribute_value(std::string("Hello")));
119 hash_value(attrs::make_attribute_value(10));
120 hash_value(attrs::make_attribute_value(std::string("Hello")));