Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
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
45
#include "
Teuchos_ConfigDefs.hpp
"
46
#ifdef HAVE_TEUCHOSCORE_QUADMATH
47
# include <quadmath.h>
// __float128 functions
48
#endif
// HAVE_TEUCHOSCORE_QUADMATH
49
50
namespace
Teuchos
{
51
59
template
<
typename
T>
60
class
ToStringTraits
{
61
public
:
62
static
std::string
toString
(
const
T &t )
63
{
64
std::ostringstream oss;
65
oss << t;
66
return
oss.str();
67
}
68
};
69
70
80
template
<
typename
T>
81
inline
82
std::string
toString
(
const
T& t)
83
{
84
return
ToStringTraits<T>::toString
(t);
85
}
86
87
89
template
<>
90
class
ToStringTraits
<bool> {
91
public
:
92
static
std::string
toString
(
const
bool
&t )
93
{
94
if
(t)
95
return
"true"
;
96
return
"false"
;
97
}
98
};
99
100
102
template
<>
103
class
ToStringTraits
<
std
::string> {
104
public
:
105
static
std::string
toString
(
const
std::string &t )
106
{
107
return
t;
108
}
109
};
110
112
template
<>
113
class
ToStringTraits
<double> {
114
public
:
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
130
template
<>
131
class
ToStringTraits<long double> {
132
public
:
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
148
template
<>
149
class
ToStringTraits
<float> {
150
public
:
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
170
template
<>
171
class
ToStringTraits<__float128> {
172
public
:
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);
186
TEUCHOS_TEST_FOR_EXCEPTION
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
203
template
<
typename
T1,
typename
T2>
204
class
ToStringTraits
<
std
::pair<T1, T2> > {
205
public
:
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_ConfigDefs.hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::ToStringTraits< bool >::toString
static std::string toString(const bool &t)
Definition
Teuchos_toString.hpp:92
Teuchos::ToStringTraits< double >::toString
static std::string toString(const double &t)
Definition
Teuchos_toString.hpp:115
Teuchos::ToStringTraits< float >::toString
static std::string toString(const float &t)
Definition
Teuchos_toString.hpp:151
Teuchos::ToStringTraits< std::pair< T1, T2 > >::toString
static std::string toString(const std::pair< T1, T2 > &t)
Definition
Teuchos_toString.hpp:206
Teuchos::ToStringTraits< std::string >::toString
static std::string toString(const std::string &t)
Definition
Teuchos_toString.hpp:105
Teuchos::ToStringTraits
Default traits class for converting objects into strings.
Definition
Teuchos_toString.hpp:60
Teuchos::ToStringTraits::toString
static std::string toString(const T &t)
Definition
Teuchos_toString.hpp:62
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition
Teuchos_TestForException.hpp:178
Teuchos
Definition
Teuchos_AbstractFactory.hpp:47
Teuchos::toString
std::string toString(const HashSet< Key > &h)
Definition
Teuchos_HashSet.hpp:112
std
Definition
DefaultMpiComm_TagTests.cpp:51
Generated by
1.17.0