File indexing completed on 2026-04-17 08:35:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef _THRIFT_TOSTRING_H_
0021 #define _THRIFT_TOSTRING_H_ 1
0022
0023 #include <cmath>
0024 #include <limits>
0025 #include <locale>
0026 #include <map>
0027 #include <set>
0028 #include <sstream>
0029 #include <string>
0030 #include <vector>
0031
0032 namespace apache {
0033 namespace thrift {
0034
0035
0036 namespace {
0037 const auto default_locale = std::locale("C");
0038 }
0039
0040 template <typename T>
0041 std::string to_string(const T& t) {
0042 std::ostringstream o;
0043 o.imbue(default_locale);
0044 o << t;
0045 return o.str();
0046 }
0047
0048
0049 inline std::string to_string(const int8_t& t) {
0050 std::ostringstream o;
0051 o.imbue(default_locale);
0052 o << static_cast<int>(t);
0053 return o.str();
0054 }
0055
0056
0057
0058 inline std::string to_string(const float& t) {
0059 std::ostringstream o;
0060 o.imbue(default_locale);
0061 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1))));
0062 o << t;
0063 return o.str();
0064 }
0065
0066 inline std::string to_string(const double& t) {
0067 std::ostringstream o;
0068 o.imbue(default_locale);
0069 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1))));
0070 o << t;
0071 return o.str();
0072 }
0073
0074 inline std::string to_string(const long double& t) {
0075 std::ostringstream o;
0076 o.imbue(default_locale);
0077 o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1))));
0078 o << t;
0079 return o.str();
0080 }
0081
0082 template <typename K, typename V>
0083 std::string to_string(const std::map<K, V>& m);
0084
0085 template <typename T>
0086 std::string to_string(const std::set<T>& s);
0087
0088 template <typename T>
0089 std::string to_string(const std::vector<T>& t);
0090
0091 template <typename K, typename V>
0092 std::string to_string(const typename std::pair<K, V>& v) {
0093 std::ostringstream o;
0094 o << to_string(v.first) << ": " << to_string(v.second);
0095 return o.str();
0096 }
0097
0098 template <typename T>
0099 std::string to_string(const T& beg, const T& end) {
0100 std::ostringstream o;
0101 for (T it = beg; it != end; ++it) {
0102 if (it != beg)
0103 o << ", ";
0104 o << to_string(*it);
0105 }
0106 return o.str();
0107 }
0108
0109 template <typename T>
0110 std::string to_string(const std::vector<T>& t) {
0111 std::ostringstream o;
0112 o << "[" << to_string(t.begin(), t.end()) << "]";
0113 return o.str();
0114 }
0115
0116 template <typename K, typename V>
0117 std::string to_string(const std::map<K, V>& m) {
0118 std::ostringstream o;
0119 o << "{" << to_string(m.begin(), m.end()) << "}";
0120 return o.str();
0121 }
0122
0123 template <typename T>
0124 std::string to_string(const std::set<T>& s) {
0125 std::ostringstream o;
0126 o << "{" << to_string(s.begin(), s.end()) << "}";
0127 return o.str();
0128 }
0129 }
0130 }
0131
0132 #endif