]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/convert/example/getting_serious.cpp
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.
6 # pragma warning(disable : 4127) // conditional expression is constant.
7 # pragma warning(disable : 4189) // local variable is initialized but not referenced.
10 #include <boost/convert.hpp>
11 #include <boost/convert/stream.hpp>
12 #include <boost/convert/lexical_cast.hpp>
16 using boost::lexical_cast
;
17 using boost::optional
;
19 static void process_failure() {}
20 static void log(...) {}
21 static int fallback_function() { return -1; }
23 //[getting_serious_default_converter
24 struct boost::cnv::by_default
: boost::cnv::cstream
{};
30 boost::cnv::cstream cnv
;
31 std::string
const str
= "123";
32 std::string
const str1
= "123";
33 std::string
const str2
= "123";
34 std::string
const str3
= "123";
35 int const fallback_value
= -1;
38 //[getting_serious_example1
39 int i2
= convert
<int>("not an int", cnv
).value_or(-1); // after the call i2==-1
41 if (i2
== -1) process_failure();
45 //[getting_serious_example2
48 int i1
= lexical_cast
<int>(str
); // Throws if the conversion fails.
49 int i2
= convert
<int>(str
, cnv
).value(); // Throws if the conversion fails.
58 //[getting_serious_example3
59 optional
<int> r1
= convert
<int>(str1
, cnv
); // Does not throw on conversion failure.
60 optional
<int> r2
= convert
<int>(str2
, cnv
); // Does not throw on conversion failure.
62 try // Delayed processing of potential exceptions.
64 int i1
= r1
.value(); // Will throw if conversion failed.
65 int i2
= r2
.value(); // Will throw if conversion failed.
67 catch (boost::bad_optional_access
const&)
69 // Handle failed conversion.
72 // Exceptions are avoided altogether.
73 int i1
= r1
? r1
.value() : fallback_value
;
74 int i2
= r2
.value_or(fallback_value
);
75 int i3
= convert
<int>(str3
, cnv
).value_or(fallback_value
);
76 int i4
= convert
<int>(str3
, cnv
).value_or_eval(fallback_function
);
81 //[getting_serious_example5
84 int operator()() const { log("Failed to convert"); return 42; }
92 boost::cnv::cstream cnv
;
93 std::string
const str
= "123";
94 int const fallback_value
= -1;
95 //[getting_serious_example4
96 boost::optional
<int> res
= boost::convert
<int>(str
, cnv
);
98 if (!res
) log("str conversion failed!");
100 int i1
= res
.value_or(fallback_value
);
104 //[getting_serious_example6
105 // Fallback function is called when failed
106 int i2
= convert
<int>(str
, cnv
).value_or_eval(fallback_func());
107 int i3
= convert
<int>(str
, cnv
, fallback_func()); // Same as above. Alternative API.
115 boost::cnv::cstream cnv
;
116 std::string
const str
= "123";
117 int const fallback_value
= -1;
118 //[getting_serious_example7
119 // Error-processing behavior are specified unambiguously and uniformly.
120 // a) i1: Returns the provided fallback value;
121 // b) i2: Calls the provided failure-processing function;
122 // c) i3: Throws an exception.
124 int i1
= convert
<int>(str
, cnv
, fallback_value
);
125 int i2
= convert
<int>(str
, cnv
, fallback_func());
129 // Throwing behavior specified explicitly rather than implied.
130 int i3
= convert
<int>(str
, cnv
, boost::throw_on_failure
);
132 catch (boost::bad_optional_access
const&)
134 // Handle failed conversion.
137 //[getting_serious_example8
138 int m1
= convert
<int>(str
, cnv
).value_or(fallback_value
);
139 int m2
= convert
<int>(str
, cnv
).value_or_eval(fallback_func());
140 int m3
= convert
<int>(str
, cnv
).value();
142 //[getting_serious_example9
143 int n1
= convert
<int>(str
).value_or(fallback_value
);
144 int n2
= convert
<int>(str
).value_or_eval(fallback_func());
145 int n3
= convert
<int>(str
).value();
150 main(int, char const* [])