File: getting_started.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (266 lines) | stat: -rw-r--r-- 7,618 bytes parent folder | download | duplicates (7)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2009-2020 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.

#ifdef _MSC_VER
#  pragma warning(disable : 4127)  // conditional expression is constant.
#  pragma warning(disable : 4189)  // local variable is initialized but not referenced.
#  pragma warning(disable : 4100)  // unreferenced formal parameter.
#  pragma warning(disable : 4714)  // marked as __forceinline not inlined.
#endif

namespace { namespace local
{
#if defined(_MSC_VER)
    static bool const is_msc = true;
#else
    static bool const is_msc = false;
#endif
}}

#include <boost/detail/lightweight_test.hpp>
#include <functional>

static
void
log(char const*)
{
  // Dummy function to demonstrate how problems might be logged.
}

//[getting_started_headers1
#include <boost/convert.hpp>
#include <boost/convert/lexical_cast.hpp>
//]

//[getting_started_using
using std::string;
using boost::lexical_cast;
using boost::convert;
//]
//[getting_started_default_converter
// Definition of the default converter (optional)
struct boost::cnv::by_default : boost::cnv::lexical_cast {};
//]

static
void
getting_started_example1()
{
    //[getting_started_example1
    try
    {
        auto  cnv = boost::cnv::lexical_cast();        // boost::lexical_cast-based converter
        int    i1 = lexical_cast<int>("123");          // boost::lexical_cast standard deployment
        int    i2 = convert<int>("123").value();       // boost::convert with the default converter
        int    i3 = convert<int>("123", cnv).value();  // boost::convert with an explicit converter
        string s1 = lexical_cast<string>(123);         // boost::lexical_cast standard deployment
        string s2 = convert<string>(123).value();      // boost::convert with the default converter
        string s3 = convert<string>(123, cnv).value(); // boost::convert with an explicit converter

        BOOST_TEST(i1 == 123);
        BOOST_TEST(i2 == 123);
        BOOST_TEST(i3 == 123);
        BOOST_TEST(s1 == "123");
        BOOST_TEST(s2 == "123");
        BOOST_TEST(s3 == "123");
    }
    catch (std::exception const& ex)
    {
        // Please be aware that the conversion requests above can fail.
        // Use try'n'catch blocks to handle any exceptions thrown.
        // Ignore this at your peril!
        std::cerr << "Exception " << ex.what() << std::endl;
    }
    //] [/getting_started_example1]
}

static
void
getting_started_example2()
{
    //[getting_started_example2
    // Does not throw. Returns fallback value (-1) when failed.
    int i = convert<int>("uhm", boost::cnv::lexical_cast()).value_or(-1);

    BOOST_TEST(i == -1); // Conversion failed. 'i' assigned the fallback value.
    //]
}

//[getting_started_headers3
#include <boost/convert/strtol.hpp>
#include <boost/convert/spirit.hpp>
//]

static
void
getting_started_example3()
{
    //[getting_started_example3
    auto cnv1 = boost::cnv::lexical_cast();
    auto cnv2 = boost::cnv::strtol();
    auto cnv3 = boost::cnv::spirit();

    int i1 = convert<int>("123", cnv1).value();
    int i2 = convert<int>("123", cnv2).value(); // Two times faster than lexical_cast.
    int i3 = convert<int>("123", cnv3).value(); // Four times faster than lexical_cast.
    //]
}

//[getting_started_headers4
#include <boost/convert/stream.hpp>
//]

