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 <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)                         \
30     derived_type& operator()(boost::parameter::aux::tag<ARG::type::param_name, param_type const>::type const& arg)
31 
32 template<typename derived_type>
33 struct boost::cnv::cnvbase
34 {
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;
47 
48     // Integration of user-types via operator>>()
49     template<typename type_in, typename type_out>
50     void
operator ()boost::cnv::cnvbase51     operator()(type_in const& in, boost::optional<type_out>& out) const
52     {
53         in >> out;
54     }
55 
56     // Basic type to string
BOOST_CNV_TO_STRINGboost::cnv::cnvbase57     BOOST_CNV_TO_STRING (   int_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase58     BOOST_CNV_TO_STRING (  uint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase59     BOOST_CNV_TO_STRING (  lint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase60     BOOST_CNV_TO_STRING ( llint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase61     BOOST_CNV_TO_STRING ( ulint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase62     BOOST_CNV_TO_STRING (ullint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase63     BOOST_CNV_TO_STRING (  sint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase64     BOOST_CNV_TO_STRING ( usint_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase65     BOOST_CNV_TO_STRING (   flt_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase66     BOOST_CNV_TO_STRING (   dbl_type v, optional<string_type>& r) const { to_str_(v, r); }
BOOST_CNV_TO_STRINGboost::cnv::cnvbase67     BOOST_CNV_TO_STRING (  ldbl_type v, optional<string_type>& r) const { to_str_(v, r); }
68     // String to basic type
BOOST_CNV_STRING_TOboost::cnv::cnvbase69     BOOST_CNV_STRING_TO (string_type const& s, optional<   int_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase70     BOOST_CNV_STRING_TO (string_type const& s, optional<  uint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase71     BOOST_CNV_STRING_TO (string_type const& s, optional<  lint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase72     BOOST_CNV_STRING_TO (string_type const& s, optional< llint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase73     BOOST_CNV_STRING_TO (string_type const& s, optional< ulint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase74     BOOST_CNV_STRING_TO (string_type const& s, optional<ullint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase75     BOOST_CNV_STRING_TO (string_type const& s, optional<  sint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase76     BOOST_CNV_STRING_TO (string_type const& s, optional< usint_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase77     BOOST_CNV_STRING_TO (string_type const& s, optional<   flt_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase78     BOOST_CNV_STRING_TO (string_type const& s, optional<   dbl_type>& r) const { str_to_(s, r); }
BOOST_CNV_STRING_TOboost::cnv::cnvbase79     BOOST_CNV_STRING_TO (string_type const& s, optional<  ldbl_type>& r) const { str_to_(s, r); }
80     // Formatters
81 //  BOOST_CNV_PARAM (locale, std::locale) { locale_    = arg[ARG::   locale]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase82     BOOST_CNV_PARAM (base,     cnv::base) { base_      = arg[ARG::     base]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase83     BOOST_CNV_PARAM (adjust, cnv::adjust) { adjust_    = arg[ARG::   adjust]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase84     BOOST_CNV_PARAM (precision,      int) { precision_ = arg[ARG::precision]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase85     BOOST_CNV_PARAM (uppercase,     bool) { uppercase_ = arg[ARG::uppercase]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase86     BOOST_CNV_PARAM (skipws,        bool) { skipws_    = arg[ARG::   skipws]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase87     BOOST_CNV_PARAM (width,          int) { width_     = arg[ARG::    width]; return dncast(); }
BOOST_CNV_PARAMboost::cnv::cnvbase88     BOOST_CNV_PARAM (fill,          char) {  fill_     = arg[ARG::     fill]; return dncast(); }
89 
90     protected:
91 
cnvbaseboost::cnv::cnvbase92     cnvbase()
93     :
94         skipws_     (false),
95         precision_  (0),
96         uppercase_  (false),
97         width_      (0),
98         fill_       (' '),
99         base_       (boost::cnv::base::dec),
100         adjust_     (boost::cnv::adjust::right)
101     {}
102 
103     template<typename string_type, typename out_type>
104     void
str_to_boost::cnv::cnvbase105     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_)
110             for (; !range.empty() && cnv::is_space(*range.begin()); ++range);
111 
112         if (range.empty())                 return;
113         if (cnv::is_space(*range.begin())) return;
114 
115         dncast().str_to(range, result_out);
116     }
117     template<typename in_type, typename string_type>
118     void
to_str_boost::cnv::cnvbase119     to_str_(in_type value_in, optional<string_type>& result_out) const
120     {
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_];
124 
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;
130 
131         if (str_size <= 0)
132             return;
133 
134         if (uppercase_)
135             for (char_type* p = beg; p < end; ++p) *p = cnv::to_upper(*p);
136 
137         if (width_)
138         {
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
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         }
155         result_out = string_type(beg, end);
156     }
157 
dncastboost::cnv::cnvbase158     derived_type const& dncast () const { return *static_cast<derived_type const*>(this); }
dncastboost::cnv::cnvbase159     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
163     static int const bufsize_ = 512;
164     bool              skipws_;
165     int            precision_;
166     bool           uppercase_;
167     int                width_;
168     int                 fill_;
169     cnv::base           base_;
170     cnv::adjust       adjust_;
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
179