Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:04

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
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 // unnamed namespace to enforce internal linkage - could be done with 'inline' when once have C++17
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 // special handling of i8 datatypes (THRIFT-5272)
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 // TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11
0057 // is enabled.
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 } // apache::thrift
0131 
0132 #endif // _THRIFT_TOSTRING_H_