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