Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
14#ifdef HAVE_TEUCHOSCORE_QUADMATH
15# include <quadmath.h> // __float128 functions
16#endif // HAVE_TEUCHOSCORE_QUADMATH
17
18namespace Teuchos {
19
27template<typename T>
29public:
30 static std::string toString( const T &t )
31 {
32 std::ostringstream oss;
33 oss << t;
34 return oss.str();
35 }
36};
37
38
48template<typename T>
49inline
50std::string toString(const T& t)
51{
53}
54
55
57template<>
59public:
60 static std::string toString( const bool &t )
61 {
62 if (t)
63 return "true";
64 return "false";
65 }
66};
67
68
70template<>
71class ToStringTraits<std::string> {
72public:
73 static std::string toString( const std::string &t )
74 {
75 return t;
76 }
77};
78
80template<>
82public:
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
98template<>
100public:
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
116template<>
118public:
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
138template<>
140public:
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
171template<typename T1, typename T2>
172class ToStringTraits<std::pair<T1, T2> > {
173public:
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
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Smart reference counting pointer class for automatic garbage collection.
Default traits class for converting objects into strings.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...