1 | // Boost.Convert test and usage example |
2 | // Copyright (c) 2009-2020 Vladimir Batov. |
3 | // Use, modification and distribution are subject to the Boost Software License, |
4 | // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. |
5 | |
6 | #include "./test.hpp" |
7 | |
8 | #if !defined(BOOST_CONVERT_CXX14) |
9 | int main(int, char const* []) { return 0; } |
10 | #else |
11 | |
12 | #include <boost/convert.hpp> |
13 | #include <boost/convert/stream.hpp> |
14 | #include <array> |
15 | #include <vector> |
16 | #include <algorithm> |
17 | |
18 | using std::string; |
19 | using std::wstring; |
20 | |
21 | static |
22 | void |
23 | test_user_type() |
24 | { |
25 | boost::cnv::cstream cnv; // stringstream-based char converter |
26 | |
27 | direction const up_dir1 = direction::up; |
28 | direction const dn_dir1 = direction::dn; |
29 | // string const up_dir0_str = boost::convert<string, direction>(direction::up, cnv).value(); |
30 | // string const dn_dir0_str = boost::convert<string, direction>(direction::dn, cnv).value(); |
31 | string const up_dir1_str = boost::convert<string>(up_dir1, cnv).value(); |
32 | string const dn_dir1_str = boost::convert<string>(dn_dir1, cnv).value(); |
33 | direction const up_dir2 = boost::convert<direction>(up_dir1_str, cnv).value(); |
34 | direction const dn_dir2 = boost::convert<direction>(dn_dir1_str, cnv).value(); |
35 | direction const up_dir3 = boost::convert<direction>(up_dir1_str, cnv).value(); |
36 | direction const dn_dir3 = boost::convert<direction>(dn_dir1_str, cnv).value(); |
37 | direction const dn_dir4 = boost::convert<direction>("junk" , cnv).value_or(direction::dn); |
38 | boost::optional<direction> up_dir4 = boost::convert<direction>("junk" , cnv); |
39 | |
40 | // BOOST_TEST(up_dir0_str == "up"); |
41 | // BOOST_TEST(dn_dir0_str == "dn"); |
42 | BOOST_TEST(up_dir1_str == "up" ); |
43 | BOOST_TEST(dn_dir1_str == "dn" ); |
44 | BOOST_TEST(up_dir2 == up_dir1); |
45 | BOOST_TEST(dn_dir2 == dn_dir1); |
46 | BOOST_TEST(up_dir3 == direction::up); |
47 | BOOST_TEST(dn_dir3 == direction::dn); |
48 | BOOST_TEST(dn_dir4 == direction::dn); |
49 | BOOST_TEST(!up_dir4); // Failed conversion |
50 | } |
51 | |
52 | static |
53 | void |
54 | test_algorithms() |
55 | { |
56 | //[algorithm_example6 |
57 | std::array<change, 3> chgs1 = {{ change::no, change::up, change::dn }}; |
58 | std::array<change::value_type, 3> chgs2 = {{ change::no, change::up, change::dn }}; |
59 | |
60 | auto strs1 = std::vector<std::string>(); |
61 | auto strs2 = std::vector<std::string>(); |
62 | auto strs3 = std::vector<std::string>(); |
63 | auto cnv = boost::cnv::cstream(); |
64 | |
65 | std::transform(chgs1.begin(), chgs1.end(), std::back_inserter(strs1), |
66 | boost::cnv::apply<string>(std::cref(cnv))); // Deduced TypeIn is 'change' |
67 | |
68 | std::transform(chgs2.begin(), chgs2.end(), std::back_inserter(strs2), |
69 | boost::cnv::apply<string>(std::cref(cnv))); // Deduced TypeIn is 'change::value_type' |
70 | |
71 | BOOST_TEST(strs1.size() == 3); |
72 | BOOST_TEST(strs1[0] == "no" ); |
73 | BOOST_TEST(strs1[1] == "up" ); |
74 | BOOST_TEST(strs1[2] == "dn" ); |
75 | |
76 | BOOST_TEST(strs2.size() == 3); |
77 | BOOST_TEST(strs2[0] == "0" ); |
78 | BOOST_TEST(strs2[1] == "1" ); |
79 | BOOST_TEST(strs2[2] == "2" ); |
80 | //] |
81 | //[algorithm_example7 |
82 | |
83 | std::transform(chgs2.begin(), chgs2.end(), std::back_inserter(strs3), |
84 | boost::cnv::apply<string, change>(std::cref(cnv))); |
85 | |
86 | BOOST_TEST(strs3.size() == 3); |
87 | BOOST_TEST(strs3[0] == "no" ); |
88 | BOOST_TEST(strs3[1] == "up" ); |
89 | BOOST_TEST(strs3[2] == "dn" ); |
90 | //] |
91 | } |
92 | |
93 | int |
94 | main(int, char const* []) |
95 | { |
96 | test_user_type(); |
97 | test_algorithms(); |
98 | |
99 | return boost::report_errors(); |
100 | } |
101 | |
102 | #endif |
103 | |