CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
to_string.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_TO_STRING_HPP
2 # define CPPAD_UTILITY_TO_STRING_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
5 
6 CppAD is distributed under multiple licenses. This distribution is under
7 the terms of the
8  Eclipse Public License Version 1.0.
9 
10 A copy of this license is included in the COPYING file of this distribution.
11 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12 -------------------------------------------------------------------------- */
13 
14 /*
15 $begin to_string$$
16 $spell
17  cppad.hpp
18  long long
19  std
20  const
21  ostringstream
22 $$
23 
24 $section Convert Certain Types to a String$$
25 
26 $head Syntax$$
27 $codei%# include <cppad/utility/to_string.hpp>
28 %$$
29 $icode%s% = to_string(%value%)%$$.
30 
31 $head See Also$$
32 $cref base_to_string$$, $cref ad_to_string$$
33 
34 $head Purpose$$
35 This routine is similar to the C++11 routine $code std::to_string$$
36 with the following differences:
37 $list number$$
38 It works with C++98.
39 $lnext
40 It has been extended to the fundamental floating point types.
41 $lnext
42 It has specifications for extending to an arbitrary type; see
43 $cref base_to_string$$.
44 $lnext
45 If $code <cppad/cppad.hpp>$$ is included,
46 and it has been extended to a $icode Base$$ type,
47 it automatically extends to the
48 $cref/AD types above Base/glossary/AD Type Above Base/$$.
49 $lnext
50 For integer types, conversion to a string is exact.
51 For floating point types, conversion to a string yields a value
52 that has relative error within machine epsilon.
53 $lend
54 
55 $head value$$
56 
57 $subhead Integer$$
58 The argument $icode value$$ can have the following prototype
59 $codei%
60  const %Integer%& %value%
61 %$$
62 where $icode Integer$$ is any of the fundamental integer types; e.g.,
63 $code short int$$ and $code unsigned long$$.
64 Note that if C++11 is supported by this compilation,
65 $code unsigned long long$$ is also a fundamental integer type.
66 
67 $subhead Float$$
68 The argument $icode value$$ can have the following prototype
69 $codei%
70  const %Float%& %value%
71 %$$
72 where $icode Float$$ is any of the fundamental floating point types; i.e.,
73 $code float$$, $code double$$, and $code long double$$.
74 
75 $head s$$
76 The return value has prototype
77 $codei%
78  std::string %s%
79 %$$
80 and contains a representation of the specified $icode value$$.
81 
82 $subhead Integer$$
83 If $icode value$$ is an $codei Integer$$,
84 the representation is equivalent to $codei%os% << %value%$$
85 where $icode os$$ is an $code std::ostringstream$$.
86 
87 $subhead Float$$
88 If $icode value$$ is a $codei Float$$,
89 enough digits are used in the representation so that
90 the result is accurate to withing round off error.
91 
92 $children%
93  example/utility/to_string.cpp
94 %$$
95 $head Example$$
96 The file $cref to_string.cpp$$
97 contains an example and test of this routine.
98 It returns true if it succeeds and false otherwise.
99 
100 $end
101 */
102 # include <limits>
103 # include <cmath>
104 # include <iomanip>
105 # include <sstream>
106 # include <cppad/core/cppad_assert.hpp>
107 
108 # define CPPAD_SPECIALIZE_TO_STRING_INTEGER(Type) \
109 template <> struct to_string_struct<Type>\
110 { std::string operator()(const Type& value) \
111  { std::stringstream os;\
112  os << value;\
113  return os.str();\
114  }\
115 };
116 
117 # define CPPAD_SPECIALIZE_TO_STRING_FLOAT(Float) \
118 template <> struct to_string_struct<Float>\
119 { std::string operator()(const Float& value) \
120  { std::stringstream os;\
121  int n_digits = 1 + std::numeric_limits<Float>::digits10;\
122  os << std::setprecision(n_digits);\
123  os << value;\
124  return os.str();\
125  }\
126 };
127 
128 namespace CppAD {
129 
130  // Default implementation,
131  // each type must define its own specilization.
132  template <class Type>
134  { std::string operator()(const Type& value)
136  false,
137  "to_string is not implemented for this type"
138  );
139  // return empty string
140  return std::string("");
141  }
142  };
143 
144  // specialization for the fundamental integer types
147  //
150  //
153  //
154 # if CPPAD_USE_CPLUSPLUS_2011
157 # endif
158 
159  // specialization for the fundamental floating point types
163 
164  // link from function to function object in structure
165  template<class Type>
166  std::string to_string(const Type& value)
167  { to_string_struct<Type> to_str;
168  return to_str(value);
169  }
170 }
171 
172 # undef CPPAD_SPECIALIZE_TO_STRING_FLOAT
173 # undef CPPAD_SPECIALIZE_TO_STRING_INTEGER
174 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
#define CPPAD_SPECIALIZE_TO_STRING_INTEGER(Type)
Definition: to_string.hpp:108
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
#define CPPAD_SPECIALIZE_TO_STRING_FLOAT(Float)
Definition: to_string.hpp:117
std::string to_string(const Type &value)
Definition: to_string.hpp:166
std::string operator()(const Type &value)
Definition: to_string.hpp:134