4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Format public header */
26 #ifndef HH_SENF_senf_Utils_Format_
27 #define HH_SENF_senf_Utils_Format_ 1
32 #include <boost/utility/enable_if.hpp>
33 #include <boost/type_traits/is_signed.hpp>
34 #include <boost/type_traits/is_unsigned.hpp>
36 //#include "Format.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
42 /** \defgroup senf_utils_format Formating
47 /** \brief Format value in engineering representation
49 The engineering representation is an exponential representation. Exponents however are
50 always multiples of 3:
53 123.45e2 -> 12.345e+03
56 Additionally, an optional delta value may be displayed:
61 senf::format::eng supports several formating options:
63 If the width is set >0, the output will be padded internally. If the width is set to
64 more than the minimal required output width including internal padding, the output is
65 padded on the left or right depending on the streams \c ajustfield setting (changed
66 with \c std::left, \c std:;right or \c std::interal). If the \c adjustfield is set to \c
67 internal, padding is added between the sign and the number.
69 \par \c std::\c setprecision
70 The default stream precision is 6. This will schow values with 6 significant digits. The
71 count includes the number of digits in front of the decimal point.
73 \par \c std::\c showbase, \c std::\c noshowbase
74 If the \c showbase flag is set, Instead of writing out the scale exponent in numeric
75 form, output the corresponding SI prefix.
77 \par \c std::\c showpos, \c std::\c noshowpos
78 If the \c showpos flag is set, positive values will have a '+' sign.
80 \par \c std::\c showpoint, \c std::\c noshowpoint
81 If the \c showpoint flag is set, the exponent will be output even if it is 0. Otherwise,
82 if \c width is set, the exponent will be replaced with 4 blanks.
84 \par \c std::\c uppercase, \c std::\c nouppercase
85 If the \c uppercase flag is set, the exponent letter will be an uppercase 'E' instead of
86 'e'. SI prefixes are \e not uppercased, since some SI prefixes differ only in case.
88 \par \c std::\c setfill
89 The fill character is honored for the outside padding but \e not for the internal
92 \par \c std::\c left, \c std::\c internal, \c std::\c right
93 The alignment flags specify the external padding but do not affect the internal
96 All these flags may optionally be set by calling members of the senf::format::eng() return
97 value with the same name.
101 os << senf::format::eng(1.23);
104 os << std::setw(1) << senf::format::eng(1.23);
107 os << std::setw(25) << std::setprecision(5) << std::showpos << std::uppercase
108 << std::internal << senf::format::eng(12345,67);
109 -> "+ 12.35+-000.07E+03"
111 os << std::showbase << senf::format::eng(12345,67);
114 senf::str(senf::format::eng(12.345,67).setw().setprecision(5).showpoint().uppercase())
115 -> " 12.35+-67.00E+00"
119 \param[in] d optional delta
121 \ingroup senf_utils_format
123 streamable_type eng(float v, float d=NAN);
130 eng(float v, float d = std::numeric_limits<float>::quiet_NaN());
132 eng const & setw(unsigned w = 1) const;
133 eng const & setprecision(unsigned p) const;
134 eng const & setfill(char c) const;
136 eng const & showbase() const;
137 eng const & noshowbase() const;
138 eng const & showpos() const;
139 eng const & noshowpos() const;
140 eng const & showpoint() const;
141 eng const & noshowpoint() const;
142 eng const & uppercase() const;
143 eng const & nouppercase() const;
144 eng const & left() const;
145 eng const & internal() const;
146 eng const & right() const;
152 mutable bool haveWidth_;
153 mutable unsigned width_;
154 mutable bool havePrecision_;
155 mutable unsigned precision_;
156 mutable bool haveFill_;
158 mutable std::ios_base::fmtflags mask_;
159 mutable std::ios_base::fmtflags flags_;
161 friend std::ostream & operator<<(std::ostream & os, eng const & v);
165 std::ostream & operator<<(std::ostream & os, eng const & v);
171 /** \brief Dump integer value with internal representation
173 senf::format::dumpint() will output a signed or unsigned numeric argument in the following
175 \li hexadecimal notation
177 \li byte values interpreted as ASCII characters (in network byte order)
179 All fields will be padded depending on the size of the type to a byte boundary (e.g. a 32bit
180 integer type will be padded to 8 hex-digits, 10 decimal digits and 4 ASCII characters)
182 \ingroup senf_utils_format
185 streamable_type dumpint(T const & v);
190 std::string dumpint(T const & v,
191 typename boost::enable_if<boost::is_signed<T> >::type * = 0);
194 std::string dumpint(T const & v,
195 typename boost::enable_if<boost::is_unsigned<T> >::type * = 0);
198 std::string dumpint(T const & v,
199 typename boost::enable_if<boost::is_signed<typename T::value_type> >::type * = 0);
202 std::string dumpint(T const & v,
203 typename boost::enable_if<boost::is_unsigned<typename T::value_type> >::type * = 0);
209 static unsigned int static_level;
211 unsigned int instance_level;
213 IndentHelper () : instance_level(1) { ++static_level; }
214 ~IndentHelper () { static_level -= instance_level; }
215 void increase() { ++static_level; ++instance_level; }
217 friend std::ostream & operator<<(std::ostream & os, IndentHelper const & indent);
219 std::ostream & operator<<(std::ostream & os, IndentHelper const & indent);
223 ///////////////////////////////hh.e////////////////////////////////////////
224 #include "Format.cci"
225 //#include "Format.ct"
226 #include "Format.cti"
233 // comment-column: 40
234 // c-file-style: "senf"
235 // indent-tabs-mode: nil
236 // ispell-local-dictionary: "american"
237 // compile-command: "scons -u test"