base.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 const>::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) { locale_ = arg[ARG:: locale]; return dncast(); }
  72. BOOST_CNV_PARAM (base, cnv::base) { base_ = arg[ARG:: base]; return dncast(); }
  73. BOOST_CNV_PARAM (adjust, cnv::adjust) { adjust_ = arg[ARG:: adjust]; return dncast(); }
  74. BOOST_CNV_PARAM (precision, int) { precision_ = arg[ARG::precision]; return dncast(); }
  75. BOOST_CNV_PARAM (uppercase, bool) { uppercase_ = arg[ARG::uppercase]; return dncast(); }
  76. BOOST_CNV_PARAM (skipws, bool) { skipws_ = arg[ARG:: skipws]; return dncast(); }
  77. BOOST_CNV_PARAM (width, int) { width_ = arg[ARG:: width]; return dncast(); }
  78. BOOST_CNV_PARAM (fill, char) { fill_ = arg[ARG:: fill]; return dncast(); }
  79. protected:
  80. cnvbase()
  81. :
  82. skipws_ (false),
  83. precision_ (0),
  84. uppercase_ (false),
  85. width_ (0),
  86. fill_ (' '),
  87. base_ (boost::cnv::base::dec),
  88. adjust_ (boost::cnv::adjust::right)
  89. {}
  90. template<typename string_type, typename out_type>
  91. void
  92. str_to_(string_type const& str, optional<out_type>& result_out) const
  93. {
  94. cnv::range<string_type const> range (str);
  95. if (skipws_)
  96. for (; !range.empty() && cnv::is_space(*range.begin()); ++range);
  97. if (range.empty()) return;
  98. if (cnv::is_space(*range.begin())) return;
  99. dncast().str_to(range, result_out);
  100. }
  101. template<typename in_type, typename string_type>
  102. void
  103. to_str_(in_type value_in, optional<string_type>& result_out) const
  104. {
  105. using char_type = typename cnv::range<string_type>::value_type;
  106. using range_type = cnv::range<char_type*>;
  107. using buf_type = char_type[bufsize_];
  108. buf_type buf;
  109. range_type range = dncast().to_str(value_in, buf);
  110. char_type* beg = range.begin();
  111. char_type* end = range.end();
  112. int str_size = end - beg;
  113. if (str_size <= 0)
  114. return;
  115. if (uppercase_)
  116. for (char_type* p = beg; p < end; ++p) *p = cnv::to_upper(*p);
  117. if (width_)
  118. {
  119. int num_fill = (std::max)(0, int(width_ - (end - beg)));
  120. int num_left = adjust_ == cnv::adjust::left ? 0
  121. : adjust_ == cnv::adjust::right ? num_fill
  122. : (num_fill / 2);
  123. int num_right = num_fill - num_left;
  124. bool move = (beg < buf + num_left) // No room for left fillers
  125. || (buf + bufsize_ < end + num_right); // No room for right fillers
  126. if (move)
  127. {
  128. std::memmove(buf + num_left, beg, str_size * sizeof(char_type));
  129. beg = buf + num_left;
  130. end = beg + str_size;
  131. }
  132. for (int k = 0; k < num_left; *(--beg) = fill_, ++k);
  133. for (int k = 0; k < num_right; *(end++) = fill_, ++k);
  134. }
  135. result_out = string_type(beg, end);
  136. }
  137. derived_type const& dncast () const { return *static_cast<derived_type const*>(this); }
  138. derived_type& dncast () { return *static_cast<derived_type*>(this); }
  139. // ULONG_MAX(8 bytes) = 18446744073709551615 (20(10) or 32(2) characters)
  140. // double (8 bytes) max is 316 chars
  141. static int const bufsize_ = 512;
  142. bool skipws_;
  143. int precision_;
  144. bool uppercase_;
  145. int width_;
  146. int fill_;
  147. cnv::base base_;
  148. cnv::adjust adjust_;
  149. // std::locale locale_;
  150. };
  151. #undef BOOST_CNV_TO_STRING
  152. #undef BOOST_CNV_STRING_TO
  153. #undef BOOST_CNV_PARAM
  154. #endif // BOOST_CONVERT_CONVERTER_BASE_HPP