diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/convert/example | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/convert/example')
-rw-r--r-- | src/boost/libs/convert/example/algorithms.cpp | 186 | ||||
-rw-r--r-- | src/boost/libs/convert/example/default_converter.cpp | 39 | ||||
-rw-r--r-- | src/boost/libs/convert/example/default_converter_fail.cpp | 20 | ||||
-rw-r--r-- | src/boost/libs/convert/example/getting_serious.cpp | 155 | ||||
-rw-r--r-- | src/boost/libs/convert/example/getting_started.cpp | 268 | ||||
-rw-r--r-- | src/boost/libs/convert/example/jamfile.v2 | 34 | ||||
-rw-r--r-- | src/boost/libs/convert/example/lexical_cast.cpp | 35 | ||||
-rw-r--r-- | src/boost/libs/convert/example/stream.cpp | 101 |
8 files changed, 838 insertions, 0 deletions
diff --git a/src/boost/libs/convert/example/algorithms.cpp b/src/boost/libs/convert/example/algorithms.cpp new file mode 100644 index 00000000..6235fbfb --- /dev/null +++ b/src/boost/libs/convert/example/algorithms.cpp @@ -0,0 +1,186 @@ +// Copyright (c) 2009-2016 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. + +#include <boost/convert.hpp> +#include <boost/convert/stream.hpp> +#include <boost/convert/lexical_cast.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/array.hpp> +#include <boost/bind.hpp> +#include <vector> + +using std::string; + +static +void +introduction() +{ +//[algorithm_introduction + + /*`The following code demonstrates conversion of an array of integers from their textual ['hexadecimal] + representation. It assigns -1 to those which fail to convert: + */ + + boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }}; + std::vector<int> ints; + boost::cnv::cstream cnv; + + // Configure converter to read hexadecimal, skip (leading) white spaces. + cnv(std::hex)(std::skipws); + + std::transform(strs.begin(), strs.end(), std::back_inserter(ints), + boost::cnv::apply<int>(boost::cref(cnv)).value_or(-1)); + + BOOST_TEST(ints.size() == 3); // Number of values processed. + BOOST_TEST(ints[0] == 5); // " 5" + BOOST_TEST(ints[1] == 15); // "0XF" + BOOST_TEST(ints[2] == -1); // "not an int" +//] +} + +static +void +example1() +{ +//[algorithm_example1 + /*`The following code demonstrates a failed attempt (and one of the reasons ['Boost.Convert] + has been developed) to convert a few `string`s to `int`s with `boost::lexical_cast`: + */ + + boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }}; + std::vector<int> ints; + + try + { + std::transform(strs.begin(), strs.end(), std::back_inserter(ints), + boost::bind(boost::lexical_cast<int, string>, _1)); + + BOOST_TEST(0 && "Never reached!"); + } + catch (std::exception&) + { + BOOST_TEST(ints.size() == 0); // No strings converted. + } +//] +} + +static +void +example2() +{ +//[algorithm_example2 + /*`If the exception-throwing behavior is the desired behavior, then ['Boost.Convert] supports that. + In addition, it also supports a non-throwing process-flow: + */ + boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }}; + std::vector<int> ints; + + std::transform(strs.begin(), strs.end(), std::back_inserter(ints), + boost::cnv::apply<int>(boost::cnv::lexical_cast()).value_or(-1)); + + BOOST_TEST(ints.size() == 3); + BOOST_TEST(ints[0] == -1); // Failed conversion does not throw. + BOOST_TEST(ints[1] == -1); // Failed conversion does not throw. + BOOST_TEST(ints[2] == -1); // Failed conversion does not throw. +//] +} + +static +void +example3() +{ +//[algorithm_example3 + /*`Deploying `boost::cnv::cstream` with better formatting capabilities yields + better results with exception-throwing and non-throwing process-flows still supported: + */ + + boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }}; + std::vector<int> ints; + boost::cnv::cstream cnv; + + try + { + std::transform(strs.begin(), strs.end(), std::back_inserter(ints), + boost::cnv::apply<int>(boost::cref(cnv(std::hex)(std::skipws)))); + + BOOST_TEST(0 && "Never reached!"); + } + catch (boost::bad_optional_access const&) + { + BOOST_TEST(ints.size() == 2); // Only the first two strings converted. + BOOST_TEST(ints[0] == 5); // " 5" + BOOST_TEST(ints[1] == 15); // "0XF" + + // "not an int" causes the exception thrown. + } +//] +} + +static +void +example4() +{ + boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }}; + std::vector<int> ints; + boost::cnv::cstream cnv; + +//[algorithm_example4 + + std::transform(strs.begin(), strs.end(), std::back_inserter(ints), + boost::cnv::apply<int>(boost::cref(cnv(std::hex)(std::skipws))).value_or(-1)); + + BOOST_TEST(ints.size() == 3); + BOOST_TEST(ints[0] == 5); + BOOST_TEST(ints[1] == 15); + BOOST_TEST(ints[2] == -1); // Failed conversion + + /*`[important One notable difference in the deployment of `boost::cnv::cstream` with algorithms is + the use of `boost::cref` (or `std::cref` in C++11). + + It needs to be remembered that with standard algorithms the deployed converter needs to be + [@http://en.cppreference.com/w/cpp/named_req/TriviallyCopyable copyable] or + [@http://en.cppreference.com/w/cpp/named_req/MoveAssignable movable (C++11)] + and is, in fact, copied or moved by the respective algorithm before being used. + Given that `std::cstringstream` is not copyable, `boost::cnv::cstream` is not copyable either. + That limitation is routinely worked-around using `boost::ref` or `boost::cref`.] + */ +//] +} + +static +void +example5() +{ +//[algorithm_example5 + /*`And now an example of algorithm-based integer-to-string formatted conversion with + `std::hex`, `std::uppercase` and `std::showbase` formatting applied: + */ + boost::array<int, 3> ints = {{ 15, 16, 17 }}; + std::vector<std::string> strs; + boost::cnv::cstream cnv; + + cnv(std::hex)(std::uppercase)(std::showbase); + + std::transform(ints.begin(), ints.end(), std::back_inserter(strs), + boost::cnv::apply<string>(boost::cref(cnv))); + + BOOST_TEST(strs.size() == 3); + BOOST_TEST(strs[0] == "0XF"); // 15 + BOOST_TEST(strs[1] == "0X10"); // 16 + BOOST_TEST(strs[2] == "0X11"); // 17 +//] +} + +int +main(int, char const* []) +{ + introduction(); + example1(); + example2(); + example3(); + example4(); + example5(); + + return boost::report_errors(); +} diff --git a/src/boost/libs/convert/example/default_converter.cpp b/src/boost/libs/convert/example/default_converter.cpp new file mode 100644 index 00000000..fdcee846 --- /dev/null +++ b/src/boost/libs/convert/example/default_converter.cpp @@ -0,0 +1,39 @@ +// Boost.Convert test and usage example +// Copyright (c) 2009-2016 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. + +#include <boost/convert.hpp> +#include <boost/convert/stream.hpp> +#include <boost/detail/lightweight_test.hpp> + +#ifdef ONLY_FOR_DEMONSTRATION_PURPOSES +//[default_converter_declaration_simple +struct boost::cnv::by_default : boost::cnv::cstream {}; +//] +#endif +//[default_converter_declaration_formatted +struct boost::cnv::by_default : boost::cnv::cstream +{ + by_default() { (*this)(std::uppercase)(std::hex); } +}; +//] + +int +main(int, char const* []) +{ + //[default_converter_example1 + // No explicit converter provided. boost::cnv::by_default is used. + int i = boost::convert<int>("F").value_or(-1); + std::string s = boost::convert<std::string>(255).value_or("bad"); + + // 'i' and 's' are converted using boost::cnv::cstream + // with std::uppercase and std::hex formatting applied. + + BOOST_TEST(i == 15); // 15(10) = F(16) + BOOST_TEST(s == "FF"); // 255(10) = FF(16) + //] + + return boost::report_errors(); +} + diff --git a/src/boost/libs/convert/example/default_converter_fail.cpp b/src/boost/libs/convert/example/default_converter_fail.cpp new file mode 100644 index 00000000..96639964 --- /dev/null +++ b/src/boost/libs/convert/example/default_converter_fail.cpp @@ -0,0 +1,20 @@ +// Boost.Convert test and usage example +// Copyright (c) 2009-2016 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. + +#include <boost/convert.hpp> + +using std::string; +using boost::convert; + +// This is expected to fail to compile: +//'boost::cnv::by_default' : class has no constructors. + +int +main(int, char const* []) +{ + int i = convert<int>("123").value(); + string s = convert<string>(123).value(); +} + diff --git a/src/boost/libs/convert/example/getting_serious.cpp b/src/boost/libs/convert/example/getting_serious.cpp new file mode 100644 index 00000000..1e0b7597 --- /dev/null +++ b/src/boost/libs/convert/example/getting_serious.cpp @@ -0,0 +1,155 @@ +// Copyright (c) 2009-2016 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(); +} diff --git a/src/boost/libs/convert/example/getting_started.cpp b/src/boost/libs/convert/example/getting_started.cpp new file mode 100644 index 00000000..5fa9c531 --- /dev/null +++ b/src/boost/libs/convert/example/getting_started.cpp @@ -0,0 +1,268 @@ +// Copyright (c) 2009-2016 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/bind.hpp> +#include <boost/detail/lightweight_test.hpp> + +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 + { + boost::cnv::lexical_cast cnv; // 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 + boost::cnv::lexical_cast cnv1; + boost::cnv::strtol cnv2; + boost::cnv::spirit cnv3; + + 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 + boost::cnv::cstream cnv; + + try + { + int i1 = lexical_cast<int>(" 123"); // Does not work. + BOOST_TEST(!"Never reached"); + } + catch (...) {} + + 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 const s1 = "123"; + std::string const s2 = "456"; + int const default_i1 = 11; + int const default_i2 = 12; + boost::cnv::cstream cnv; + + //[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 const s1 = "123"; + std::string const s2 = "456"; + int const default_i1 = 11; + int const default_i2 = 12; + boost::cnv::cstream cnv; + + //[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(boost::bind(fallback_fun, "bad i1", default_i1)); + int i2 = convert<int>(s2).value_or_eval(boost::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(); +} diff --git a/src/boost/libs/convert/example/jamfile.v2 b/src/boost/libs/convert/example/jamfile.v2 new file mode 100644 index 00000000..485cb491 --- /dev/null +++ b/src/boost/libs/convert/example/jamfile.v2 @@ -0,0 +1,34 @@ +# Convert Jamfile +# Copyright (c) Vladimir Batov 2009-2014 +# Distributed under the Boost Software License, Version 1.0. +# See copy at http://www.boost.org/LICENSE_1_0.txt. + +# bring in the rules for testing +import testing ; + +project convert_examples + : requirements + <warnings>on + <toolset>gcc:<warnings>all + <toolset>msvc:<warnings>all + <toolset>gcc:<cxxflags>"-Wno-unused-local-typedefs -Wno-unused-variable -Wno-long-long" + <toolset>msvc:<cxxflags>"/wd4996 /wd4512 /wd4610 /wd4510 /wd4127 /wd4701 /wd4127 /wd4305 /wd4100 /wd4512 /wd4714" + <toolset>msvc:<asynch-exceptions>on + <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE + <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE + <toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS + <toolset>msvc:<define>_CRT_SECURE_NO_WARNINGS + <include>../include + ; + +run algorithms.cpp : : : : convert_example_algorithms ; +run default_converter.cpp : : : : convert_example_default_converter ; +run getting_serious.cpp : : : : convert_example_getting_started ; +run getting_started.cpp : : : : convert_example_getting_serious ; +run lexical_cast.cpp : : : : convert_example_lexical_cast_converter ; +run stream.cpp : : : : convert_example_stream_converter ; + +compile-fail default_converter_fail.cpp : <warnings>off <cxxflags>"-w" ; + + + diff --git a/src/boost/libs/convert/example/lexical_cast.cpp b/src/boost/libs/convert/example/lexical_cast.cpp new file mode 100644 index 00000000..5527cf14 --- /dev/null +++ b/src/boost/libs/convert/example/lexical_cast.cpp @@ -0,0 +1,35 @@ +// Copyright (c) 2009-2016 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. + +//[lexical_cast_headers1 +#include <boost/convert.hpp> +#include <boost/convert/lexical_cast.hpp> +#include <boost/detail/lightweight_test.hpp> + +using std::string; +using boost::convert; +using boost::lexical_cast; + +struct boost::cnv::by_default : boost::cnv::lexical_cast {}; +//] + +int +main(int, char const* []) +{ + //[lexical_cast_example1 + int i1 = lexical_cast<int>("123"); // Throws if the conversion fails. + int i2 = convert<int>("123").value(); // Throws if the conversion fails. + int i3 = convert<int>("uhm").value_or(-1); // Returns -1 if the conversion fails. + string s1 = lexical_cast<string>(123); + string s2 = convert<string>(123).value(); + + BOOST_TEST(i1 == 123); + BOOST_TEST(i2 == 123); + BOOST_TEST(i3 == -1); + BOOST_TEST(s1 == "123"); + BOOST_TEST(s2 == "123"); + //] + + return boost::report_errors(); +} diff --git a/src/boost/libs/convert/example/stream.cpp b/src/boost/libs/convert/example/stream.cpp new file mode 100644 index 00000000..0a22c234 --- /dev/null +++ b/src/boost/libs/convert/example/stream.cpp @@ -0,0 +1,101 @@ +// Copyright (c) 2009-2016 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. + +#include <boost/convert.hpp> +#include <boost/convert/lexical_cast.hpp> + +//[stream_headers1 +#include <boost/convert.hpp> +#include <boost/convert/stream.hpp> +#include <boost/detail/lightweight_test.hpp> + +using std::string; +using boost::convert; + +struct boost::cnv::by_default : boost::cnv::cstream {}; +//] +//[stream_headers2 +namespace cnv = boost::cnv; +namespace arg = boost::cnv::parameter; +//] + +#include "../test/test.hpp" + +static +void +example1() +{ + //[stream_example1 + int i2 = convert<int>("123").value(); // Throws when fails. + int i3 = convert<int>("uhm").value_or(-1); // Returns -1 when fails. + string s2 = convert<string>(123).value(); + + BOOST_TEST(i2 == 123); + BOOST_TEST(i3 == -1); + BOOST_TEST(s2 == "123"); + //] +} + +static +void +example2() +{ + //[stream_example2 + boost::cnv::cstream ccnv; + boost::cnv::wstream wcnv; + + int v01 = convert<int>(" FF", ccnv(std::hex)(std::skipws)).value_or(0); + int v02 = convert<int>(L" F", wcnv(std::hex)(std::skipws)).value_or(0); + int v03 = convert<int>(" FF", ccnv(std::dec)(std::skipws)).value_or(-5); + int v04 = convert<int>(L" F", wcnv(std::dec)(std::skipws)).value_or(-5); + + BOOST_TEST(v01 == 255); // "FF" + BOOST_TEST(v02 == 15); // L"F" + BOOST_TEST(v03 == -5); // Failed to convert "FF" as decimal. + BOOST_TEST(v04 == -5); // Failed to convert L"F" as decimal. + //] + //[stream_example3 + ccnv(std::showbase)(std::uppercase)(std::hex); + + BOOST_TEST(convert<string>(255, ccnv, "bad") == "0XFF"); + BOOST_TEST(convert<string>( 15, ccnv, "bad") == "0XF"); + //] + //[stream_example4 + ccnv(arg::base = cnv::base::dec) + (arg::uppercase = true) + (arg::notation = cnv::notation::scientific); + //] + //[stream_example5 + ccnv(std::dec)(std::uppercase)(std::scientific); + //] +} + +static +void +example6() +{ + //[stream_example6 + boost::cnv::cstream cnv1; + boost::cnv::lexical_cast cnv2; + + change chg = change::up; + string s1 = convert<string>(chg, cnv1, "bad"); // Input type (change) deduced + string s2 = convert<string, change>(change::dn, cnv1, "bad"); // Input type (change) enforced + + BOOST_TEST(convert<change>("up", cnv1, change::no) == change::up); + BOOST_TEST(convert<change>("up", cnv2, change::no) == change::up); + BOOST_TEST(s1 == "up"); + BOOST_TEST(s2 == "dn"); + //] +} + +int +main(int, char const* []) +{ + example1(); + example2(); + example6(); + + return boost::report_errors(); +} |