]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/convert/printf.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / convert / printf.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_PRINTF_HPP
6 #define BOOST_CONVERT_PRINTF_HPP
7
8 #include <boost/convert/base.hpp>
9 #include <boost/make_default.hpp>
10 #include <boost/mpl/vector.hpp>
11 #include <boost/mpl/find.hpp>
12 #include <boost/range/as_literal.hpp>
13 #include <string>
14 #include <cstdio>
15
16 namespace boost { namespace cnv
17 {
18 struct printf;
19 }}
20
21 struct boost::cnv::printf : public boost::cnv::cnvbase<boost::cnv::printf>
22 {
23 typedef boost::cnv::printf this_type;
24 typedef boost::cnv::cnvbase<this_type> base_type;
25
26 using base_type::operator();
27
28 template<typename in_type>
29 cnv::range<char*>
30 to_str(in_type value_in, char* buf) const
31 {
32 char const* fmt = pformat(pos<in_type>());
33 int const num_chars = snprintf(buf, bufsize_, fmt, precision_, value_in);
34 bool const success = num_chars < bufsize_;
35
36 return cnv::range<char*>(buf, success ? (buf + num_chars) : buf);
37 }
38 template<typename string_type, typename out_type>
39 void
40 str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
41 {
42 out_type result = boost::make_default<out_type>();
43 int const num_read = sscanf(&*range.begin(), format(pos<out_type>()), &result);
44
45 if (num_read == 1)
46 result_out = result;
47 }
48
49 private:
50
51 template<typename Type> int pos() const
52 {
53 typedef boost::mpl::vector<double, float,
54 int, unsigned int,
55 short int, unsigned short int,
56 long int, unsigned long int
57 > managed_types;
58
59 typedef typename boost::mpl::find<managed_types, Type>::type type_iterator;
60 typedef typename type_iterator::pos type_pos;
61
62 return type_pos::value;
63 }
64
65 char const* pformat(int pos) const
66 {
67 static char const* d_format[] = { "%.*f", "%.*f", "%.*d", "%.*u", "%.*hd", "%.*hu", "%.*ld", "%.*lu" }; // Must match managed_types
68 static char const* x_format[] = { "%.*f", "%.*f", "%.*x", "%.*x", "%.*hx", "%.*hx", "%.*lx", "%.*lx" }; // Must match managed_types
69 static char const* o_format[] = { "%.*f", "%.*f", "%.*o", "%.*o", "%.*ho", "%.*ho", "%.*lo", "%.*lo" }; // Must match managed_types
70 char const* format = base_ == 10 ? d_format[pos]
71 : base_ == 16 ? x_format[pos]
72 : base_ == 8 ? o_format[pos]
73 : (BOOST_ASSERT(0), (char const*) 0);
74 return format;
75 }
76 char const* format(int pos) const
77 {
78 static char const* d_format[] = { "%f", "%f", "%d", "%u", "%hd", "%hu", "%ld", "%lu" }; // Must match managed_types
79 static char const* x_format[] = { "%f", "%f", "%x", "%x", "%hx", "%hx", "%lx", "%lx" }; // Must match managed_types
80 static char const* o_format[] = { "%f", "%f", "%o", "%o", "%ho", "%ho", "%lo", "%lo" }; // Must match managed_types
81 char const* format = base_ == 10 ? d_format[pos]
82 : base_ == 16 ? x_format[pos]
83 : base_ == 8 ? o_format[pos]
84 : (BOOST_ASSERT(0), (char const*) 0);
85 return format;
86 }
87 };
88
89 #endif // BOOST_CONVERT_PRINTF_HPP