File: getting_serious.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 (155 lines) | stat: -rw-r--r-- 4,386 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
// 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 BOOST_MSVC
#  pragma warning(disable : 4127)  // conditional expression is constant.
#  pragma warning(disable : 4189)  // local variable is initialized but not referenced.
#endif

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

using std::string;
using boost::convert;
using boost::lexical_cast;
using boost::optional;

static void process_failure() {}
static void log(...) {}
static int fallback_function() { return -1; }

//[getting_serious_default_converter
struct boost::cnv::by_default : boost::cnv::cstream {};
//]
static
void
example1()
{
    boost::cnv::cstream  cnv;
    std::string const    str = "123";
    std::string const   str1 = "123";
    std::string const   str2 = "123";
    std::string const   str3 = "123";
    int const fallback_value = -1;

    {
        //[getting_serious_example1
        int i2 = convert<int>("not an int", cnv).value_or(-1); // after the call i2==-1

        if (i2 == -1) process_failure();
        //]
    }
    {
        //[getting_serious_example2
        try
        {
            int i1 = lexical_cast<int>(str);         // Throws if the conversion fails.
            int i2 = convert<int>(str, cnv).value(); // Throws if the conversion fails.
        }
        catch (...)
        {
            process_failure();
        }
        //]
    }
    {
        //[getting_serious_example3
        optional<int> r1 = convert<int>(str1, cnv); // Does not throw on conversion failure.
        optional<int> r2 = convert<int>(str2, cnv); // Does not throw on conversion failure.
        // ...
        try // Delayed processing of potential exceptions.
        {
            int i1 = r1.value(); // Will throw if conversion failed.
            int i2 = r2.value(); // Will throw if conversion failed.
        }
        catch (boost::bad_optional_access const&)
        {
            // Handle failed conversion.
        }

        // Exceptions are avoided altogether.
        int i1 = r1 ? r1.value() : fallback_value;
        int i2 = r2.value_or(fallback_value);
        int i3 = convert<int>(str3, cnv).value_or(fallback_value);
        int i4 = convert<int>(str3, cnv).value_or_eval(fallback_function);
        //]
    }
}

//[getting_serious_example5
struct fallback_func
{
    int operator()() const { log("Failed to convert"); return 42; }
};
//]

static
void
example4()
{
    boost::cnv::cstream  cnv;
    std::string const    str = "123";
    int const fallback_value = -1;
    //[getting_serious_example4
    boost::optional<int> res = boost::convert<int>(str, cnv);

    if (!res) log("str conversion failed!");

    int i1 = res.value_or(fallback_value);

    // ...proceed
    //]
    //[getting_serious_example6
    // Fallback function is called when failed
    int i2 = convert<int>(str, cnv).value_or_eval(fallback_func());
    int i3 = convert<int>(str, cnv, fallback_func()); // Same as above. Alternative API.
    //]
}

static
void
example7()
{
    boost::cnv::cstream  cnv;
    std::string const    str = "123";
    int const fallback_value = -1;
    //[getting_serious_example7
    // Error-processing behavior are specified unambiguously and uniformly.
    // a) i1: Returns the provided fallback value;
    // b) i2: Calls the provided failure-processing function;
    // c) i3: Throws an exception.

    int i1 = convert<int>(str, cnv, fallback_value);
    int i2 = convert<int>(str, cnv, fallback_func());

    try
    {
        // Throwing behavior specified explicitly rather than implied.
        int i3 = convert<int>(str, cnv, boost::throw_on_failure);
    }
    catch (boost::bad_optional_access const&)
    {
      // Handle failed conversion.
    }
    //]
    //[getting_serious_example8
    int m1 = convert<int>(str, cnv).value_or(fallback_value);
    int m2 = convert<int>(str, cnv).value_or_eval(fallback_func());
    int m3 = convert<int>(str, cnv).value();
    //]
    //[getting_serious_example9
    int n1 = convert<int>(str).value_or(fallback_value);
    int n2 = convert<int>(str).value_or_eval(fallback_func());
    int n3 = convert<int>(str).value();
    //]
}

int
main(int, char const* [])
{
    example1();
    example4();
    example7();
}