fallbacks.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Boost.Convert test and usage example
  2. // Copyright (c) 2009-2016 Vladimir Batov.
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  5. #include "./test.hpp"
  6. #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
  7. int main(int, char const* []) { return 0; }
  8. #else
  9. #include <boost/convert.hpp>
  10. #include <boost/convert/stream.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/function.hpp>
  13. #include <boost/bind.hpp>
  14. namespace { namespace local
  15. {
  16. bool called_functor_int;
  17. bool called_functor_double;
  18. bool called_functor_foo;
  19. bool called_function_int;
  20. bool called_function_long;
  21. }}
  22. struct functor_int { int operator() () const { local:: called_functor_int = true; return INT_MAX; }};
  23. struct functor_double { double operator() () const { local::called_functor_double = true; return INT_MAX; }};
  24. struct functor_foo { int func (int) const { local:: called_functor_foo = true; return INT_MAX; }};
  25. int function_int () { local:: called_function_int = true; return INT_MAX; }
  26. long function_long () { local::called_function_long = true; return INT_MAX; }
  27. int
  28. main(int, char const* [])
  29. {
  30. boost::cnv::cstream cnv;
  31. functor_foo foo;
  32. int i01 = boost::convert<int>("uhm", cnv).value_or_eval(functor_int());
  33. int i02 = boost::convert<int>("uhm", cnv).value_or_eval(functor_double());
  34. int i03 = boost::convert<int>("uhm", cnv).value_or_eval(boost::bind(&functor_foo::func, foo, 0));
  35. int i04 = boost::convert<int>("uhm", cnv).value_or_eval(function_int);
  36. int i05 = boost::convert<int>("uhm", cnv).value_or_eval(function_long);
  37. BOOST_TEST(local:: called_functor_int && i01 == INT_MAX);
  38. BOOST_TEST(local::called_functor_double && i02 == INT_MAX);
  39. BOOST_TEST(local:: called_functor_foo && i03 == INT_MAX);
  40. BOOST_TEST(local:: called_function_int && i04 == INT_MAX);
  41. BOOST_TEST(local:: called_function_long && i05 == INT_MAX);
  42. local:: called_functor_int = false;
  43. local::called_functor_double = false;
  44. local:: called_functor_foo = false;
  45. local:: called_function_int = false;
  46. local:: called_function_long = false;
  47. boost::convert<int>("uhm", cnv, functor_int());
  48. boost::convert<int>("uhm", cnv, functor_double());
  49. boost::convert<int>("uhm", cnv, boost::bind(&functor_foo::func, foo, 0));
  50. boost::convert<int>("uhm", cnv, function_int);
  51. boost::convert<int>("uhm", cnv, function_long);
  52. BOOST_TEST(local:: called_functor_int && i01 == INT_MAX);
  53. BOOST_TEST(local::called_functor_double && i02 == INT_MAX);
  54. BOOST_TEST(local:: called_functor_foo && i03 == INT_MAX);
  55. BOOST_TEST(local:: called_function_int && i04 == INT_MAX);
  56. BOOST_TEST(local:: called_function_long && i05 == INT_MAX);
  57. try
  58. {
  59. boost::convert<int>("uhm", cnv, boost::throw_on_failure);
  60. BOOST_TEST(0);
  61. }
  62. catch (boost::bad_optional_access const&)
  63. {
  64. }
  65. return boost::report_errors();
  66. }
  67. #endif