static
void
getting_started_example4()
{
    //[getting_started_example4
    try
    {
        int i1 = lexical_cast<int>("   123"); // Does not work.
        BOOST_TEST(!"Never reached");
    }
    catch (...) {}

    auto        cnv = boost::cnv::cstream();
    int          i2 = convert<int>("   123", cnv(std::skipws)).value(); // Success
    string       s1 = lexical_cast<string>(12.34567);
    string       s2 = convert<string>(12.34567, cnv(std::fixed)(std::setprecision(3))).value();
    string       s3 = convert<string>(12.34567, cnv(std::scientific)(std::setprecision(3))).value();
    string expected = local::is_msc ? "1.235e+001" : "1.235e+01";

    BOOST_TEST(i2 == 123);        // boost::cnv::cstream. Successful conversion of "   123".
    BOOST_TEST(s1 == "12.34567"); // boost::lexical_cast. Precision is not configurable.
    BOOST_TEST(s2 == "12.346");   // boost::cnv::cstream. Precision was set to 3. Fixed.
    BOOST_TEST(s3 == expected);   // boost::cnv::cstream. Precision was set to 3. Scientific.
    //]
}

static
void
getting_started_example5()
{
    //[getting_started_example5
    boost::cnv::cstream cnv;

    int i1 = lexical_cast<int>("123");              // Throws when conversion fails.
    int i2 = convert<int>("123", cnv).value();      // Throws when conversion fails.
    int i3 = convert<int>("uhm", cnv).value_or(-1); // Returns -1 when conversion fails.

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

static
void
getting_started_example6()
{
    std::string s1 = "123";
    std::string s2 = "456";
    int default_i1 = 11;
    int default_i2 = 12;
    auto       cnv = boost::cnv::cstream();

    //[getting_started_example6

    int i1 = convert<int>(s1, cnv(std::hex)).value_or(-1); // Read as hex
    int i2 = convert<int>(s2, cnv(std::dec)).value_or(-1); // Read as decimal

    if (i1 == -1) log("bad i1"), i1 = default_i1; // Log failure. Proceed with the default
    if (i2 == -1) log("bad i2"), i2 = default_i2; // Log failure. Proceed with the default

    // ... proceed
    //]
    BOOST_TEST(i1 == 291);
    BOOST_TEST(i2 == 456);
}

static
void
getting_started_example7()
{
    std::string s1 = "123";
    std::string s2 = "456";
    int default_i1 = 11;
    int default_i2 = 12;
    auto       cnv = boost::cnv::cstream();

    //[getting_started_example7

    int i1 = convert<int>(s1, cnv(std::hex)).value_or(default_i1); // If failed, proceed with the default
    int i2 = convert<int>(s2, cnv(std::dec)).value_or(default_i2); // If failed, proceed with the default
    // ... proceed
    //]
    BOOST_TEST(i1 == 291);
    BOOST_TEST(i2 == 456);
}

static
//[getting_started_example9_func
int
fallback_fun(char const* msg, int fallback_value)
{
    // The principal advantage of a fallback_func over a fallback_value
    // is that the former is only called when the conversion request fails.
    // Consequently, the returned fallback_value is only calculated (which
    // potentially might be expensive) when it is absolutely necessary.
    log(msg); return fallback_value;
}
//]
static
void
getting_started_example9()
{
    std::string const s1 = "123";
    std::string const s2 = "456";
    int const default_i1 = 11;
    int const default_i2 = 12;

    //[getting_started_example9
    int i1 = convert<int>(s1).value_or_eval(std::bind(fallback_fun, "bad i1", default_i1));
    int i2 = convert<int>(s2).value_or_eval(std::bind(fallback_fun, "bad i2", default_i2));
    // ... proceed
    //]
    BOOST_TEST(i1 == 123);
    BOOST_TEST(i2 == 456);
}

static
void
getting_started_example8()
{
    std::string const str = "123";
    int const  default_i1 = 12;
    //[getting_started_example8
    int i1 = default_i1;

    try
    {
        i1 = lexical_cast<int>(str);
    }
    catch (...)
    {
        log("bad i1");
    }
    //]
    BOOST_TEST(i1 == 123);
}

int
main(int, char const* [])
{
    getting_started_example1();
    getting_started_example2();
    getting_started_example3();
    getting_started_example4();
    getting_started_example5();
    getting_started_example6();
    getting_started_example7();
    getting_started_example8();
    getting_started_example9();

    return boost::report_errors();
}