base.hpp 8.1 KB

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