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