blob: d91c58c4b2ea02e8a155f4e239e6e8c582e78497 [file] [log] [blame]
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +02001/*
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
Jim Apple117a5cc2017-03-29 20:39:36 -070023#include <cmath>
24#include <limits>
ubuntu323f0322021-05-31 19:08:05 +053025#include <locale>
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020026#include <map>
27#include <set>
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020028#include <sstream>
Jim Apple117a5cc2017-03-29 20:39:36 -070029#include <string>
30#include <vector>
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020031
Konrad Grochowski16a23a62014-11-13 15:33:38 +010032namespace apache {
33namespace thrift {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020034
Lukas Barth1d6a3262023-05-17 17:27:52 +020035// unnamed namespace to enforce internal linkage - could be done with 'inline' when once have C++17
36namespace {
37const auto default_locale = std::locale("C");
38}
39
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020040template <typename T>
41std::string to_string(const T& t) {
Jim Apple117a5cc2017-03-29 20:39:36 -070042 std::ostringstream o;
Lukas Barth1d6a3262023-05-17 17:27:52 +020043 o.imbue(default_locale);
Jim Apple117a5cc2017-03-29 20:39:36 -070044 o << t;
45 return o.str();
46}
47
48// TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11
49// is enabled.
50inline std::string to_string(const float& t) {
51 std::ostringstream o;
Lukas Barth1d6a3262023-05-17 17:27:52 +020052 o.imbue(default_locale);
James E. King, III33df4eb2017-04-06 08:45:17 -040053 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1))));
Jim Apple117a5cc2017-03-29 20:39:36 -070054 o << t;
55 return o.str();
56}
57
58inline std::string to_string(const double& t) {
59 std::ostringstream o;
Lukas Barth1d6a3262023-05-17 17:27:52 +020060 o.imbue(default_locale);
James E. King, III33df4eb2017-04-06 08:45:17 -040061 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1))));
Jim Apple117a5cc2017-03-29 20:39:36 -070062 o << t;
63 return o.str();
64}
65
66inline std::string to_string(const long double& t) {
67 std::ostringstream o;
Lukas Barth1d6a3262023-05-17 17:27:52 +020068 o.imbue(default_locale);
James E. King, III33df4eb2017-04-06 08:45:17 -040069 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1))));
Jim Apple117a5cc2017-03-29 20:39:36 -070070 o << t;
71 return o.str();
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020072}
73
74template <typename K, typename V>
75std::string to_string(const std::map<K, V>& m);
76
77template <typename T>
78std::string to_string(const std::set<T>& s);
79
80template <typename T>
81std::string to_string(const std::vector<T>& t);
82
83template <typename K, typename V>
84std::string to_string(const typename std::pair<K, V>& v) {
85 std::ostringstream o;
86 o << to_string(v.first) << ": " << to_string(v.second);
87 return o.str();
88}
89
90template <typename T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091std::string to_string(const T& beg, const T& end) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020092 std::ostringstream o;
93 for (T it = beg; it != end; ++it) {
94 if (it != beg)
95 o << ", ";
96 o << to_string(*it);
97 }
98 return o.str();
99}
100
101template <typename T>
102std::string to_string(const std::vector<T>& t) {
103 std::ostringstream o;
104 o << "[" << to_string(t.begin(), t.end()) << "]";
105 return o.str();
106}
107
108template <typename K, typename V>
109std::string to_string(const std::map<K, V>& m) {
110 std::ostringstream o;
111 o << "{" << to_string(m.begin(), m.end()) << "}";
112 return o.str();
113}
114
115template <typename T>
116std::string to_string(const std::set<T>& s) {
117 std::ostringstream o;
118 o << "{" << to_string(s.begin(), s.end()) << "}";
119 return o.str();
120}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121}
122} // apache::thrift
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200123
124#endif // _THRIFT_TOSTRING_H_