1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | |
20 | #ifndef _THRIFT_TOSTRING_H_ |
21 | #define _THRIFT_TOSTRING_H_ 1 |
22 | |
23 | #include <cmath> |
24 | #include <limits> |
25 | #include <map> |
26 | #include <set> |
27 | #include <sstream> |
28 | #include <string> |
29 | #include <vector> |
30 | |
31 | namespace apache { |
32 | namespace thrift { |
33 | |
34 | template <typename T> |
35 | std::string to_string(const T& t) { |
36 | std::ostringstream o; |
37 | o << t; |
38 | return o.str(); |
39 | } |
40 | |
41 | // TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11 |
42 | // is enabled. |
43 | inline std::string to_string(const float& t) { |
44 | std::ostringstream o; |
45 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1)))); |
46 | o << t; |
47 | return o.str(); |
48 | } |
49 | |
50 | inline std::string to_string(const double& t) { |
51 | std::ostringstream o; |
52 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1)))); |
53 | o << t; |
54 | return o.str(); |
55 | } |
56 | |
57 | inline std::string to_string(const long double& t) { |
58 | std::ostringstream o; |
59 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1)))); |
60 | o << t; |
61 | return o.str(); |
62 | } |
63 | |
64 | template <typename K, typename V> |
65 | std::string to_string(const std::map<K, V>& m); |
66 | |
67 | template <typename T> |
68 | std::string to_string(const std::set<T>& s); |
69 | |
70 | template <typename T> |
71 | std::string to_string(const std::vector<T>& t); |
72 | |
73 | template <typename K, typename V> |
74 | std::string to_string(const typename std::pair<K, V>& v) { |
75 | std::ostringstream o; |
76 | o << to_string(v.first) << ": " << to_string(v.second); |
77 | return o.str(); |
78 | } |
79 | |
80 | template <typename T> |
81 | std::string to_string(const T& beg, const T& end) { |
82 | std::ostringstream o; |
83 | for (T it = beg; it != end; ++it) { |
84 | if (it != beg) |
85 | o << ", " ; |
86 | o << to_string(*it); |
87 | } |
88 | return o.str(); |
89 | } |
90 | |
91 | template <typename T> |
92 | std::string to_string(const std::vector<T>& t) { |
93 | std::ostringstream o; |
94 | o << "[" << to_string(t.begin(), t.end()) << "]" ; |
95 | return o.str(); |
96 | } |
97 | |
98 | template <typename K, typename V> |
99 | std::string to_string(const std::map<K, V>& m) { |
100 | std::ostringstream o; |
101 | o << "{" << to_string(m.begin(), m.end()) << "}" ; |
102 | return o.str(); |
103 | } |
104 | |
105 | template <typename T> |
106 | std::string to_string(const std::set<T>& s) { |
107 | std::ostringstream o; |
108 | o << "{" << to_string(s.begin(), s.end()) << "}" ; |
109 | return o.str(); |
110 | } |
111 | } |
112 | } // apache::thrift |
113 | |
114 | #endif // _THRIFT_TOSTRING_H_ |
115 | |