Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_toString.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_TO_STRING_HPP
11 #define TEUCHOS_TO_STRING_HPP
12 
13 #include "Teuchos_ConfigDefs.hpp"
14 #ifdef HAVE_TEUCHOSCORE_QUADMATH
15 # include <quadmath.h> // __float128 functions
16 #endif // HAVE_TEUCHOSCORE_QUADMATH
17 
18 namespace Teuchos {
19 
27 template<typename T>
29 public:
30  static std::string toString( const T &t )
31  {
32  std::ostringstream oss;
33  oss << t;
34  return oss.str();
35  }
36 };
37 
38 
48 template<typename T>
49 inline
50 std::string toString(const T& t)
51 {
53 }
54 
55 
57 template<>
58 class ToStringTraits<bool> {
59 public:
60  static std::string toString( const bool &t )
61  {
62  if (t)
63  return "true";
64  return "false";
65  }
66 };
67 
68 
70 template<>
71 class ToStringTraits<std::string> {
72 public:
73  static std::string toString( const std::string &t )
74  {
75  return t;
76  }
77 };
78 
80 template<>
81 class ToStringTraits<double> {
82 public:
83  static std::string toString (const double& t) {
84  std::ostringstream os;
85  os.setf (std::ios::scientific);
86  // 17 = round(52 * log10(2)) + 1. That's one decimal digit more
87  // than the binary precision justifies, which should be plenty.
88  // Guy Steele et al. have a better algorithm for floating-point
89  // I/O, but using a lot of digits is the lazy approach.
90  os.precision (17);
91  os << t;
92  return os.str();
93  }
94 };
95 
96 #ifdef HAVE_TEUCHOS_LONG_DOUBLE
97 
98 template<>
99 class ToStringTraits<long double> {
100 public:
101  static std::string toString (const long double& t) {
102  std::ostringstream os;
103  os.setf (std::ios::scientific);
104  // 26 = round(80 * log10(2)) + 1. That's one decimal digit more
105  // than the binary precision justifies, which should be plenty.
106  // Guy Steele et al. have a better algorithm for floating-point
107  // I/O, but using a lot of digits is the lazy approach.
108  os.precision (26);
109  os << t;
110  return os.str();
111  }
112 };
113 #endif
114 
116 template<>
117 class ToStringTraits<float> {
118 public:
119  static std::string toString (const float& t) {
120  std::ostringstream os;
121  os.setf (std::ios::scientific);
122  // 8 = round(23 * log10(2)) + 1. That's one decimal digit more
123  // than the binary precision justifies, which should be plenty.
124  // Guy Steele et al. have a better algorithm for floating-point
125  // I/O, but using a lot of digits is the lazy approach.
126  os.precision (8);
127  os << t;
128  return os.str();
129  }
130 };
131 
132 
133 #ifdef HAVE_TEUCHOSCORE_QUADMATH
134 template<>
139 class ToStringTraits<__float128> {
140 public:
141  static std::string toString (const __float128& val)
142  {
143  // libquadmath doesn't implement operator<< (std::ostream&,
144  // __float128), but it does have a print function.
145  const size_t bufSize = 128;
146  char buf[128];
147 
148  // FIXME (mfh 04 Sep 2015) We should test the returned int value
149  // to make sure that it is < bufSize. On the other hand, I do not
150  // want to add more header dependencies to this file, and
151  // TEUCHOS_TEST_FOR_EXCEPTION is not already included.
152 #if 0
153  const int numCharPrinted = quadmath_snprintf (buf, bufSize, "%.30Qe", val);
155  (static_cast<size_t> (numCharPrinted) >= bufSize, std::runtime_error,
156  "Teuchos::toString: Failed to print __float128 value: buffer has "
157  << bufSize << " characters, but quadmath_snprintf wanted "
158  << numCharPrinted << " characters!");
159 #else
160  (void) quadmath_snprintf (buf, bufSize, "%.30Qe", val);
161 #endif
162  return std::string (buf);
163  }
164 };
165 #endif // HAVE_TEUCHOSCORE_QUADMATH
166 
167 
171 template<typename T1, typename T2>
172 class ToStringTraits<std::pair<T1, T2> > {
173 public:
174  static std::string toString (const std::pair<T1, T2>& t) {
175  std::ostringstream oss;
176  oss << "(" << t.first << "," << t.second << ")";
177  return oss.str();
178  }
179 };
180 
181 } // end namespace Teuchos
182 
183 
184 #endif // TEUCHOS_TO_STRING_HPP
185 
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Default traits class for converting objects into strings.