stream.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <boost/convert.hpp>
  5. #include <boost/convert/lexical_cast.hpp>
  6. //[stream_headers1
  7. #include <boost/convert.hpp>
  8. #include <boost/convert/stream.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. using std::string;
  11. using boost::convert;
  12. struct boost::cnv::by_default : boost::cnv::cstream {};
  13. //]
  14. //[stream_headers2
  15. namespace cnv = boost::cnv;
  16. namespace arg = boost::cnv::parameter;
  17. //]
  18. #include "../test/test.hpp"
  19. static
  20. void
  21. example1()
  22. {
  23. //[stream_example1
  24. int i2 = convert<int>("123").value(); // Throws when fails.
  25. int i3 = convert<int>("uhm").value_or(-1); // Returns -1 when fails.
  26. string s2 = convert<string>(123).value();
  27. BOOST_TEST(i2 == 123);
  28. BOOST_TEST(i3 == -1);
  29. BOOST_TEST(s2 == "123");
  30. //]
  31. }
  32. static
  33. void
  34. example2()
  35. {
  36. //[stream_example2
  37. boost::cnv::cstream ccnv;
  38. boost::cnv::wstream wcnv;
  39. int v01 = convert<int>(" FF", ccnv(std::hex)(std::skipws)).value_or(0);
  40. int v02 = convert<int>(L" F", wcnv(std::hex)(std::skipws)).value_or(0);
  41. int v03 = convert<int>(" FF", ccnv(std::dec)(std::skipws)).value_or(-5);
  42. int v04 = convert<int>(L" F", wcnv(std::dec)(std::skipws)).value_or(-5);
  43. BOOST_TEST(v01 == 255); // "FF"
  44. BOOST_TEST(v02 == 15); // L"F"
  45. BOOST_TEST(v03 == -5); // Failed to convert "FF" as decimal.
  46. BOOST_TEST(v04 == -5); // Failed to convert L"F" as decimal.
  47. //]
  48. //[stream_example3
  49. ccnv(std::showbase)(std::uppercase)(std::hex);
  50. BOOST_TEST(convert<string>(255, ccnv, "bad") == "0XFF");
  51. BOOST_TEST(convert<string>( 15, ccnv, "bad") == "0XF");
  52. //]
  53. //[stream_example4
  54. ccnv(arg::base = cnv::base::dec)
  55. (arg::uppercase = true)
  56. (arg::notation = cnv::notation::scientific);
  57. //]
  58. //[stream_example5
  59. ccnv(std::dec)(std::uppercase)(std::scientific);
  60. //]
  61. }
  62. static
  63. void
  64. example6()
  65. {
  66. //[stream_example6
  67. boost::cnv::cstream cnv1;
  68. boost::cnv::lexical_cast cnv2;
  69. change chg = change::up;
  70. string s1 = convert<string>(chg, cnv1, "bad"); // Input type (change) deduced
  71. string s2 = convert<string, change>(change::dn, cnv1, "bad"); // Input type (change) enforced
  72. BOOST_TEST(convert<change>("up", cnv1, change::no) == change::up);
  73. BOOST_TEST(convert<change>("up", cnv2, change::no) == change::up);
  74. BOOST_TEST(s1 == "up");
  75. BOOST_TEST(s2 == "dn");
  76. //]
  77. }
  78. int
  79. main(int, char const* [])
  80. {
  81. example1();
  82. example2();
  83. example6();
  84. return boost::report_errors();
  85. }