Squashed 'third_party/boostorg/lexical_cast/' content from commit 5bfe667
Change-Id: I518d0a0b83082059176813a98b80ad92892ac745
git-subtree-dir: third_party/boostorg/lexical_cast
git-subtree-split: 5bfe6672d467ebd8de5d911c07b2a931b43156de
diff --git a/example/args_to_numbers.cpp b/example/args_to_numbers.cpp
new file mode 100644
index 0000000..edcc779
--- /dev/null
+++ b/example/args_to_numbers.cpp
@@ -0,0 +1,35 @@
+// Copyright 2013 Antony Polukhin
+
+// Distributed under the Boost Software License, Version 1.0.
+// (See the accompanying file LICENSE_1_0.txt
+// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
+
+//[lexical_cast_args_example
+//`The following example treats command line arguments as a sequence of numeric data
+
+#include <boost/lexical_cast.hpp>
+#include <vector>
+
+int main(int /*argc*/, char * argv[])
+{
+ using boost::lexical_cast;
+ using boost::bad_lexical_cast;
+
+ std::vector<short> args;
+
+ while (*++argv)
+ {
+ try
+ {
+ args.push_back(lexical_cast<short>(*argv));
+ }
+ catch(const bad_lexical_cast &)
+ {
+ args.push_back(0);
+ }
+ }
+
+ // ...
+}
+
+//] [/lexical_cast_args_example]
diff --git a/example/generic_stringize.cpp b/example/generic_stringize.cpp
new file mode 100644
index 0000000..06b392b
--- /dev/null
+++ b/example/generic_stringize.cpp
@@ -0,0 +1,65 @@
+// Copyright 2013-2017 Antony Polukhin
+
+// Distributed under the Boost Software License, Version 1.0.
+// (See the accompanying file LICENSE_1_0.txt
+// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
+
+#include <boost/config.hpp>
+#ifdef BOOST_MSVC
+# pragma warning(disable: 4512) // generic_stringize.cpp(37) : warning C4512: 'stringize_functor' : assignment operator could not be generated
+#endif
+
+//[lexical_cast_stringize
+/*`
+ In this example we'll make a `stringize` method that accepts a sequence, converts
+ each element of the sequence into string and appends that string to the result.
+
+ Example is based on the example from the [@http://www.packtpub.com/boost-cplusplus-application-development-cookbook/book Boost C++ Application Development Cookbook]
+ by Antony Polukhin, ISBN 9781849514880.
+
+ Step 1: Making a functor that converts any type to a string and remembers result:
+*/
+
+#include <boost/lexical_cast.hpp>
+
+struct stringize_functor {
+private:
+ std::string& result;
+
+public:
+ explicit stringize_functor(std::string& res)
+ : result(res)
+ {}
+
+ template <class T>
+ void operator()(const T& v) const {
+ result += boost::lexical_cast<std::string>(v);
+ }
+};
+
+//` Step 2: Applying `stringize_functor` to each element in sequence:
+#include <boost/fusion/include/for_each.hpp>
+template <class Sequence>
+std::string stringize(const Sequence& seq) {
+ std::string result;
+ boost::fusion::for_each(seq, stringize_functor(result));
+ return result;
+}
+
+//` Step 3: Using the `stringize` with different types:
+#include <cassert>
+#include <boost/fusion/adapted/boost_tuple.hpp>
+#include <boost/fusion/adapted/std_pair.hpp>
+
+int main() {
+ boost::tuple<char, int, char, int> decim('-', 10, 'e', 5);
+ assert(stringize(decim) == "-10e5");
+
+ std::pair<int, std::string> value_and_type(270, "Kelvin");
+ assert(stringize(value_and_type) == "270Kelvin");
+}
+
+//] [/lexical_cast_stringize]
+
+
+
diff --git a/example/small_examples.cpp b/example/small_examples.cpp
new file mode 100644
index 0000000..ae9a88e
--- /dev/null
+++ b/example/small_examples.cpp
@@ -0,0 +1,56 @@
+// Copyright 2013-2017 Antony Polukhin
+
+// Distributed under the Boost Software License, Version 1.0.
+// (See the accompanying file LICENSE_1_0.txt
+// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
+
+#include <boost/lexical_cast.hpp>
+#include <string>
+#include <cstdio>
+
+#ifdef BOOST_MSVC
+# pragma warning(disable: 4996) // `strerror` is not safe
+#endif
+
+//[lexical_cast_log_errno
+//`The following example uses numeric data in a string expression:
+
+void log_message(const std::string &);
+
+void log_errno(int yoko)
+{
+ log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
+}
+
+//] [/lexical_cast_log_errno]
+
+
+//[lexical_cast_fixed_buffer
+//`The following example converts some number and puts it to file:
+
+void number_to_file(int number, FILE* file)
+{
+ typedef boost::array<char, 50> buf_t; // You can use std::array if your compiler supports it
+ buf_t buffer = boost::lexical_cast<buf_t>(number); // No dynamic memory allocation
+ std::fputs(buffer.begin(), file);
+}
+
+//] [/lexical_cast_fixed_buffer]
+
+//[lexical_cast_substring_conversion
+//`The following example takes part of the string and converts it to `int`:
+
+int convert_strings_part(const std::string& s, std::size_t pos, std::size_t n)
+{
+ return boost::lexical_cast<int>(s.data() + pos, n);
+}
+
+//] [/lexical_cast_substring_conversion]
+
+void log_message(const std::string &) {}
+
+int main()
+{
+ return 0;
+}
+
diff --git a/example/variant_to_long_double.cpp b/example/variant_to_long_double.cpp
new file mode 100644
index 0000000..9b1aa27
--- /dev/null
+++ b/example/variant_to_long_double.cpp
@@ -0,0 +1,41 @@
+// Copyright 2013-2017 Antony Polukhin
+
+// Distributed under the Boost Software License, Version 1.0.
+// (See the accompanying file LICENSE_1_0.txt
+// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
+
+
+//[lexical_cast_variant_to_long_double
+/*`
+ In this example we'll make a `to_long_double` method that converts value of the Boost.Variant to `long double`.
+*/
+
+#include <boost/lexical_cast.hpp>
+#include <boost/variant.hpp>
+#include <cassert>
+
+struct to_long_double_functor: boost::static_visitor<long double> {
+ template <class T>
+ long double operator()(const T& v) const {
+ // Lexical cast has many optimizations including optimizations for situations that usually
+ // occur in generic programming, like std::string to std::string or arithmetic type to arithmetic type conversion.
+ return boost::lexical_cast<long double>(v);
+ }
+};
+
+// Throws `boost::bad_lexical_cast` if value of the variant is not convertible to `long double`
+template <class Variant>
+long double to_long_double(const Variant& v) {
+ return boost::apply_visitor(to_long_double_functor(), v);
+}
+
+int main() {
+ boost::variant<char, int, std::string> v1('0'), v2("10.0001"), v3(1);
+
+ const long double sum = to_long_double(v1) + to_long_double(v2) + to_long_double(v3);
+ const int ret = (sum > 11 && sum < 11.1 ? 0 : 1);
+ assert(ret == 0);
+ return ret;
+}
+
+//] [/lexical_cast_variant_to_long_double]