TToString.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef _THRIFT_TOSTRING_H_
  20. #define _THRIFT_TOSTRING_H_ 1
  21. #include <cmath>
  22. #include <limits>
  23. #include <map>
  24. #include <set>
  25. #include <sstream>
  26. #include <string>
  27. #include <vector>
  28. namespace apache {
  29. namespace thrift {
  30. template <typename T>
  31. std::string to_string(const T& t) {
  32. std::ostringstream o;
  33. o << t;
  34. return o.str();
  35. }
  36. // TODO: replace the computations below with std::numeric_limits::max_digits10 once C++11
  37. // is enabled.
  38. inline std::string to_string(const float& t) {
  39. std::ostringstream o;
  40. o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1))));
  41. o << t;
  42. return o.str();
  43. }
  44. inline std::string to_string(const double& t) {
  45. std::ostringstream o;
  46. o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1))));
  47. o << t;
  48. return o.str();
  49. }
  50. inline std::string to_string(const long double& t) {
  51. std::ostringstream o;
  52. o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1))));
  53. o << t;
  54. return o.str();
  55. }
  56. template <typename K, typename V>
  57. std::string to_string(const std::map<K, V>& m);
  58. template <typename T>
  59. std::string to_string(const std::set<T>& s);
  60. template <typename T>
  61. std::string to_string(const std::vector<T>& t);
  62. template <typename K, typename V>
  63. std::string to_string(const typename std::pair<K, V>& v) {
  64. std::ostringstream o;
  65. o << to_string(v.first) << ": " << to_string(v.second);
  66. return o.str();
  67. }
  68. template <typename T>
  69. std::string to_string(const T& beg, const T& end) {
  70. std::ostringstream o;
  71. for (T it = beg; it != end; ++it) {
  72. if (it != beg)
  73. o << ", ";
  74. o << to_string(*it);
  75. }
  76. return o.str();
  77. }
  78. template <typename T>
  79. std::string to_string(const std::vector<T>& t) {
  80. std::ostringstream o;
  81. o << "[" << to_string(t.begin(), t.end()) << "]";
  82. return o.str();
  83. }
  84. template <typename K, typename V>
  85. std::string to_string(const std::map<K, V>& m) {
  86. std::ostringstream o;
  87. o << "{" << to_string(m.begin(), m.end()) << "}";
  88. return o.str();
  89. }
  90. template <typename T>
  91. std::string to_string(const std::set<T>& s) {
  92. std::ostringstream o;
  93. o << "{" << to_string(s.begin(), s.end()) << "}";
  94. return o.str();
  95. }
  96. }
  97. } // apache::thrift
  98. #endif // _THRIFT_TOSTRING_H_