• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009-2016 Vladimir Batov.
2 // Use, modification and distribution are subject to the Boost Software License,
3 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
4 
5 #include <boost/convert.hpp>
6 #include <boost/convert/lexical_cast.hpp>
7 
8 //[stream_headers1
9 #include <boost/convert.hpp>
10 #include <boost/convert/stream.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12 
13 using std::string;
14 using boost::convert;
15 
16 struct boost::cnv::by_default : boost::cnv::cstream {};
17 //]
18 //[stream_headers2
19 namespace cnv = boost::cnv;
20 namespace arg = boost::cnv::parameter;
21 //]
22 
23 #include "../test/test.hpp"
24 
25 static
26 void
example1()27 example1()
28 {
29     //[stream_example1
30     int    i2 = convert<int>("123").value();      // Throws when fails.
31     int    i3 = convert<int>("uhm").value_or(-1); // Returns -1 when fails.
32     string s2 = convert<string>(123).value();
33 
34     BOOST_TEST(i2 == 123);
35     BOOST_TEST(i3 == -1);
36     BOOST_TEST(s2 == "123");
37     //]
38 }
39 
40 static
41 void
example2()42 example2()
43 {
44     //[stream_example2
45     boost::cnv::cstream ccnv;
46     boost::cnv::wstream wcnv;
47 
48     int v01 = convert<int>("  FF", ccnv(std::hex)(std::skipws)).value_or(0);
49     int v02 = convert<int>(L"  F", wcnv(std::hex)(std::skipws)).value_or(0);
50     int v03 = convert<int>("  FF", ccnv(std::dec)(std::skipws)).value_or(-5);
51     int v04 = convert<int>(L"  F", wcnv(std::dec)(std::skipws)).value_or(-5);
52 
53     BOOST_TEST(v01 == 255); // "FF"
54     BOOST_TEST(v02 ==  15); // L"F"
55     BOOST_TEST(v03 ==  -5); // Failed to convert "FF" as decimal.
56     BOOST_TEST(v04 ==  -5); // Failed to convert L"F" as decimal.
57     //]
58     //[stream_example3
59     ccnv(std::showbase)(std::uppercase)(std::hex);
60 
61     BOOST_TEST(convert<string>(255, ccnv, "bad") == "0XFF");
62     BOOST_TEST(convert<string>( 15, ccnv, "bad") ==  "0XF");
63     //]
64     //[stream_example4
65     ccnv(arg::base = cnv::base::dec)
66         (arg::uppercase = true)
67         (arg::notation = cnv::notation::scientific);
68     //]
69     //[stream_example5
70     ccnv(std::dec)(std::uppercase)(std::scientific);
71     //]
72 }
73 
74 static
75 void
example6()76 example6()
77 {
78     //[stream_example6
79     boost::cnv::cstream      cnv1;
80     boost::cnv::lexical_cast cnv2;
81 
82     change chg = change::up;
83     string  s1 = convert<string>(chg, cnv1, "bad");                // Input type (change) deduced
84     string  s2 = convert<string, change>(change::dn, cnv1, "bad"); // Input type (change) enforced
85 
86     BOOST_TEST(convert<change>("up", cnv1, change::no) == change::up);
87     BOOST_TEST(convert<change>("up", cnv2, change::no) == change::up);
88     BOOST_TEST(s1 == "up");
89     BOOST_TEST(s2 == "dn");
90     //]
91 }
92 
93 int
main(int,char const * [])94 main(int, char const* [])
95 {
96     example1();
97     example2();
98     example6();
99 
100     return boost::report_errors();
101 }
102