1 | // Boost.Convert test and usage example |
2 | // Copyright (c) 2009-2020 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 | |
6 | #include "./test.hpp" |
7 | |
8 | #if !defined(BOOST_CONVERT_CXX14) |
9 | int main(int, char const* []) { return 0; } |
10 | #else |
11 | |
12 | #include <boost/convert.hpp> |
13 | #include <boost/convert/stream.hpp> |
14 | #include <functional> |
15 | |
16 | namespace { namespace local |
17 | { |
18 | bool called_functor_int; |
19 | bool called_functor_double; |
20 | bool called_functor_foo; |
21 | bool called_function_int; |
22 | bool called_function_long; |
23 | }} |
24 | |
25 | struct functor_int { int operator() () const { local:: called_functor_int = true; return INT_MAX; }}; |
26 | struct functor_double { double operator() () const { local::called_functor_double = true; return INT_MAX; }}; |
27 | struct functor_foo { int func (int) const { local:: called_functor_foo = true; return INT_MAX; }}; |
28 | |
29 | int function_int () { local:: called_function_int = true; return INT_MAX; } |
30 | long function_long () { local::called_function_long = true; return INT_MAX; } |
31 | |
32 | int |
33 | main(int, char const* []) |
34 | { |
35 | auto cnv = boost::cnv::cstream(); |
36 | auto foo = functor_foo(); |
37 | |
38 | int i01 = boost::convert<int>("uhm" , cnv).value_or_eval(functor_int()); |
39 | int i02 = boost::convert<int>("uhm" , cnv).value_or_eval(functor_double()); |
40 | int i03 = boost::convert<int>("uhm" , cnv).value_or_eval(std::bind(&functor_foo::func, foo, 0)); |
41 | int i04 = boost::convert<int>("uhm" , cnv).value_or_eval(function_int); |
42 | int i05 = boost::convert<int>("uhm" , cnv).value_or_eval(function_long); |
43 | |
44 | BOOST_TEST(local:: called_functor_int && i01 == INT_MAX); |
45 | BOOST_TEST(local::called_functor_double && i02 == INT_MAX); |
46 | BOOST_TEST(local:: called_functor_foo && i03 == INT_MAX); |
47 | BOOST_TEST(local:: called_function_int && i04 == INT_MAX); |
48 | BOOST_TEST(local:: called_function_long && i05 == INT_MAX); |
49 | |
50 | local:: called_functor_int = false; |
51 | local::called_functor_double = false; |
52 | local:: called_functor_foo = false; |
53 | local:: called_function_int = false; |
54 | local:: called_function_long = false; |
55 | |
56 | boost::convert<int>("uhm" , cnv, functor_int()); |
57 | boost::convert<int>("uhm" , cnv, functor_double()); |
58 | boost::convert<int>("uhm" , cnv, std::bind(&functor_foo::func, foo, 0)); |
59 | boost::convert<int>("uhm" , cnv, function_int); |
60 | boost::convert<int>("uhm" , cnv, function_long); |
61 | |
62 | BOOST_TEST(local:: called_functor_int && i01 == INT_MAX); |
63 | BOOST_TEST(local::called_functor_double && i02 == INT_MAX); |
64 | BOOST_TEST(local:: called_functor_foo && i03 == INT_MAX); |
65 | BOOST_TEST(local:: called_function_int && i04 == INT_MAX); |
66 | BOOST_TEST(local:: called_function_long && i05 == INT_MAX); |
67 | |
68 | try |
69 | { |
70 | boost::convert<int>("uhm" , cnv, boost::throw_on_failure); |
71 | BOOST_TEST(0); |
72 | } |
73 | catch (boost::bad_optional_access const&) |
74 | { |
75 | } |
76 | return boost::report_errors(); |
77 | } |
78 | |
79 | #endif |
80 | |