Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Teuchos_toString.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#ifndef TEUCHOS_TO_STRING_HPP
43#define TEUCHOS_TO_STRING_HPP
44
46#ifdef HAVE_TEUCHOSCORE_QUADMATH
47# include <quadmath.h> // __float128 functions
48#endif // HAVE_TEUCHOSCORE_QUADMATH
49
50namespace Teuchos {
51
59template<typename T>
61public:
62 static std::string toString( const T &t )
63 {
64 std::ostringstream oss;
65 oss << t;
66 return oss.str();
67 }
68};
69
70
80template<typename T>
81inline
82std::string toString(const T& t)
83{
85}
86
87
89template<>
91public:
92 static std::string toString( const bool &t )
93 {
94 if (t)
95 return "true";
96 return "false";
97 }
98};
99
100
102template<>
103class ToStringTraits<std::string> {
104public:
105 static std::string toString( const std::string &t )
106 {
107 return t;
108 }
109};
110
112template<>
114public:
115 static std::string toString (const double& t) {
116 std::ostringstream os;
117 os.setf (std::ios::scientific);
118 // 17 = round(52 * log10(2)) + 1. That's one decimal digit more
119 // than the binary precision justifies, which should be plenty.
120 // Guy Steele et al. have a better algorithm for floating-point
121 // I/O, but using a lot of digits is the lazy approach.
122 os.precision (17);
123 os << t;
124 return os.str();
125 }
126};
127
128#ifdef HAVE_TEUCHOS_LONG_DOUBLE
130template<>
131class ToStringTraits<long double> {
132public:
133 static std::string toString (const long double& t) {
134 std::ostringstream os;
135 os.setf (std::ios::scientific);
136 // 26 = round(80 * log10(2)) + 1. That's one decimal digit more
137 // than the binary precision justifies, which should be plenty.
138 // Guy Steele et al. have a better algorithm for floating-point
139 // I/O, but using a lot of digits is the lazy approach.
140 os.precision (26);
141 os << t;
142 return os.str();
143 }
144};
145#endif
146
148template<>
150public:
151 static std::string toString (const float& t) {
152 std::ostringstream os;
153 os.setf (std::ios::scientific);
154 // 8 = round(23 * log10(2)) + 1. That's one decimal digit more
155 // than the binary precision justifies, which should be plenty.
156 // Guy Steele et al. have a better algorithm for floating-point
157 // I/O, but using a lot of digits is the lazy approach.
158 os.precision (8);
159 os << t;
160 return os.str();
161 }
162};
163
164
165#ifdef HAVE_TEUCHOSCORE_QUADMATH
170template<>
171class ToStringTraits<__float128> {
172public:
173 static std::string toString (const __float128& val)
174 {
175 // libquadmath doesn't implement operator<< (std::ostream&,
176 // __float128), but it does have a print function.
177 const size_t bufSize = 128;
178 char buf[128];
179
180 // FIXME (mfh 04 Sep 2015) We should test the returned int value
181 // to make sure that it is < bufSize. On the other hand, I do not
182 // want to add more header dependencies to this file, and
183 // TEUCHOS_TEST_FOR_EXCEPTION is not already included.
184#if 0
185 const int numCharPrinted = quadmath_snprintf (buf, bufSize, "%.30Qe", val);
187 (static_cast<size_t> (numCharPrinted) >= bufSize, std::runtime_error,
188 "Teuchos::toString: Failed to print __float128 value: buffer has "
189 << bufSize << " characters, but quadmath_snprintf wanted "
190 << numCharPrinted << " characters!");
191#else
192 (void) quadmath_snprintf (buf, bufSize, "%.30Qe", val);
193#endif
194 return std::string (buf);
195 }
196};
197#endif // HAVE_TEUCHOSCORE_QUADMATH
198
199
203template<typename T1, typename T2>
204class ToStringTraits<std::pair<T1, T2> > {
205public:
206 static std::string toString (const std::pair<T1, T2>& t) {
207 std::ostringstream oss;
208 oss << "(" << t.first << "," << t.second << ")";
209 return oss.str();
210 }
211};
212
213} // end namespace Teuchos
214
215
216#endif // TEUCHOS_TO_STRING_HPP
217
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Concrete serial communicator subclass.
static std::string toString(const bool &t)
static std::string toString(const double &t)
static std::string toString(const float &t)
static std::string toString(const std::pair< T1, T2 > &t)
static std::string toString(const std::string &t)
Default traits class for converting objects into strings.
static std::string toString(const T &t)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
std::string toString(const HashSet< Key > &h)