base.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef BOOST_CONVERT_CONVERTER_BASE_HPP
  5. #define BOOST_CONVERT_CONVERTER_BASE_HPP
  6. #include <boost/convert/parameters.hpp>
  7. #include <boost/convert/detail/is_string.hpp>
  8. #include <cstring>
  9. namespace boost { namespace cnv
  10. {
  11. namespace ARG = boost::cnv::parameter;
  12. template<typename> struct cnvbase;
  13. }}
  14. #define BOOST_CNV_TO_STRING \
  15. template<typename string_type> \
  16. typename boost::enable_if<cnv::is_string<string_type>, void>::type \
  17. operator()
  18. #define BOOST_CNV_STRING_TO \
  19. template<typename string_type> \
  20. typename boost::enable_if<cnv::is_string<string_type>, void>::type \
  21. operator()
  22. #define BOOST_CNV_PARAM(param_name, param_type) \
  23. derived_type& operator()(boost::parameter::aux::tag<ARG::type::param_name, param_type>::type const& arg)
  24. template<typename derived_type>
  25. struct boost::cnv::cnvbase
  26. {
  27. using this_type = cnvbase;
  28. using int_type = int;
  29. using uint_type = unsigned int;
  30. using lint_type = long int;
  31. using ulint_type = unsigned long int;
  32. using sint_type = short int;
  33. using usint_type = unsigned short int;
  34. using llint_type = long long int;
  35. using ullint_type = unsigned long long int;
  36. using flt_type = float;
  37. using dbl_type = double;
  38. using ldbl_type = long double;
  39. // Integration of user-types via operator>>()
  40. template<typename type_in, typename type_out>
  41. void
  42. operator()(type_in const& in, boost::optional<type_out>& out) const
  43. {
  44. in >> out;
  45. }
  46. // Basic type to string
  47. BOOST_CNV_TO_STRING ( int_type v, optional<string_type>& r) const { to_str_(v, r); }
  48. BOOST_CNV_TO_STRING ( uint_type v, optional<string_type>& r) const { to_str_(v, r); }
  49. BOOST_CNV_TO_STRING ( lint_type v, optional<string_type>& r) const { to_str_(v, r); }
  50. BOOST_CNV_TO_STRING ( llint_type v, optional<string_type>& r) const { to_str_(v, r); }
  51. BOOST_CNV_TO_STRING ( ulint_type v, optional<string_type>& r) const { to_str_(v, r); }
  52. BOOST_CNV_TO_STRING (ullint_type v, optional<string_type>& r) const { to_str_(v, r); }
  53. BOOST_CNV_TO_STRING ( sint_type v, optional<string_type>& r) const { to_str_(v, r); }
  54. BOOST_CNV_TO_STRING ( usint_type v, optional<string_type>& r) const { to_str_(v, r); }
  55. BOOST_CNV_TO_STRING ( flt_type v, optional<string_type>& r) const { to_str_(v, r); }
  56. BOOST_CNV_TO_STRING ( dbl_type v, optional<string_type>& r) const { to_str_(v, r); }
  57. BOOST_CNV_TO_STRING ( ldbl_type v, optional<string_type>& r) const { to_str_(v, r); }
  58. // String to basic type
  59. BOOST_CNV_STRING_TO (string_type const& s, optional< int_type>& r) const { str_to_(s, r); }
  60. BOOST_CNV_STRING_TO (string_type const& s, optional< uint_type>& r) const { str_to_(s, r); }
  61. BOOST_CNV_STRING_TO (string_type const& s, optional< lint_type>& r) const { str_to_(s, r); }
  62. BOOST_CNV_STRING_TO (string_type const& s, optional< llint_type>& r) const { str_to_(s, r); }
  63. BOOST_CNV_STRING_TO (string_type const& s, optional< ulint_type>& r) const { str_to_(s, r); }
  64. BOOST_CNV_STRING_TO (string_type const& s, optional<ullint_type>& r) const { str_to_(s, r); }
  65. BOOST_CNV_STRING_TO (string_type const& s, optional< sint_type>& r) const { str_to_(s, r); }
  66. BOOST_CNV_STRING_TO (string_type const& s, optional< usint_type>& r) const { str_to_(s, r); }
  67. BOOST_CNV_STRING_TO (string_type const& s, optional< flt_type>& r) const { str_to_(s, r); }
  68. BOOST_CNV_STRING_TO (string_type const& s, optional< dbl_type>& r) const { str_to_(s, r); }
  69. BOOST_CNV_STRING_TO (string_type const& s, optional< ldbl_type>& r) const { str_to_(s, r); }
  70. // Formatters
  71. // BOOST_CNV_PARAM (locale, std::locale const) { locale_ = arg[ARG:: locale]; return dncast(); }
  72. BOOST_CNV_PARAM (base, cnv::base const) { base_ = arg[ARG:: base]; return dncast(); }
  73. BOOST_CNV_PARAM (adjust, cnv::adjust const) { adjust_ = arg[ARG:: adjust]; return dncast(); }
  74. BOOST_CNV_PARAM (precision, int const) { precision_ = arg[ARG::precision]; return dncast(); }
  75. BOOST_CNV_PARAM (precision, int) { precision_ = arg[ARG::precision]; return dncast(); }
  76. BOOST_CNV_PARAM (uppercase, bool const) { uppercase_ = arg[ARG::uppercase]; return dncast(); }
  77. BOOST_CNV_PARAM (skipws, bool const) { skipws_ = arg[ARG:: skipws]; return dncast(); }
  78. BOOST_CNV_PARAM (width, int const) { width_ = arg[ARG:: width]; return dncast(); }
  79. BOOST_CNV_PARAM (fill, char const) { fill_ = arg[ARG:: fill]; return dncast(); }
  80. protected:
  81. cnvbase()
  82. :
  83. skipws_ (false),
  84. precision_ (0),
  85. uppercase_ (false),
  86. width_ (0),
  87. fill_ (' '),
  88. base_ (boost::cnv::base::dec),
  89. adjust_ (boost::cnv::adjust::right)
  90. {}
  91. template<typename string_type, typename out_type>
  92. void
  93. str_to_(string_type const& str, optional<out_type>& result_out) const
  94. {
  95. cnv::range<string_type const> range (str);
  96. if (skipws_)
  97. for (; !range.empty() && cnv::is_space(*range.begin()); ++range);
  98. if (range.empty()) return;
  99. if (cnv::is_space(*range.begin())) return;
  100. dncast().str_to(range, result_out);
  101. }
  102. template<typename in_type, typename string_type>
  103. void
  104. to_str_(in_type value_in, optional<string_type>& result_out) const
  105. {
  106. using char_type = typename cnv::range<string_type>::value_type;
  107. using range_type = cnv::range<char_type*>;
  108. using buf_type = char_type[bufsize_];
  109. buf_type buf;
  110. range_type range = dncast().to_str(value_in, buf);
  111. char_type* beg = range.begin();
  112. char_type* end = range.end();
  113. int str_size = end - beg;
  114. if (str_size <= 0)
  115. return;
  116. if (uppercase_)
  117. for (char_type* p = beg; p < end; ++p) *p = cnv::to_upper(*p);
  118. if (width_)
  119. {
  120. int num_fill = (std::max)(0, int(width_ - (end - beg)));
  121. int num_left = adjust_ == cnv::adjust::left ? 0
  122. : adjust_ == cnv::adjust::right ? num_fill
  123. : (num_fill / 2);
  124. int num_right = num_fill - num_left;
  125. bool move = (beg < buf + num_left) // No room for left fillers
  126. || (buf + bufsize_ < end + num_right); // No room for right fillers
  127. if (move)
  128. {
  129. std::memmove(buf + num_left, beg, str_size * sizeof(char_type));
  130. beg = buf + num_left;
  131. end = beg + str_size;
  132. }
  133. for (int k = 0; k < num_left; *(--beg) = fill_, ++k);
  134. for (int k = 0; k < num_right; *(end++) = fill_, ++k);
  135. }
  136. result_out = string_type(beg, end);
  137. }
  138. derived_type const& dncast () const { return *static_cast<derived_type const*>(this); }
  139. derived_type& dncast () { return *static_cast<derived_type*>(this); }
  140. // ULONG_MAX(8 bytes) = 18446744073709551615 (20(10) or 32(2) characters)
  141. // double (8 bytes) max is 316 chars
  142. static int const bufsize_ = 512;
  143. bool skipws_;
  144. int precision_;
  145. bool uppercase_;
  146. int width_;
  147. int fill_;
  148. cnv::base base_;
  149. cnv::adjust adjust_;
  150. // std::locale locale_;
  151. };
  152. #undef BOOST_CNV_TO_STRING
  153. #undef BOOST_CNV_STRING_TO
  154. #undef BOOST_CNV_PARAM
  155. #endif // BOOST_CONVERT_CONVERTER_BASE_HPP