util_static_type_disp.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * \file util_static_type_disp.cpp
  9. * \author Andrey Semashev
  10. * \date 09.01.2009
  11. *
  12. * \brief This header contains tests for the static type dispatcher.
  13. */
  14. #define BOOST_TEST_MODULE util_static_type_disp
  15. #include <string>
  16. #include <typeinfo>
  17. #include <boost/mpl/vector.hpp>
  18. #include <boost/test/unit_test.hpp>
  19. #include <boost/test/tools/floating_point_comparison.hpp>
  20. #include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
  21. namespace logging = boost::log;
  22. namespace {
  23. // A simple attribute value
  24. template< typename T >
  25. struct my_value
  26. {
  27. T m_Value;
  28. explicit my_value(T const& value) : m_Value(value) {}
  29. // The function passes the contained type into the dispatcher
  30. bool dispatch(logging::type_dispatcher& dispatcher)
  31. {
  32. logging::type_dispatcher::callback< T > callback = dispatcher.get_callback< T >();
  33. if (callback)
  34. {
  35. callback(m_Value);
  36. return true;
  37. }
  38. else
  39. return false;
  40. }
  41. };
  42. // The function tests general functionality of the type dispatcher
  43. template< typename DispatcherT >
  44. void test_general_functionality(DispatcherT& disp)
  45. {
  46. // These two attributes are supported by the dispatcher
  47. my_value< std::string > val1("Hello world!");
  48. disp.set_expected(val1.m_Value);
  49. BOOST_CHECK(val1.dispatch(disp));
  50. my_value< double > val2(1.2);
  51. disp.set_expected(val2.m_Value);
  52. BOOST_CHECK(val2.dispatch(disp));
  53. // This one is not
  54. my_value< float > val3(static_cast< float >(-4.3));
  55. disp.set_expected();
  56. BOOST_CHECK(!val3.dispatch(disp));
  57. }
  58. // Type dispatcher for the supported types
  59. struct my_dispatcher :
  60. public logging::static_type_dispatcher<
  61. boost::mpl::vector< int, double, std::string >
  62. >
  63. {
  64. typedef logging::static_type_dispatcher<
  65. boost::mpl::vector< int, double, std::string >
  66. > base_type;
  67. enum type_expected
  68. {
  69. none_expected,
  70. int_expected,
  71. double_expected,
  72. string_expected
  73. };
  74. my_dispatcher() :
  75. base_type(*this),
  76. m_Expected(none_expected),
  77. m_Int(0),
  78. m_Double(0.0)
  79. {
  80. }
  81. void set_expected()
  82. {
  83. m_Expected = none_expected;
  84. }
  85. void set_expected(int value)
  86. {
  87. m_Expected = int_expected;
  88. m_Int = value;
  89. }
  90. void set_expected(double value)
  91. {
  92. m_Expected = double_expected;
  93. m_Double = value;
  94. }
  95. void set_expected(std::string const& value)
  96. {
  97. m_Expected = string_expected;
  98. m_String = value;
  99. }
  100. // Implement visitation logic for all supported types
  101. void operator() (int const& value) const
  102. {
  103. BOOST_CHECK_EQUAL(m_Expected, int_expected);
  104. BOOST_CHECK_EQUAL(m_Int, value);
  105. }
  106. void operator() (double const& value) const
  107. {
  108. BOOST_CHECK_EQUAL(m_Expected, double_expected);
  109. BOOST_CHECK_CLOSE(m_Double, value, 0.001);
  110. }
  111. void operator() (std::string const& value) const
  112. {
  113. BOOST_CHECK_EQUAL(m_Expected, string_expected);
  114. BOOST_CHECK_EQUAL(m_String, value);
  115. }
  116. private:
  117. type_expected m_Expected;
  118. int m_Int;
  119. double m_Double;
  120. std::string m_String;
  121. };
  122. } // namespace
  123. // The test checks that general functionality works
  124. BOOST_AUTO_TEST_CASE(type_dispatch)
  125. {
  126. my_dispatcher disp;
  127. test_general_functionality(disp);
  128. }