summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/convert/example/stream.cpp
blob: 0a22c2346c8e78d66345213256f43f99c7f1c9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2009-2016 Vladimir Batov.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.

#include <boost/convert.hpp>
#include <boost/convert/lexical_cast.hpp>

//[stream_headers1
#include <boost/convert.hpp>
#include <boost/convert/stream.hpp>
#include <boost/detail/lightweight_test.hpp>

using std::string;
using boost::convert;

struct boost::cnv::by_default : boost::cnv::cstream {};
//]
//[stream_headers2
namespace cnv = boost::cnv;
namespace arg = boost::cnv::parameter;
//]

#include "../test/test.hpp"

static
void
example1()
{
    //[stream_example1
    int    i2 = convert<int>("123").value();      // Throws when fails.
    int    i3 = convert<int>("uhm").value_or(-1); // Returns -1 when fails.
    string s2 = convert<string>(123).value();

    BOOST_TEST(i2 == 123);
    BOOST_TEST(i3 == -1);
    BOOST_TEST(s2 == "123");
    //]
}

static
void
example2()
{
    //[stream_example2
    boost::cnv::cstream ccnv;
    boost::cnv::wstream wcnv;

    int v01 = convert<int>("  FF", ccnv(std::hex)(std::skipws)).value_or(0);
    int v02 = convert<int>(L"  F", wcnv(std::hex)(std::skipws)).value_or(0);
    int v03 = convert<int>("  FF", ccnv(std::dec)(std::skipws)).value_or(-5);
    int v04 = convert<int>(L"  F", wcnv(std::dec)(std::skipws)).value_or(-5);

    BOOST_TEST(v01 == 255); // "FF"
    BOOST_TEST(v02 ==  15); // L"F"
    BOOST_TEST(v03 ==  -5); // Failed to convert "FF" as decimal.
    BOOST_TEST(v04 ==  -5); // Failed to convert L"F" as decimal.
    //]
    //[stream_example3
    ccnv(std::showbase)(std::uppercase)(std::hex);

    BOOST_TEST(convert<string>(255, ccnv, "bad") == "0XFF");
    BOOST_TEST(convert<string>( 15, ccnv, "bad") ==  "0XF");
    //]
    //[stream_example4
    ccnv(arg::base = cnv::base::dec)
        (arg::uppercase = true)
        (arg::notation = cnv::notation::scientific);
    //]
    //[stream_example5
    ccnv(std::dec)(std::uppercase)(std::scientific);
    //]
}

static
void
example6()
{
    //[stream_example6
    boost::cnv::cstream      cnv1;
    boost::cnv::lexical_cast cnv2;

    change chg = change::up;
    string  s1 = convert<string>(chg, cnv1, "bad");                // Input type (change) deduced
    string  s2 = convert<string, change>(change::dn, cnv1, "bad"); // Input type (change) enforced

    BOOST_TEST(convert<change>("up", cnv1, change::no) == change::up);
    BOOST_TEST(convert<change>("up", cnv2, change::no) == change::up);
    BOOST_TEST(s1 == "up");
    BOOST_TEST(s2 == "dn");
    //]
}

int
main(int, char const* [])
{
    example1();
    example2();
    example6();

    return boost::report_errors();
}