There are numerous file formats used in GPS and probably other systems that print floating point numbers that don't conform to standard formats used by C++, including FORTRAN-esque use of 'D' to indicate the exponent, or having a fixed and/or large number of digits in the exponent. This class tries to resolve that by making a double-precision floating point value that carries formatting information along with it.
When using FormattedDouble in a file record wrapper, you would typically define the value stored in the record as a formatted double and initialize it in the record class' constructor with the FormattedDouble(double,StringUtils::FFLead,unsigned,unsigned,unsigned,char,StringUtils::FFSign,StringUtils::FFAlign) constructor, which defines all the relevant formatting information. The value is whatever you'd normally initialize it to outside the context of the formatting. The formatting information at this point is retained for output and input and the value can be treated as any other double. Additionally, you can assign a string to this class and it will interpret it according to the formatting information.
Definition at line 70 of file FormattedDouble.hpp.
#include <FormattedDouble.hpp>
Public Member Functions | |
void | dump (std::ostream &s) const |
debug output all data members More... | |
FormattedDouble (const std::string &str, unsigned width=0, char expChar='e') | |
FormattedDouble (double d, StringUtils::FFLead lead, unsigned mantissa=0, unsigned exponent=2, unsigned width=0, char expChar='e', StringUtils::FFSign sign=StringUtils::FFSign::NegOnly, StringUtils::FFAlign align=StringUtils::FFAlign::Left) | |
FormattedDouble (unsigned width=0, char expChar='e') | |
operator double () const | |
Cast this object to a double for math and such. More... | |
operator double & () | |
Cast this object to a double for math and such. More... | |
operator std::string () | |
Cast this object to a string using formatting configuration. More... | |
FormattedDouble | operator* (double d) const |
FormattedDouble & | operator++ () |
Increment the value (prefix). More... | |
FormattedDouble | operator++ (int) |
Increment the value (postfix). More... | |
FormattedDouble & | operator-- () |
Decrement the value (prefix). More... | |
FormattedDouble | operator-- (int) |
Decrement the value (postfix). More... | |
FormattedDouble | operator/ (double d) const |
FormattedDouble & | operator= (const FormattedDouble &right)=default |
Copy assignment. More... | |
FormattedDouble & | operator= (const std::string &s) |
Assign a value by decoding a string using existing formatting. More... | |
FormattedDouble & | operator= (double d) |
Assign a value without affecting formatting. More... | |
Public Attributes | |
StringUtils::FFAlign | alignment |
Alignment when padding with space. More... | |
char | exponentChar |
What character delimits the exponent. More... | |
unsigned | exponentLen |
How many digits of exponent. More... | |
StringUtils::FFLead | leadChar |
Leading non-space character. More... | |
StringUtils::FFSign | leadSign |
How to handle signs for positive val. More... | |
unsigned | mantissaLen |
How many digits of mantissa. More... | |
unsigned | totalLen |
Total width of space-padded value. More... | |
double | val |
The value as read or to be formatted. More... | |
gnsstk::FormattedDouble::FormattedDouble | ( | double | d, |
StringUtils::FFLead | lead, | ||
unsigned | mantissa = 0 , |
||
unsigned | exponent = 2 , |
||
unsigned | width = 0 , |
||
char | expChar = 'e' , |
||
StringUtils::FFSign | sign = StringUtils::FFSign::NegOnly , |
||
StringUtils::FFAlign | align = StringUtils::FFAlign::Left |
||
) |
Prepare a value for stream output.
[in] | d | The value that is to be formatted in an ostream. |
[in] | lead | How the lead-in to the value is to be formatted. |
[in] | mantissa | How many digits of precision should be in the mantissa, e.g. mantissa=5 could result in something like 1.2345e+00. |
[in] | exponent | How many digits of precision should be in the exponent, e.g. exponent=3 could result in something like 1.2345e+000. Exponents will always be at least 2 characters in length. |
[in] | width | The total number of characters in the formatted value. If the length of the formatted value including mantissa, exponent, sign, etc. is >= width, no additional formatting will take place. If the length of the formatted value is < width, it will be padded with spaces according to align. |
[in] | expChar | The character used to designate the exponent, e.g. "e" or "E" or "D". |
[in] | sign | How numerical sign is to be handled in formatting. |
[in] | align | How to pad the formatted value according to width. Left adds space to the end of the formatted value while Right inserts space at the beginning. |
Definition at line 44 of file FormattedDouble.cpp.
|
explicit |
Prepare for stream input.
[in] | width | The total number of characters in the formatted value. If the length of the formatted value including mantissa, exponent, sign, etc. is >= width, no additional formatting will take place. If the length of the formatted value is < width, it will be padded with spaces according to align. |
[in] | expChar | The character used to designate the exponent, e.g. "e" or "E" or "D". |
Definition at line 55 of file FormattedDouble.cpp.
|
explicit |
Decode a string containing a double-precision floating point number with the given formatting options.
[in] | str | The string to decode. |
[in] | width | The total number of characters in the formatted value. If the length of the formatted value including mantissa, exponent, sign, etc. is >= width, no additional formatting will take place. If the length of the formatted value is < width, it will be padded with spaces according to align. |
[in] | expChar | The character used to designate the exponent, e.g. "e" or "E" or "D". |
Definition at line 65 of file FormattedDouble.cpp.
void gnsstk::FormattedDouble::dump | ( | std::ostream & | s | ) | const |
debug output all data members
Definition at line 143 of file FormattedDouble.cpp.
|
inline |
Cast this object to a double for math and such.
Definition at line 137 of file FormattedDouble.hpp.
|
inline |
Cast this object to a double for math and such.
Definition at line 141 of file FormattedDouble.hpp.
|
inline |
Cast this object to a string using formatting configuration.
Definition at line 208 of file FormattedDouble.hpp.
|
inline |
Multiply val by a scalar. This ensures that you can scale FormattedDouble objects and retain the formatting.
Definition at line 222 of file FormattedDouble.hpp.
|
inline |
Increment the value (prefix).
Definition at line 156 of file FormattedDouble.hpp.
|
inline |
Increment the value (postfix).
Definition at line 159 of file FormattedDouble.hpp.
|
inline |
Decrement the value (prefix).
Definition at line 162 of file FormattedDouble.hpp.
|
inline |
Decrement the value (postfix).
Definition at line 165 of file FormattedDouble.hpp.
|
inline |
Divide val by a scalar. This ensures that you can scale FormattedDouble objects and retain the formatting.
Definition at line 215 of file FormattedDouble.hpp.
|
default |
Copy assignment.
FormattedDouble & gnsstk::FormattedDouble::operator= | ( | const std::string & | s | ) |
Assign a value by decoding a string using existing formatting.
Definition at line 76 of file FormattedDouble.cpp.
|
inline |
Assign a value without affecting formatting.
Definition at line 172 of file FormattedDouble.hpp.
StringUtils::FFAlign gnsstk::FormattedDouble::alignment |
Alignment when padding with space.
Definition at line 188 of file FormattedDouble.hpp.
char gnsstk::FormattedDouble::exponentChar |
What character delimits the exponent.
Definition at line 186 of file FormattedDouble.hpp.
unsigned gnsstk::FormattedDouble::exponentLen |
How many digits of exponent.
Definition at line 184 of file FormattedDouble.hpp.
StringUtils::FFLead gnsstk::FormattedDouble::leadChar |
Leading non-space character.
Definition at line 182 of file FormattedDouble.hpp.
StringUtils::FFSign gnsstk::FormattedDouble::leadSign |
How to handle signs for positive val.
Definition at line 187 of file FormattedDouble.hpp.
unsigned gnsstk::FormattedDouble::mantissaLen |
How many digits of mantissa.
Definition at line 183 of file FormattedDouble.hpp.
unsigned gnsstk::FormattedDouble::totalLen |
Total width of space-padded value.
Definition at line 185 of file FormattedDouble.hpp.
double gnsstk::FormattedDouble::val |
The value as read or to be formatted.
Definition at line 181 of file FormattedDouble.hpp.