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