Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 23 | #include <cmath> |
| 24 | #include <limits> |
ubuntu | 323f032 | 2021-05-31 19:08:05 +0530 | [diff] [blame] | 25 | #include <locale> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 26 | #include <map> |
| 27 | #include <set> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 28 | #include <sstream> |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 31 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 32 | namespace apache { |
| 33 | namespace thrift { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 34 | |
Lukas Barth | 1d6a326 | 2023-05-17 17:27:52 +0200 | [diff] [blame] | 35 | // unnamed namespace to enforce internal linkage - could be done with 'inline' when once have C++17 |
| 36 | namespace { |
| 37 | const auto default_locale = std::locale("C"); |
| 38 | } |
| 39 | |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 40 | template <typename T> |
| 41 | std::string to_string(const T& t) { |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 42 | std::ostringstream o; |
Lukas Barth | 1d6a326 | 2023-05-17 17:27:52 +0200 | [diff] [blame] | 43 | o.imbue(default_locale); |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 44 | o << t; |
| 45 | return o.str(); |
| 46 | } |
| 47 | |
Sven Roederer | 791f85c | 2024-07-18 02:06:17 +0200 | [diff] [blame] | 48 | // special handling of i8 datatypes (THRIFT-5272) |
| 49 | inline 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 Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 56 | // TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11 |
| 57 | // is enabled. |
| 58 | inline std::string to_string(const float& t) { |
| 59 | std::ostringstream o; |
Lukas Barth | 1d6a326 | 2023-05-17 17:27:52 +0200 | [diff] [blame] | 60 | o.imbue(default_locale); |
James E. King, III | 33df4eb | 2017-04-06 08:45:17 -0400 | [diff] [blame] | 61 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1)))); |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 62 | o << t; |
| 63 | return o.str(); |
| 64 | } |
| 65 | |
| 66 | inline std::string to_string(const double& t) { |
| 67 | std::ostringstream o; |
Lukas Barth | 1d6a326 | 2023-05-17 17:27:52 +0200 | [diff] [blame] | 68 | o.imbue(default_locale); |
James E. King, III | 33df4eb | 2017-04-06 08:45:17 -0400 | [diff] [blame] | 69 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1)))); |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 70 | o << t; |
| 71 | return o.str(); |
| 72 | } |
| 73 | |
| 74 | inline std::string to_string(const long double& t) { |
| 75 | std::ostringstream o; |
Lukas Barth | 1d6a326 | 2023-05-17 17:27:52 +0200 | [diff] [blame] | 76 | o.imbue(default_locale); |
James E. King, III | 33df4eb | 2017-04-06 08:45:17 -0400 | [diff] [blame] | 77 | o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1)))); |
Jim Apple | 117a5cc | 2017-03-29 20:39:36 -0700 | [diff] [blame] | 78 | o << t; |
| 79 | return o.str(); |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | template <typename K, typename V> |
| 83 | std::string to_string(const std::map<K, V>& m); |
| 84 | |
| 85 | template <typename T> |
| 86 | std::string to_string(const std::set<T>& s); |
| 87 | |
| 88 | template <typename T> |
| 89 | std::string to_string(const std::vector<T>& t); |
| 90 | |
| 91 | template <typename K, typename V> |
| 92 | std::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 | |
| 98 | template <typename T> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | std::string to_string(const T& beg, const T& end) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 100 | 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 | |
| 109 | template <typename T> |
| 110 | std::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 | |
| 116 | template <typename K, typename V> |
| 117 | std::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 | |
| 123 | template <typename T> |
| 124 | std::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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 129 | } |
| 130 | } // apache::thrift |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 131 | |
| 132 | #endif // _THRIFT_TOSTRING_H_ |