From e0737c56c1e384e05064a0c6e1ac46979371041b Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Wed, 19 Dec 2018 22:58:32 +0100 Subject: New upstream version 2.0.0+repack --- .../example/nonfinite_serialization_archives.cpp | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 boost/libs/math/example/nonfinite_serialization_archives.cpp (limited to 'boost/libs/math/example/nonfinite_serialization_archives.cpp') diff --git a/boost/libs/math/example/nonfinite_serialization_archives.cpp b/boost/libs/math/example/nonfinite_serialization_archives.cpp new file mode 100644 index 00000000..355108f9 --- /dev/null +++ b/boost/libs/math/example/nonfinite_serialization_archives.cpp @@ -0,0 +1,136 @@ +/** nonfinite_serialization_archives.cpp +* +* Copyright (c) 2011 Paul A. Bristow +* +* Distributed under the Boost Software License, Version 1.0. +* (See accompanying file LICENSE_1_0.txt +* or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* This very simple program illustrates how to use the +* `boost/math/nonfinite_num_facets.hpp' to obtain C99 +* representation of infinity and NaN. +* From the original Floating Point Utilities contribution by Johan Rade. +* Floating Point Utility library has been accepted into Boost, +* but the utilities are incorporated into Boost.Math library. +* +\file + +\brief A simple example of using non_finite_num facet for +C99 standard output of infinity and NaN in serialization archives. + +\detail This example shows how to create a C99 non-finite locale, +and imbue input and output streams with the non_finite_num put and get facets. +This allow output and input of infinity and NaN in a Standard portable way, +This permits 'loop-back' of output back into input (and portably across different system too). +This is particularly useful when used with Boost.Seralization so that non-finite NaNs and infinity +values in text and xml archives can be handled correctly and portably. + +*/ + + +#ifdef _MSC_VER +# pragma warning(disable : 4127) // conditional expression is constant. +#endif + + +#include +using boost::archive::text_oarchive; +#include +using boost::archive::codecvt_null; +using boost::archive::no_codecvt; + +#include +using boost::math::nonfinite_num_get; +using boost::math::nonfinite_num_put; + +#include +using std::cout; +using std::endl; +using std::cerr; + +#include +using std::setw; +using std::left; +using std::right; +using std::internal; + +#include +using std::string; + +#include +using std::istringstream; + +#include +using std::ofstream; + +#include +using std::numeric_limits; + +#include +using std::locale; + + +/* +Use with serialization archives. + +It is important that the same locale is used +when an archive is saved and when it is loaded. +Otherwise, loading the archive may fail. + +By default, archives are saved and loaded with a classic C locale with a +`boost::archive::codecvt_null` facet added. +Normally you do not have to worry about that. +The constructors for the archive classes, as a side-effect, +imbue the stream with such a locale. + +However, if you want to use the facets `nonfinite_num_put` and `nonfinite_num_get` +with archives,`then you have to manage the locale manually. + +That is done by calling the archive constructor with the flag `boost::archive::no_codecvt`. +Then the archive constructor will not imbue the stream with a new locale. + +The following code shows how to use `nonfinite_num_put` with a `text_oarchive`: + +*/ + +int main() +{ + + if((std::numeric_limits::has_infinity == false) || (std::numeric_limits::infinity() == 0)) + { + std::cout << "Infinity not supported on this platform." << std::endl; + return 0; + } + + if((std::numeric_limits::has_quiet_NaN == false) || (std::numeric_limits::quiet_NaN() == 0)) + { + std::cout << "NaN not supported on this platform." << std::endl; + return 0; + } + + locale default_locale(locale::classic(), new boost::archive::codecvt_null); + // codecvt_null so the archive constructor will not imbue the stream with a new locale. + + locale my_locale(default_locale, new nonfinite_num_put); + // Add nonfinite_num_put facet to locale. + + ofstream ofs("test.txt"); + ofs.imbue(my_locale); + + boost::archive::text_oarchive oa(ofs, no_codecvt); + + double x = numeric_limits::infinity(); + oa & x; + + +} // int main() + + +/* The same method works with nonfinite_num_get and text_iarchive. + +If you use the trap_infinity and trap_nan flags with a serialization archive, +then you must set the exception mask of the stream. +Serialization archives do not check the stream state. + + +*/ -- cgit v1.2.3