nonfinite_num_facet_serialization.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /** nonfinite_num_facet_serialization.cpp
  2. *
  3. * Copyright (c) 2011 Francois Mauger
  4. * Copyright (c) 2011 Paul A. Bristow
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt
  8. * or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * This sample program by Francois Mauger illustrates how to use the
  11. * `boost/math/nonfinite_num_facets.hpp' material from the original
  12. * Floating Point Utilities contribution by Johan Rade. Here it is
  13. * shown how non finite floating number can be serialized and
  14. * deserialized from I/O streams and/or Boost text/XML archives. It
  15. * produces two archives stored in `test.txt' and `test.xml' files.
  16. *
  17. * Tested with Boost 1.44, gcc 4.4.1, Linux/i686 (32bits).
  18. * Tested with Boost.1.46.1 MSVC 10.0 32 bit.
  19. */
  20. #ifdef _MSC_VER
  21. # pragma warning(push)
  22. //# pragma warning(disable : 4100) // unreferenced formal parameter.
  23. #endif
  24. #include <iostream>
  25. #include <sstream>
  26. #include <fstream>
  27. #include <limits>
  28. #include <boost/cstdint.hpp>
  29. #include <boost/serialization/nvp.hpp>
  30. #include <boost/archive/text_oarchive.hpp>
  31. #include <boost/archive/text_iarchive.hpp>
  32. #include <boost/archive/xml_oarchive.hpp>
  33. #include <boost/archive/xml_iarchive.hpp>
  34. #include <boost/archive/codecvt_null.hpp>
  35. // from the Floating Point Utilities :
  36. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  37. static const char sep = ','; // Separator of bracketed float and double values.
  38. // Use max_digits10 (or equivalent) to obtain
  39. // all potentially significant decimal digits for the floating-point types.
  40. #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
  41. std::streamsize max_digits10_float = 2 + std::numeric_limits<float>::digits * 30103UL / 100000UL;
  42. std::streamsize max_digits10_double = 2 + std::numeric_limits<double>::digits * 30103UL / 100000UL;
  43. #else
  44. // Can use new C++0X max_digits10 (the maximum potentially significant digits).
  45. std::streamsize max_digits10_float = std::numeric_limits<float>::max_digits10;
  46. std::streamsize max_digits10_double = std::numeric_limits<double>::max_digits10;
  47. #endif
  48. /* A class with a float and a double */
  49. struct foo
  50. {
  51. foo () : fvalue (3.1415927F), dvalue (3.1415926535897931)
  52. { // Construct using 32 and 64-bit max_digits10 decimal digits value of pi.
  53. }
  54. // Set the values at -infinity :
  55. void minus_infinity ()
  56. {
  57. fvalue = -std::numeric_limits<float>::infinity ();
  58. dvalue = -std::numeric_limits<double>::infinity ();
  59. return;
  60. }
  61. // Set the values at +infinity :
  62. void plus_infinity ()
  63. {
  64. fvalue = +std::numeric_limits<float>::infinity ();
  65. dvalue = +std::numeric_limits<double>::infinity ();
  66. return;
  67. }
  68. // Set the values at NaN :
  69. void nan ()
  70. {
  71. fvalue = +std::numeric_limits<float>::quiet_NaN ();
  72. dvalue = +std::numeric_limits<double>::quiet_NaN ();
  73. return;
  74. }
  75. // Print :
  76. void print (std::ostream & a_out, const std::string & a_title)
  77. {
  78. if (a_title.empty ()) a_out << "foo";
  79. else a_out << a_title;
  80. a_out << " : " << std::endl;
  81. a_out << "|-- " << "fvalue = ";
  82. a_out.precision (7);
  83. a_out << fvalue << std::endl;
  84. a_out << "`-- " << "dvalue = ";
  85. a_out.precision (15);
  86. a_out << dvalue << std::endl;
  87. return;
  88. }
  89. // I/O operators :
  90. friend std::ostream & operator<< (std::ostream & a_out, const foo & a_foo);
  91. friend std::istream & operator>> (std::istream & a_in, foo & a_foo);
  92. // Boost serialization :
  93. template <class Archive>
  94. void serialize (Archive & ar, int /*version*/)
  95. {
  96. ar & BOOST_SERIALIZATION_NVP (fvalue);
  97. ar & BOOST_SERIALIZATION_NVP (dvalue);
  98. return;
  99. }
  100. // Attributes :
  101. float fvalue; // Single precision floating-point number.
  102. double dvalue; // Double precision floating-point number.
  103. };
  104. std::ostream & operator<< (std::ostream & a_out, const foo & a_foo)
  105. { // Output bracketed FPs, for example "(3.1415927,3.1415926535897931)"
  106. a_out.precision (max_digits10_float);
  107. a_out << "(" << a_foo.fvalue << sep ;
  108. a_out.precision (max_digits10_double);
  109. a_out << a_foo.dvalue << ")";
  110. return a_out;
  111. }
  112. std::istream & operator>> (std::istream & a_in, foo & a_foo)
  113. { // Input bracketed floating-point values into a foo structure,
  114. // for example from "(3.1415927,3.1415926535897931)"
  115. char c = 0;
  116. a_in.get (c);
  117. if (c != '(')
  118. {
  119. std::cerr << "ERROR: operator>> No ( " << std::endl;
  120. a_in.setstate(std::ios::failbit);
  121. return a_in;
  122. }
  123. float f;
  124. a_in >> std::ws >> f;
  125. if (! a_in)
  126. {
  127. return a_in;
  128. }
  129. a_in >> std::ws;
  130. a_in.get (c);
  131. if (c != sep)
  132. {
  133. std::cerr << "ERROR: operator>> c='" << c << "'" << std::endl;
  134. std::cerr << "ERROR: operator>> No '" << sep << "'" << std::endl;
  135. a_in.setstate(std::ios::failbit);
  136. return a_in;
  137. }
  138. double d;
  139. a_in >> std::ws >> d;
  140. if (! a_in)
  141. {
  142. return a_in;
  143. }
  144. a_in >> std::ws;
  145. a_in.get (c);
  146. if (c != ')')
  147. {
  148. std::cerr << "ERROR: operator>> No ) " << std::endl;
  149. a_in.setstate(std::ios::failbit);
  150. return a_in;
  151. }
  152. a_foo.fvalue = f;
  153. a_foo.dvalue = d;
  154. return a_in;
  155. }
  156. int main (void)
  157. {
  158. std::clog << std::endl
  159. << "Nonfinite_serialization.cpp' example program." << std::endl;
  160. #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
  161. std::cout << "BOOST_NO_CXX11_NUMERIC_LIMITS is defined, so no max_digits10 available either,"
  162. "using our own version instead." << std::endl;
  163. #endif
  164. std::cout << "std::numeric_limits<float>::max_digits10 is " << max_digits10_float << std::endl;
  165. std::cout << "std::numeric_limits<double>::max_digits10 is " << max_digits10_double << std::endl;
  166. std::locale the_default_locale (std::locale::classic (),
  167. new boost::archive::codecvt_null<char>);
  168. // Demonstrate use of nonfinite facets with stringstreams.
  169. {
  170. std::clog << "Construct some foo structures with a finite and nonfinites." << std::endl;
  171. foo f0;
  172. foo f1; f1.minus_infinity ();
  173. foo f2; f2.plus_infinity ();
  174. foo f3; f3.nan ();
  175. // Display them.
  176. f0.print (std::clog, "f0");
  177. f1.print (std::clog, "f1");
  178. f2.print (std::clog, "f2");
  179. f3.print (std::clog, "f3");
  180. std::clog << " Write to a string buffer." << std::endl;
  181. std::ostringstream oss;
  182. std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
  183. oss.imbue (the_out_locale);
  184. oss.precision (max_digits10_double);
  185. oss << f0 << f1 << f2 << f3;
  186. std::clog << "Output is: `" << oss.str () << "'" << std::endl;
  187. std::clog << "Done output to ostringstream." << std::endl;
  188. }
  189. {
  190. std::clog << "Read foo structures from a string buffer." << std::endl;
  191. std::string the_string = "(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)";
  192. std::clog << "Input is: `" << the_string << "'" << std::endl;
  193. std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
  194. std::istringstream iss (the_string);
  195. iss.imbue (the_in_locale);
  196. foo f0, f1, f2, f3;
  197. iss >> f0 >> f1 >> f2 >> f3;
  198. if (! iss)
  199. {
  200. std::cerr << "Format error !" << std::endl;
  201. }
  202. else
  203. {
  204. std::cerr << "Read OK." << std::endl;
  205. f0.print (std::clog, "f0");
  206. f1.print (std::clog, "f1");
  207. f2.print (std::clog, "f2");
  208. f3.print (std::clog, "f3");
  209. }
  210. std::clog << "Done input from istringstream." << std::endl;
  211. }
  212. { // Demonstrate use of nonfinite facets for Serialization with Boost text archives.
  213. std::clog << "Serialize (using Boost text archive)." << std::endl;
  214. // Construct some foo structures with a finite and nonfinites.
  215. foo f0;
  216. foo f1; f1.minus_infinity ();
  217. foo f2; f2.plus_infinity ();
  218. foo f3; f3.nan ();
  219. // Display them.
  220. f0.print (std::clog, "f0");
  221. f1.print (std::clog, "f1");
  222. f2.print (std::clog, "f2");
  223. f3.print (std::clog, "f3");
  224. std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
  225. // Use a temporary folder .temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
  226. std::ofstream fout ("./.temps/nonfinite_archive_test.txt");
  227. fout.imbue (the_out_locale);
  228. boost::archive::text_oarchive toar (fout, boost::archive::no_codecvt);
  229. // Write to archive.
  230. toar & f0;
  231. toar & f1;
  232. toar & f2;
  233. toar & f3;
  234. std::clog << "Done." << std::endl;
  235. }
  236. {
  237. std::clog << "Deserialize (Boost text archive)..." << std::endl;
  238. std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
  239. // Use a temporary folder .temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
  240. std::ifstream fin ("./.temps/nonfinite_archive_test.txt");
  241. fin.imbue (the_in_locale);
  242. boost::archive::text_iarchive tiar (fin, boost::archive::no_codecvt);
  243. foo f0, f1, f2, f3;
  244. // Read from archive.
  245. tiar & f0;
  246. tiar & f1;
  247. tiar & f2;
  248. tiar & f3;
  249. // Display foos.
  250. f0.print (std::clog, "f0");
  251. f1.print (std::clog, "f1");
  252. f2.print (std::clog, "f2");
  253. f3.print (std::clog, "f3");
  254. std::clog << "Done." << std::endl;
  255. }
  256. { // Demonstrate use of nonfinite facets for Serialization with Boost XML Archive.
  257. std::clog << "Serialize (Boost XML archive)..." << std::endl;
  258. // Construct some foo structures with a finite and nonfinites.
  259. foo f0;
  260. foo f1; f1.minus_infinity ();
  261. foo f2; f2.plus_infinity ();
  262. foo f3; f3.nan ();
  263. // Display foos.
  264. f0.print (std::clog, "f0");
  265. f1.print (std::clog, "f1");
  266. f2.print (std::clog, "f2");
  267. f3.print (std::clog, "f3");
  268. std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
  269. // Use a temporary folder /.temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
  270. std::ofstream fout ("./.temps/nonfinite_XML_archive_test.txt");
  271. fout.imbue (the_out_locale);
  272. boost::archive::xml_oarchive xoar (fout, boost::archive::no_codecvt);
  273. xoar & BOOST_SERIALIZATION_NVP (f0);
  274. xoar & BOOST_SERIALIZATION_NVP (f1);
  275. xoar & BOOST_SERIALIZATION_NVP (f2);
  276. xoar & BOOST_SERIALIZATION_NVP (f3);
  277. std::clog << "Done." << std::endl;
  278. }
  279. {
  280. std::clog << "Deserialize (Boost XML archive)..." << std::endl;
  281. std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
  282. // Use a temporary folder /.temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
  283. std::ifstream fin ("./.temps/nonfinite_XML_archive_test.txt"); // Previously written above.
  284. fin.imbue (the_in_locale);
  285. boost::archive::xml_iarchive xiar (fin, boost::archive::no_codecvt);
  286. foo f0, f1, f2, f3;
  287. xiar & BOOST_SERIALIZATION_NVP (f0);
  288. xiar & BOOST_SERIALIZATION_NVP (f1);
  289. xiar & BOOST_SERIALIZATION_NVP (f2);
  290. xiar & BOOST_SERIALIZATION_NVP (f3);
  291. f0.print (std::clog, "f0");
  292. f1.print (std::clog, "f1");
  293. f2.print (std::clog, "f2");
  294. f3.print (std::clog, "f3");
  295. std::clog << "Done." << std::endl;
  296. }
  297. std::clog << "End nonfinite_serialization.cpp' example program." << std::endl;
  298. return 0;
  299. }
  300. /*
  301. Output:
  302. Nonfinite_serialization.cpp' example program.
  303. std::numeric_limits<float>::max_digits10 is 8
  304. std::numeric_limits<double>::max_digits10 is 17
  305. Construct some foo structures with a finite and nonfinites.
  306. f0 :
  307. |-- fvalue = 3.141593
  308. `-- dvalue = 3.14159265358979
  309. f1 :
  310. |-- fvalue = -1.#INF
  311. `-- dvalue = -1.#INF
  312. f2 :
  313. |-- fvalue = 1.#INF
  314. `-- dvalue = 1.#INF
  315. f3 :
  316. |-- fvalue = 1.#QNAN
  317. `-- dvalue = 1.#QNAN
  318. Write to a string buffer.
  319. Output is: `(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)'
  320. Done output to ostringstream.
  321. Read foo structures from a string buffer.
  322. Input is: `(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)'
  323. Read OK.
  324. f0 :
  325. |-- fvalue = 3.141593
  326. `-- dvalue = 3.14159265358979
  327. f1 :
  328. |-- fvalue = -1.#INF
  329. `-- dvalue = -1.#INF
  330. f2 :
  331. |-- fvalue = 1.#INF
  332. `-- dvalue = 1.#INF
  333. f3 :
  334. |-- fvalue = 1.#QNAN
  335. `-- dvalue = 1.#QNAN
  336. Done input from istringstream.
  337. Serialize (using Boost text archive).
  338. f0 :
  339. |-- fvalue = 3.141593
  340. `-- dvalue = 3.14159265358979
  341. f1 :
  342. |-- fvalue = -1.#INF
  343. `-- dvalue = -1.#INF
  344. f2 :
  345. |-- fvalue = 1.#INF
  346. `-- dvalue = 1.#INF
  347. f3 :
  348. |-- fvalue = 1.#QNAN
  349. `-- dvalue = 1.#QNAN
  350. Done.
  351. Deserialize (Boost text archive)...
  352. f0 :
  353. |-- fvalue = 3.141593
  354. `-- dvalue = 3.14159265358979
  355. f1 :
  356. |-- fvalue = -1.#INF
  357. `-- dvalue = -1.#INF
  358. f2 :
  359. |-- fvalue = 1.#INF
  360. `-- dvalue = 1.#INF
  361. f3 :
  362. |-- fvalue = 1.#QNAN
  363. `-- dvalue = 1.#QNAN
  364. Done.
  365. Serialize (Boost XML archive)...
  366. f0 :
  367. |-- fvalue = 3.141593
  368. `-- dvalue = 3.14159265358979
  369. f1 :
  370. |-- fvalue = -1.#INF
  371. `-- dvalue = -1.#INF
  372. f2 :
  373. |-- fvalue = 1.#INF
  374. `-- dvalue = 1.#INF
  375. f3 :
  376. |-- fvalue = 1.#QNAN
  377. `-- dvalue = 1.#QNAN
  378. Done.
  379. Deserialize (Boost XML archive)...
  380. f0 :
  381. |-- fvalue = 3.141593
  382. `-- dvalue = 3.14159265358979
  383. f1 :
  384. |-- fvalue = -1.#INF
  385. `-- dvalue = -1.#INF
  386. f2 :
  387. |-- fvalue = 1.#INF
  388. `-- dvalue = 1.#INF
  389. f3 :
  390. |-- fvalue = 1.#QNAN
  391. `-- dvalue = 1.#QNAN
  392. Done.
  393. End nonfinite_serialization.cpp' example program.
  394. */