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:
@@ -93,6 +93,16 @@ namespace YAML
|
||||
{
|
||||
return m_pState->SetPostCommentIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetFloatPrecision(unsigned n)
|
||||
{
|
||||
return m_pState->SetFloatPrecision(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetDoublePrecision(unsigned n)
|
||||
{
|
||||
return m_pState->SetDoublePrecision(n, GLOBAL);
|
||||
}
|
||||
|
||||
// SetLocalValue
|
||||
// . Either start/end a group, or set a modifier locally
|
||||
@@ -145,6 +155,15 @@ namespace YAML
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::SetLocalPrecision(const _Precision& precision)
|
||||
{
|
||||
if(precision.floatPrecision >= 0)
|
||||
m_pState->SetFloatPrecision(precision.floatPrecision, LOCAL);
|
||||
if(precision.doublePrecision >= 0)
|
||||
m_pState->SetDoublePrecision(precision.doublePrecision, LOCAL);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// GotoNextPreAtomicState
|
||||
// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
|
||||
bool Emitter::GotoNextPreAtomicState()
|
||||
@@ -661,13 +680,22 @@ namespace YAML
|
||||
}
|
||||
}
|
||||
|
||||
void Emitter::PreWriteStreamable(std::stringstream& str)
|
||||
void Emitter::PreWriteStreamable(std::stringstream&)
|
||||
{
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
str.precision(15);
|
||||
}
|
||||
|
||||
unsigned Emitter::GetFloatPrecision() const
|
||||
{
|
||||
return m_pState->GetFloatPrecision();
|
||||
}
|
||||
|
||||
unsigned Emitter::GetDoublePrecision() const
|
||||
{
|
||||
return m_pState->GetDoublePrecision();
|
||||
}
|
||||
|
||||
void Emitter::PostWriteIntegralType(const std::stringstream& str)
|
||||
{
|
||||
m_stream << str.str();
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include "emitterstate.h"
|
||||
#include "yaml-cpp/exceptions.h"
|
||||
#include <limits>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
@@ -21,6 +22,8 @@ namespace YAML
|
||||
m_seqFmt.set(Block);
|
||||
m_mapFmt.set(Block);
|
||||
m_mapKeyFmt.set(Auto);
|
||||
m_floatPrecision.set(6);
|
||||
m_doublePrecision.set(15);
|
||||
}
|
||||
|
||||
EmitterState::~EmitterState()
|
||||
@@ -261,5 +264,21 @@ namespace YAML
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetFloatPrecision(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value > std::numeric_limits<float>::digits10)
|
||||
return false;
|
||||
_Set(m_floatPrecision, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetDoublePrecision(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value > std::numeric_limits<double>::digits10)
|
||||
return false;
|
||||
_Set(m_doublePrecision, value, scope);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -145,6 +145,11 @@ namespace YAML
|
||||
|
||||
bool SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
|
||||
|
||||
bool SetFloatPrecision(unsigned value, FMT_SCOPE scope);
|
||||
unsigned GetFloatPrecision() const { return m_floatPrecision.get(); }
|
||||
bool SetDoublePrecision(unsigned value, FMT_SCOPE scope);
|
||||
unsigned GetDoublePrecision() const { return m_doublePrecision.get(); }
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
@@ -169,6 +174,8 @@ namespace YAML
|
||||
Setting<EMITTER_MANIP> m_seqFmt;
|
||||
Setting<EMITTER_MANIP> m_mapFmt;
|
||||
Setting<EMITTER_MANIP> m_mapKeyFmt;
|
||||
Setting<unsigned> m_floatPrecision;
|
||||
Setting<unsigned> m_doublePrecision;
|
||||
|
||||
SettingChanges m_modifiedSettings;
|
||||
SettingChanges m_globalModifiedSettings;
|
||||
|
Reference in New Issue
Block a user