mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Added float/double precision setters
This commit is contained in:
@@ -43,10 +43,13 @@ namespace YAML
|
||||
bool SetIndent(unsigned n);
|
||||
bool SetPreCommentIndent(unsigned n);
|
||||
bool SetPostCommentIndent(unsigned n);
|
||||
bool SetFloatPrecision(unsigned n);
|
||||
bool SetDoublePrecision(unsigned n);
|
||||
|
||||
// local setters
|
||||
Emitter& SetLocalValue(EMITTER_MANIP value);
|
||||
Emitter& SetLocalIndent(const _Indent& indent);
|
||||
Emitter& SetLocalPrecision(const _Precision& precision);
|
||||
|
||||
// overloads of write
|
||||
Emitter& Write(const std::string& str);
|
||||
@@ -70,6 +73,10 @@ namespace YAML
|
||||
void PreWriteStreamable(std::stringstream& str);
|
||||
void PostWriteIntegralType(const std::stringstream& str);
|
||||
void PostWriteStreamable(const std::stringstream& str);
|
||||
|
||||
template<typename T> void SetStreamablePrecision(std::stringstream&) {}
|
||||
unsigned GetFloatPrecision() const;
|
||||
unsigned GetDoublePrecision() const;
|
||||
|
||||
private:
|
||||
void PreAtomicWrite();
|
||||
@@ -118,11 +125,24 @@ namespace YAML
|
||||
|
||||
std::stringstream str;
|
||||
PreWriteStreamable(str);
|
||||
SetStreamablePrecision<T>(str);
|
||||
str << value;
|
||||
PostWriteStreamable(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void Emitter::SetStreamablePrecision<float>(std::stringstream& str)
|
||||
{
|
||||
str.precision(GetFloatPrecision());
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void Emitter::SetStreamablePrecision<double>(std::stringstream& str)
|
||||
{
|
||||
str.precision(GetDoublePrecision());
|
||||
}
|
||||
|
||||
// overloads of insertion
|
||||
inline Emitter& operator << (Emitter& emitter, const std::string& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, bool v) { return emitter.Write(v); }
|
||||
@@ -156,6 +176,10 @@ namespace YAML
|
||||
inline Emitter& operator << (Emitter& emitter, _Indent indent) {
|
||||
return emitter.SetLocalIndent(indent);
|
||||
}
|
||||
|
||||
inline Emitter& operator << (Emitter& emitter, _Precision precision) {
|
||||
return emitter.SetLocalPrecision(precision);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -135,6 +135,25 @@ namespace YAML
|
||||
inline _Binary Binary(const unsigned char *data, std::size_t size) {
|
||||
return _Binary(data, size);
|
||||
}
|
||||
|
||||
struct _Precision {
|
||||
_Precision(int floatPrecision_, int doublePrecision_): floatPrecision(floatPrecision_), doublePrecision(doublePrecision_) {}
|
||||
|
||||
int floatPrecision;
|
||||
int doublePrecision;
|
||||
};
|
||||
|
||||
inline _Precision FloatPrecision(unsigned n) {
|
||||
return _Precision(n, -1);
|
||||
}
|
||||
|
||||
inline _Precision DoublePrecision(unsigned n) {
|
||||
return _Precision(-1, n);
|
||||
}
|
||||
|
||||
inline _Precision Precision(unsigned n) {
|
||||
return _Precision(n, n);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
Reference in New Issue
Block a user