attr_value_visitation.cpp 3.3 KB

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