attr_value_extraction.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <utility>
  10. #include <iostream>
  11. #include <boost/mpl/vector.hpp>
  12. #include <boost/log/attributes/value_extraction.hpp>
  13. #include <boost/log/attributes/attribute_value_impl.hpp>
  14. #include <boost/log/utility/value_ref.hpp>
  15. namespace logging = boost::log;
  16. namespace attrs = boost::log::attributes;
  17. //[ example_attr_value_extraction
  18. void print_value(logging::attribute_value const& attr)
  19. {
  20. // Extract a reference to the stored value
  21. logging::value_ref< int > val = logging::extract< int >(attr);
  22. // Check the result
  23. if (val)
  24. std::cout << "Extraction succeeded: " << val.get() << std::endl;
  25. else
  26. std::cout << "Extraction failed" << std::endl;
  27. }
  28. //]
  29. //[ example_attr_value_extraction_multiple_types
  30. void print_value_multiple_types(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. // Extract a reference to the stored value
  35. logging::value_ref< types > val = logging::extract< types >(attr);
  36. // Check the result
  37. if (val)
  38. {
  39. std::cout << "Extraction succeeded" << std::endl;
  40. switch (val.which())
  41. {
  42. case 0:
  43. std::cout << "int: " << val.get< int >() << std::endl;
  44. break;
  45. case 1:
  46. std::cout << "string: " << val.get< std::string >() << std::endl;
  47. break;
  48. }
  49. }
  50. else
  51. std::cout << "Extraction failed" << std::endl;
  52. }
  53. //]
  54. //[ example_attr_value_extraction_visitor
  55. struct hash_visitor
  56. {
  57. typedef std::size_t result_type;
  58. result_type operator() (int val) const
  59. {
  60. std::size_t h = val;
  61. h = (h << 15) + h;
  62. h ^= (h >> 6) + (h << 7);
  63. return h;
  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. h = (h << 15) + h;
  71. h ^= (h >> 6) + (h << 7);
  72. return h;
  73. }
  74. };
  75. void hash_value(logging::attribute_value const& attr)
  76. {
  77. // Define the set of expected types of the stored value
  78. typedef boost::mpl::vector< int, std::string > types;
  79. // Extract the stored value
  80. logging::value_ref< types > val = logging::extract< types >(attr);
  81. // Check the result
  82. if (val)
  83. std::cout << "Extraction succeeded, hash value: " << val.apply_visitor(hash_visitor()) << std::endl;
  84. else
  85. std::cout << "Extraction failed" << std::endl;
  86. }
  87. //]
  88. #if 0
  89. //[ example_attr_value_extraction_visitor_rec
  90. void hash_value(logging::record_view const& rec, logging::attribute_name name)
  91. {
  92. // Define the set of expected types of the stored value
  93. typedef boost::mpl::vector< int, std::string > types;
  94. // Extract the stored value
  95. logging::value_ref< types > val = logging::extract< types >(name, rec);
  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. #endif
  104. int main(int, char*[])
  105. {
  106. print_value(attrs::make_attribute_value(10));
  107. print_value_multiple_types(attrs::make_attribute_value(10));
  108. print_value_multiple_types(attrs::make_attribute_value(std::string("Hello")));
  109. hash_value(attrs::make_attribute_value(10));
  110. hash_value(attrs::make_attribute_value(std::string("Hello")));
  111. return 0;
  112. }