diff --git a/doc/algorithms.qbk b/doc/algorithms.qbk index a95d054..4fc6197 100644 --- a/doc/algorithms.qbk +++ b/doc/algorithms.qbk @@ -1,77 +1,13 @@ [section Boost.Convert with Standard Algorithms] -The following code demonstrates a failed attempt to convert a few strings with `boost::lexical_cast`: +[algorithm_example1] +[algorithm_example2] +[algorithm_example3] - array strings = {{ "0XF", "0X10", "0X11", "0X12", "not an int" }}; - std::vector integers; +The following `boost::optional`-based version is not available with pre-C++11 compilers: - try - { - std::transform( - strings.begin(), - strings.end(), - std::back_inserter(integers), - boost::bind(boost::lexical_cast, _1)); + std::vector> opts; - BOOST_ASSERT(!"We should not be here"); - } - catch (std::exception&) - { - // No strings converted. - } - -The first take by ['Boost.Convert] is more successful (it knows how to handle the hexadecimal format) but still not quite satisfactory (your mileage may vary) as conversion is interrupted when ['Boost.Convert] comes across an invalid input: - - boost::array strings = {{ "0XF", "0X10", "0X11", "0X12", "not an int" }}; - std::vector integers; - boost::cstringstream_converter cnv; // stringstream-based char converter - - try - { - std::transform( - strings.begin(), - strings.end(), - std::back_inserter(integers), - boost::convert(cnv(std::hex))); - - BOOST_ASSERT(!"We should not be here"); - } - catch (std::exception&) - { - // Only the first four strings converted. Failed to convert the last one. - - BOOST_ASSERT(integers[0] == 15); - BOOST_ASSERT(integers[1] == 16); - BOOST_ASSERT(integers[2] == 17); - BOOST_ASSERT(integers[3] == 18); - } - -And at last a fully working version (with a conversion-failure fallback value provided): - - boost::array strings = {{ "0XF", "0X10", "0X11", "0X12", "not an int" }}; - std::vector integers; - boost::cstringstream_converter cnv; // stringstream-based char converter - - std::transform( - strings.begin(), - strings.end(), - std::back_inserter(integers), - boost::convert(cnv(std::hex)).value_or(-1)); - - BOOST_ASSERT(integers[0] == 15); - BOOST_ASSERT(integers[1] == 16); - BOOST_ASSERT(integers[2] == 17); - BOOST_ASSERT(integers[3] == 18); - BOOST_ASSERT(integers[4] == -1); // Failed conversion - -The following `boost::conversion::optional`-based version is not available with pre-C++11 compilers: - - std::vector> opt_ints; - - std::transform( - strings.begin(), - strings.end(), - std::back_inserter(opt_ints), - boost::convert(cnv(std::hex))); + std::transform(strs.begin(), strs.end(), std::back_inserter(opts), boost::convert(cnv(std::hex))); [endsect] diff --git a/doc/introduction.qbk b/doc/introduction.qbk index e0ae257..171c9f6 100644 --- a/doc/introduction.qbk +++ b/doc/introduction.qbk @@ -38,7 +38,7 @@ The `std::stringstream`-based converter draws on the standard `std::streams` fun [import ../example/algorithms.cpp] -[algorithm_simple] +[algorithm_introduction] An interesting (and yet to be explored) property is that ['Boost.Convert] is not limited to string-to-type and type-to-string conversions. `boost::convert()` is type-agnostic and the plugged-in converter ultimately dictates what types and conversions are supported. Consequently, a wide range of conversion\/transformation-related tasks can be addressed and ['deployed uniformly] by plugging-in special-purpose converters. diff --git a/example/algorithms.cpp b/example/algorithms.cpp index 9e3dcf0..435c6de 100644 --- a/example/algorithms.cpp +++ b/example/algorithms.cpp @@ -2,34 +2,130 @@ #include #include #include +#include #include -#include +static void -example::algorithm::strings_to_ints_simple() +introduction() { -//[algorithm_simple +//[algorithm_introduction /*`For example, the following snippet converts an array of integers from their textual hexadecimal representation and assigns INT_MAX to those which fail to convert: */ - boost::array strings = {{ "0XF", "0X10", "0X11", "0X12", "not an int" }}; + boost::array strings = {{ " 5", "0XF", "not an int" }}; std::vector integers; boost::cnv::cstringstream cnv; // stringstream-based char converter + cnv(std::hex)(std::skipws); + std::transform( strings.begin(), strings.end(), std::back_inserter(integers), - boost::convert(cnv(std::hex)).value_or(INT_MAX)); + boost::convert(cnv).value_or(INT_MAX)); - BOOST_TEST(integers.size() == 5); - BOOST_TEST(integers[0] == 15); - BOOST_TEST(integers[1] == 16); - BOOST_TEST(integers[2] == 17); - BOOST_TEST(integers[3] == 18); - BOOST_TEST(integers[4] == INT_MAX); // Failed conversion + BOOST_TEST(integers.size() == 3); + BOOST_TEST(integers[0] == 5); + BOOST_TEST(integers[1] == 15); + BOOST_TEST(integers[2] == INT_MAX); // Failed conversion //] } +static +void +example1() +{ + //[algorithm_example1 + /*`The following code demonstrates a failed attempt to convert a few strings with `boost::lexical_cast`: + */ + + boost::array strs = {{ " 5", "0XF", "not an int" }}; + std::vector ints; + + try + { + std::transform( + strs.begin(), + strs.end(), + std::back_inserter(ints), + boost::bind(boost::lexical_cast, _1)); + + BOOST_TEST(!"We should not be here"); + } + catch (std::exception&) + { + // No strings converted. + BOOST_TEST(ints.size() == 0); + } + //] +} + +static +void +example2() +{ + //[algorithm_example2 + /*`The first take by ['Boost.Convert] is more successful (it knows how to handle the hexadecimal + format and to skip white spaces) but still not quite satisfactory (your mileage may vary) as + conversion is interrupted when ['Boost.Convert] comes across an invalid input: + */ + + boost::array strs = {{ " 5", "0XF", "not an int" }}; + std::vector ints; + boost::cnv::cstringstream cnv; + + try + { + std::transform( + strs.begin(), + strs.end(), + std::back_inserter(ints), + boost::convert(cnv(std::hex)(std::skipws))); + + BOOST_TEST(!"We should not be here"); + } + catch (std::exception&) + { + // Only the first two strings converted. Failed to convert the last one. + + BOOST_TEST(ints.size() == 2); + BOOST_TEST(ints[0] == 5); + BOOST_TEST(ints[1] == 15); + } + //] +} + +static +void +example3() +{ + boost::array strs = {{ " 5", "0XF", "not an int" }}; + std::vector ints; + boost::cnv::cstringstream cnv; + + //[algorithm_example3 + /*`And at last a fully working version (with a conversion-failure fallback value provided): + */ + std::transform( + strs.begin(), + strs.end(), + std::back_inserter(ints), + boost::convert(cnv(std::hex)).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 + //] +} +void +example::algorithms() +{ + introduction(); + example1(); + example2(); + example3(); +} diff --git a/example/example.hpp b/example/example.hpp index 4007da5..c2e9938 100644 --- a/example/example.hpp +++ b/example/example.hpp @@ -8,11 +8,7 @@ struct example static void getting_started (); static void getting_serious (); static void lexical_cast (); - - struct algorithm - { - static void strings_to_ints_simple(); - }; + static void algorithms (); }; #endif // BOOST_CONVERT_EXAMPLE_HPP diff --git a/test/algorithms.cpp b/test/algorithms.cpp index ed388b3..6c3572d 100644 --- a/test/algorithms.cpp +++ b/test/algorithms.cpp @@ -102,8 +102,6 @@ strings_to_ints() integers.clear(); - example::algorithm::strings_to_ints_simple(); - #ifdef NOT_AVAILABLE_UNTIL_CPP11 std::vector> opt_ints; diff --git a/test/test_convert.cpp b/test/test_convert.cpp index 10931e2..0866cd9 100644 --- a/test/test_convert.cpp +++ b/test/test_convert.cpp @@ -52,22 +52,24 @@ int main(int argc, char const* argv[]) { test::ints ints; - test::strings strings; + test::strings strs; boost::random::mt19937 gen; boost::random::uniform_int_distribution<> dist (INT_MIN, INT_MAX); - ints .reserve(test::num_cycles); - strings.reserve(test::num_cycles); + ints.reserve(test::num_cycles); + strs.reserve(test::num_cycles); for (int k = 0; k < test::num_cycles; ++k) { - int v = dist(gen); + int iv = dist(gen); + string sv = boost::lexical_cast(iv); - ints .push_back(v); - strings.push_back(boost::lexical_cast(v)); + ints.push_back(iv); + strs.push_back(sv); } example::getting_started(); example::getting_serious(); + example::algorithms(); test::invalid(boost::cnv::lexical_cast()); // test::invalid(boost::cnv::cstringstream()); @@ -92,8 +94,8 @@ main(int argc, char const* argv[]) test::algorithms(); test::callables(); test::encryption(); - test::performance(strings, ints); - test::spirit(strings); + test::performance(strs, ints); + test::spirit(strs); return boost::report_errors(); }