base.hpp 8.4 KB

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