]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/convert/example/algorithms.cpp
1 // Copyright (c) 2009-2020 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.
5 #include <boost/convert.hpp>
6 #include <boost/convert/stream.hpp>
7 #include <boost/convert/lexical_cast.hpp>
8 #include <boost/detail/lightweight_test.hpp>
19 //[algorithm_introduction
21 /*`The following code demonstrates conversion of an array of integers from their textual ['hexadecimal]
22 representation. It assigns -1 to those which fail to convert:
25 std::array
<char const*, 3> strs
= {{ " 5", "0XF", "not an int" }};
26 std::vector
<int> ints
;
27 boost::cnv::cstream cnv
;
29 // Configure converter to read hexadecimal, skip (leading) white spaces.
30 cnv(std::hex
)(std::skipws
);
32 std::transform(strs
.begin(), strs
.end(), std::back_inserter(ints
),
33 boost::cnv::apply
<int>(std::cref(cnv
)).value_or(-1));
35 BOOST_TEST(ints
.size() == 3); // Number of values processed.
36 BOOST_TEST(ints
[0] == 5); // " 5"
37 BOOST_TEST(ints
[1] == 15); // "0XF"
38 BOOST_TEST(ints
[2] == -1); // "not an int"
47 /*`The following code demonstrates a failed attempt (and one of the reasons ['Boost.Convert]
48 has been developed) to convert a few `string`s to `int`s with `boost::lexical_cast`:
51 std::array
<char const*, 3> strs
= {{ " 5", "0XF", "not an int" }};
52 std::vector
<int> ints
;
56 std::transform(strs
.begin(), strs
.end(), std::back_inserter(ints
),
57 std::bind(boost::lexical_cast
<int, string
>, std::placeholders::_1
));
59 BOOST_TEST(0 && "Never reached!");
61 catch (std::exception
&)
63 BOOST_TEST(ints
.size() == 0); // No strings converted.
73 /*`If the exception-throwing behavior is the desired behavior, then ['Boost.Convert] supports that.
74 In addition, it also supports a non-throwing process-flow:
76 std::array
<char const*, 3> strs
= {{ " 5", "0XF", "not an int" }};
77 std::vector
<int> ints
;
79 std::transform(strs
.begin(), strs
.end(), std::back_inserter(ints
),
80 boost::cnv::apply
<int>(boost::cnv::lexical_cast()).value_or(-1));
82 BOOST_TEST(ints
.size() == 3);
83 BOOST_TEST(ints
[0] == -1); // Failed conversion does not throw.
84 BOOST_TEST(ints
[1] == -1); // Failed conversion does not throw.
85 BOOST_TEST(ints
[2] == -1); // Failed conversion does not throw.
94 /*`Deploying `boost::cnv::cstream` with better formatting capabilities yields
95 better results with exception-throwing and non-throwing process-flows still supported:
98 std::array
<char const*, 3> strs
= {{ " 5", "0XF", "not an int" }};
99 std::vector
<int> ints
;
100 boost::cnv::cstream cnv
;
104 std::transform(strs
.begin(), strs
.end(), std::back_inserter(ints
),
105 boost::cnv::apply
<int>(std::cref(cnv(std::hex
)(std::skipws
))));
107 BOOST_TEST(0 && "Never reached!");
109 catch (boost::bad_optional_access
const&)
111 BOOST_TEST(ints
.size() == 2); // Only the first two strings converted.
112 BOOST_TEST(ints
[0] == 5); // " 5"
113 BOOST_TEST(ints
[1] == 15); // "0XF"
115 // "not an int" causes the exception thrown.
124 std::array
<char const*, 3> strs
= {{ " 5", "0XF", "not an int" }};
125 std::vector
<int> ints
;
126 boost::cnv::cstream cnv
;
128 //[algorithm_example4
130 std::transform(strs
.begin(), strs
.end(), std::back_inserter(ints
),
131 boost::cnv::apply
<int>(std::cref(cnv(std::hex
)(std::skipws
))).value_or(-1));
133 BOOST_TEST(ints
.size() == 3);
134 BOOST_TEST(ints
[0] == 5);
135 BOOST_TEST(ints
[1] == 15);
136 BOOST_TEST(ints
[2] == -1); // Failed conversion
138 /*`[important One notable difference in the deployment of `boost::cnv::cstream` with algorithms is
139 the use of `std::cref`.
141 It needs to be remembered that with standard algorithms the deployed converter needs to be
142 [@http://en.cppreference.com/w/cpp/named_req/TriviallyCopyable copyable] or
143 [@http://en.cppreference.com/w/cpp/named_req/MoveAssignable movable (C++11)]
144 and is, in fact, copied or moved by the respective algorithm before being used.
145 Given that `std::cstringstream` is not copyable, `boost::cnv::cstream` is not copyable either.
146 That limitation is routinely worked-around using `std::ref` or `std::cref`.]
155 //[algorithm_example5
156 /*`And now an example of algorithm-based integer-to-string formatted conversion with
157 `std::hex`, `std::uppercase` and `std::showbase` formatting applied:
159 std::array
<int, 3> ints
= {{ 15, 16, 17 }};
160 std::vector
<std::string
> strs
;
161 boost::cnv::cstream cnv
;
163 cnv(std::hex
)(std::uppercase
)(std::showbase
);
165 std::transform(ints
.begin(), ints
.end(), std::back_inserter(strs
),
166 boost::cnv::apply
<string
>(std::cref(cnv
)));
168 BOOST_TEST(strs
.size() == 3);
169 BOOST_TEST(strs
[0] == "0XF"); // 15
170 BOOST_TEST(strs
[1] == "0X10"); // 16
171 BOOST_TEST(strs
[2] == "0X11"); // 17
176 main(int, char const* [])
185 return boost::report_errors();