base.hpp 8.5 KB

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