blob: fe46032691842cd0b63b072cad169dfe2c22c5b6 [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
Sven Roederer791f85c2024-07-18 02:06:17 +020048// special handling of i8 datatypes (THRIFT-5272)
49inline std::string to_string(const int8_t& t) {
50 std::ostringstream o;
51 o.imbue(default_locale);
52 o << static_cast<int>(t);
53 return o.str();
54}
55
Jim Apple117a5cc2017-03-29 20:39:36 -070056// TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11
57// is enabled.
58inline std::string to_string(const float& 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<float>::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 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<double>::digits * std::log10(2.0f) + 1))));
Jim Apple117a5cc2017-03-29 20:39:36 -070070 o << t;
71 return o.str();
72}
73
74inline std::string to_string(const long double& t) {
75 std::ostringstream o;
Lukas Barth1d6a3262023-05-17 17:27:52 +020076 o.imbue(default_locale);
James E. King, III33df4eb2017-04-06 08:45:17 -040077 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 -070078 o << t;
79 return o.str();
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020080}
81
82template <typename K, typename V>
83std::string to_string(const std::map<K, V>& m);
84
85template <typename T>
86std::string to_string(const std::set<T>& s);
87
88template <typename T>
89std::string to_string(const std::vector<T>& t);
90
91template <typename K, typename V>
92std::string to_string(const typename std::pair<K, V>& v) {
93 std::ostringstream o;
94 o << to_string(v.first) << ": " << to_string(v.second);
95 return o.str();
96}
97
98template <typename T>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010099std::string to_string(const T& beg, const T& end) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200100 std::ostringstream o;
101 for (T it = beg; it != end; ++it) {
102 if (it != beg)
103 o << ", ";
104 o << to_string(*it);
105 }
106 return o.str();
107}
108
109template <typename T>
110std::string to_string(const std::vector<T>& t) {
111 std::ostringstream o;
112 o << "[" << to_string(t.begin(), t.end()) << "]";
113 return o.str();
114}
115
116template <typename K, typename V>
117std::string to_string(const std::map<K, V>& m) {
118 std::ostringstream o;
119 o << "{" << to_string(m.begin(), m.end()) << "}";
120 return o.str();
121}
122
123template <typename T>
124std::string to_string(const std::set<T>& s) {
125 std::ostringstream o;
126 o << "{" << to_string(s.begin(), s.end()) << "}";
127 return o.str();
128}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100129}
130} // apache::thrift
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200131
132#endif // _THRIFT_TOSTRING_H_