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 //
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>
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 {
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 
129 template<>
130 class ToStringTraits<float> {
131 public:
132  static std::string toString (const float& t) {
133  std::ostringstream os;
134  os.setf (std::ios::scientific);
135  // 8 = round(23 * log10(2)) + 1. That's one decimal digit more
136  // than the binary precision justifies, which should be plenty.
137  // Guy Steele et al. have a better algorithm for floating-point
138  // I/O, but using a lot of digits is the lazy approach.
139  os.precision (8);
140  os << t;
141  return os.str();
142  }
143 };
144 
145 
146 #ifdef HAVE_TEUCHOSCORE_QUADMATH
147 template<>
152 class ToStringTraits<__float128> {
153 public:
154  static std::string toString (const __float128& val)
155  {
156  // libquadmath doesn't implement operator<< (std::ostream&,
157  // __float128), but it does have a print function.
158  const size_t bufSize = 128;
159  char buf[128];
160 
161  // FIXME (mfh 04 Sep 2015) We should test the returned int value
162  // to make sure that it is < bufSize. On the other hand, I do not
163  // want to add more header dependencies to this file, and
164  // TEUCHOS_TEST_FOR_EXCEPTION is not already included.
165 #if 0
166  const int numCharPrinted = quadmath_snprintf (buf, bufSize, "%.30Qe", val);
168  (static_cast<size_t> (numCharPrinted) >= bufSize, std::runtime_error,
169  "Teuchos::toString: Failed to print __float128 value: buffer has "
170  << bufSize << " characters, but quadmath_snprintf wanted "
171  << numCharPrinted << " characters!");
172 #else
173  (void) quadmath_snprintf (buf, bufSize, "%.30Qe", val);
174 #endif
175  return std::string (buf);
176  }
177 };
178 #endif // HAVE_TEUCHOSCORE_QUADMATH
179 
180 
184 template<typename T1, typename T2>
185 class ToStringTraits<std::pair<T1, T2> > {
186 public:
187  static std::string toString (const std::pair<T1, T2>& t) {
188  std::ostringstream oss;
189  oss << "(" << t.first << "," << t.second << ")";
190  return oss.str();
191  }
192 };
193 
194 } // end namespace Teuchos
195 
196 
197 #endif // TEUCHOS_TO_STRING_HPP
#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.