getting_started.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) 2009-2016 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifdef _MSC_VER
  5. # pragma warning(disable : 4127) // conditional expression is constant.
  6. # pragma warning(disable : 4189) // local variable is initialized but not referenced.
  7. # pragma warning(disable : 4100) // unreferenced formal parameter.
  8. # pragma warning(disable : 4714) // marked as __forceinline not inlined.
  9. #endif
  10. namespace { namespace local
  11. {
  12. #if defined(_MSC_VER)
  13. static bool const is_msc = true;
  14. #else
  15. static bool const is_msc = false;
  16. #endif
  17. }}
  18. #include <boost/bind.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. static
  21. void
  22. log(char const*)
  23. {
  24. // Dummy function to demonstrate how problems might be logged.
  25. }
  26. //[getting_started_headers1
  27. #include <boost/convert.hpp>
  28. #include <boost/convert/lexical_cast.hpp>
  29. //]
  30. //[getting_started_using
  31. using std::string;
  32. using boost::lexical_cast;
  33. using boost::convert;
  34. //]
  35. //[getting_started_default_converter
  36. // Definition of the default converter (optional)
  37. struct boost::cnv::by_default : boost::cnv::lexical_cast {};
  38. //]
  39. static
  40. void
  41. getting_started_example1()
  42. {
  43. //[getting_started_example1
  44. try
  45. {
  46. boost::cnv::lexical_cast cnv; // boost::lexical_cast-based converter
  47. int i1 = lexical_cast<int>("123"); // boost::lexical_cast standard deployment
  48. int i2 = convert<int>("123").value(); // boost::convert with the default converter
  49. int i3 = convert<int>("123", cnv).value(); // boost::convert with an explicit converter
  50. string s1 = lexical_cast<string>(123); // boost::lexical_cast standard deployment
  51. string s2 = convert<string>(123).value(); // boost::convert with the default converter
  52. string s3 = convert<string>(123, cnv).value(); // boost::convert with an explicit converter
  53. BOOST_TEST(i1 == 123);
  54. BOOST_TEST(i2 == 123);
  55. BOOST_TEST(i3 == 123);
  56. BOOST_TEST(s1 == "123");
  57. BOOST_TEST(s2 == "123");
  58. BOOST_TEST(s3 == "123");
  59. }
  60. catch (std::exception const& ex)
  61. {
  62. // Please be aware that the conversion requests above can fail.
  63. // Use try'n'catch blocks to handle any exceptions thrown.
  64. // Ignore this at your peril!
  65. std::cerr << "Exception " << ex.what() << std::endl;
  66. }
  67. //] [/getting_started_example1]
  68. }
  69. static
  70. void
  71. getting_started_example2()
  72. {
  73. //[getting_started_example2
  74. // Does not throw. Returns fallback value (-1) when failed.
  75. int i = convert<int>("uhm", boost::cnv::lexical_cast()).value_or(-1);
  76. BOOST_TEST(i == -1); // Conversion failed. 'i' assigned the fallback value.
  77. //]
  78. }
  79. //[getting_started_headers3
  80. #include <boost/convert/strtol.hpp>
  81. #include <boost/convert/spirit.hpp>
  82. //]
  83. static
  84. void
  85. getting_started_example3()
  86. {
  87. //[getting_started_example3
  88. boost::cnv::lexical_cast cnv1;
  89. boost::cnv::strtol cnv2;
  90. boost::cnv::spirit cnv3;
  91. int i1 = convert<int>("123", cnv1).value();
  92. int i2 = convert<int>("123", cnv2).value(); // Two times faster than lexical_cast.
  93. int i3 = convert<int>("123", cnv3).value(); // Four times faster than lexical_cast.
  94. //]
  95. }
  96. //[getting_started_headers4
  97. #include <boost/convert/stream.hpp>
  98. //]
  99. static
  100. void
  101. getting_started_example4()
  102. {
  103. //[getting_started_example4
  104. boost::cnv::cstream cnv;
  105. try
  106. {
  107. int i1 = lexical_cast<int>(" 123"); // Does not work.
  108. BOOST_TEST(!"Never reached");
  109. }
  110. catch (...) {}
  111. int i2 = convert<int>(" 123", cnv(std::skipws)).value(); // Success
  112. string s1 = lexical_cast<string>(12.34567);
  113. string s2 = convert<string>(12.34567, cnv(std::fixed)(std::setprecision(3))).value();
  114. string s3 = convert<string>(12.34567, cnv(std::scientific)(std::setprecision(3))).value();
  115. string expected = local::is_msc ? "1.235e+001" : "1.235e+01";
  116. BOOST_TEST(i2 == 123); // boost::cnv::cstream. Successful conversion of " 123".
  117. BOOST_TEST(s1 == "12.34567"); // boost::lexical_cast. Precision is not configurable.
  118. BOOST_TEST(s2 == "12.346"); // boost::cnv::cstream. Precision was set to 3. Fixed.
  119. BOOST_TEST(s3 == expected); // boost::cnv::cstream. Precision was set to 3. Scientific.
  120. //]
  121. }
  122. static
  123. void
  124. getting_started_example5()
  125. {
  126. //[getting_started_example5
  127. boost::cnv::cstream cnv;
  128. int i1 = lexical_cast<int>("123"); // Throws when conversion fails.
  129. int i2 = convert<int>("123", cnv).value(); // Throws when conversion fails.
  130. int i3 = convert<int>("uhm", cnv).value_or(-1); // Returns -1 when conversion fails.
  131. BOOST_TEST(i1 == 123);
  132. BOOST_TEST(i2 == 123);
  133. BOOST_TEST(i3 == -1);
  134. //]
  135. }
  136. static
  137. void
  138. getting_started_example6()
  139. {
  140. std::string const s1 = "123";
  141. std::string const s2 = "456";
  142. int const default_i1 = 11;
  143. int const default_i2 = 12;
  144. boost::cnv::cstream cnv;
  145. //[getting_started_example6
  146. int i1 = convert<int>(s1, cnv(std::hex)).value_or(-1); // Read as hex
  147. int i2 = convert<int>(s2, cnv(std::dec)).value_or(-1); // Read as decimal
  148. if (i1 == -1) log("bad i1"), i1 = default_i1; // Log failure. Proceed with the default
  149. if (i2 == -1) log("bad i2"), i2 = default_i2; // Log failure. Proceed with the default
  150. // ... proceed
  151. //]
  152. BOOST_TEST(i1 == 291);
  153. BOOST_TEST(i2 == 456);
  154. }
  155. static
  156. void
  157. getting_started_example7()
  158. {
  159. std::string const s1 = "123";
  160. std::string const s2 = "456";
  161. int const default_i1 = 11;
  162. int const default_i2 = 12;
  163. boost::cnv::cstream cnv;
  164. //[getting_started_example7
  165. int i1 = convert<int>(s1, cnv(std::hex)).value_or(default_i1); // If failed, proceed with the default
  166. int i2 = convert<int>(s2, cnv(std::dec)).value_or(default_i2); // If failed, proceed with the default
  167. // ... proceed
  168. //]
  169. BOOST_TEST(i1 == 291);
  170. BOOST_TEST(i2 == 456);
  171. }
  172. static
  173. //[getting_started_example9_func
  174. int
  175. fallback_fun(char const* msg, int fallback_value)
  176. {
  177. // The principal advantage of a fallback_func over a fallback_value
  178. // is that the former is only called when the conversion request fails.
  179. // Consequently, the returned fallback_value is only calculated (which
  180. // potentially might be expensive) when it is absolutely necessary.
  181. log(msg); return fallback_value;
  182. }
  183. //]
  184. static
  185. void
  186. getting_started_example9()
  187. {
  188. std::string const s1 = "123";
  189. std::string const s2 = "456";
  190. int const default_i1 = 11;
  191. int const default_i2 = 12;
  192. //[getting_started_example9
  193. int i1 = convert<int>(s1).value_or_eval(boost::bind(fallback_fun, "bad i1", default_i1));
  194. int i2 = convert<int>(s2).value_or_eval(boost::bind(fallback_fun, "bad i2", default_i2));
  195. // ... proceed
  196. //]
  197. BOOST_TEST(i1 == 123);
  198. BOOST_TEST(i2 == 456);
  199. }
  200. static
  201. void
  202. getting_started_example8()
  203. {
  204. std::string const str = "123";
  205. int const default_i1 = 12;
  206. //[getting_started_example8
  207. int i1 = default_i1;
  208. try
  209. {
  210. i1 = lexical_cast<int>(str);
  211. }
  212. catch (...)
  213. {
  214. log("bad i1");
  215. }
  216. //]
  217. BOOST_TEST(i1 == 123);
  218. }
  219. int
  220. main(int, char const* [])
  221. {
  222. getting_started_example1();
  223. getting_started_example2();
  224. getting_started_example3();
  225. getting_started_example4();
  226. getting_started_example5();
  227. getting_started_example6();
  228. getting_started_example7();
  229. getting_started_example8();
  230. getting_started_example9();
  231. return boost::report_errors();
  232. }