Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
5024caa6
Commit
5024caa6
authored
Jan 11, 2012
by
Jesse Beder
Browse files
Added float/double precision setters
parent
21ed2b58
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
118 additions
and
2 deletions
+118
-2
include/yaml-cpp/emitter.h
include/yaml-cpp/emitter.h
+24
-0
include/yaml-cpp/emittermanip.h
include/yaml-cpp/emittermanip.h
+19
-0
src/emitter.cpp
src/emitter.cpp
+30
-2
src/emitterstate.cpp
src/emitterstate.cpp
+19
-0
src/emitterstate.h
src/emitterstate.h
+7
-0
test/emittertests.cpp
test/emittertests.cpp
+19
-0
No files found.
include/yaml-cpp/emitter.h
View file @
5024caa6
...
@@ -43,10 +43,13 @@ namespace YAML
...
@@ -43,10 +43,13 @@ namespace YAML
bool
SetIndent
(
unsigned
n
);
bool
SetIndent
(
unsigned
n
);
bool
SetPreCommentIndent
(
unsigned
n
);
bool
SetPreCommentIndent
(
unsigned
n
);
bool
SetPostCommentIndent
(
unsigned
n
);
bool
SetPostCommentIndent
(
unsigned
n
);
bool
SetFloatPrecision
(
unsigned
n
);
bool
SetDoublePrecision
(
unsigned
n
);
// local setters
// local setters
Emitter
&
SetLocalValue
(
EMITTER_MANIP
value
);
Emitter
&
SetLocalValue
(
EMITTER_MANIP
value
);
Emitter
&
SetLocalIndent
(
const
_Indent
&
indent
);
Emitter
&
SetLocalIndent
(
const
_Indent
&
indent
);
Emitter
&
SetLocalPrecision
(
const
_Precision
&
precision
);
// overloads of write
// overloads of write
Emitter
&
Write
(
const
std
::
string
&
str
);
Emitter
&
Write
(
const
std
::
string
&
str
);
...
@@ -70,6 +73,10 @@ namespace YAML
...
@@ -70,6 +73,10 @@ namespace YAML
void
PreWriteStreamable
(
std
::
stringstream
&
str
);
void
PreWriteStreamable
(
std
::
stringstream
&
str
);
void
PostWriteIntegralType
(
const
std
::
stringstream
&
str
);
void
PostWriteIntegralType
(
const
std
::
stringstream
&
str
);
void
PostWriteStreamable
(
const
std
::
stringstream
&
str
);
void
PostWriteStreamable
(
const
std
::
stringstream
&
str
);
template
<
typename
T
>
void
SetStreamablePrecision
(
std
::
stringstream
&
)
{}
unsigned
GetFloatPrecision
()
const
;
unsigned
GetDoublePrecision
()
const
;
private:
private:
void
PreAtomicWrite
();
void
PreAtomicWrite
();
...
@@ -118,11 +125,24 @@ namespace YAML
...
@@ -118,11 +125,24 @@ namespace YAML
std
::
stringstream
str
;
std
::
stringstream
str
;
PreWriteStreamable
(
str
);
PreWriteStreamable
(
str
);
SetStreamablePrecision
<
T
>
(
str
);
str
<<
value
;
str
<<
value
;
PostWriteStreamable
(
str
);
PostWriteStreamable
(
str
);
return
*
this
;
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
// overloads of insertion
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
const
std
::
string
&
v
)
{
return
emitter
.
Write
(
v
);
}
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
const
std
::
string
&
v
)
{
return
emitter
.
Write
(
v
);
}
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
bool
v
)
{
return
emitter
.
Write
(
v
);
}
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
bool
v
)
{
return
emitter
.
Write
(
v
);
}
...
@@ -156,6 +176,10 @@ namespace YAML
...
@@ -156,6 +176,10 @@ namespace YAML
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
_Indent
indent
)
{
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
_Indent
indent
)
{
return
emitter
.
SetLocalIndent
(
indent
);
return
emitter
.
SetLocalIndent
(
indent
);
}
}
inline
Emitter
&
operator
<<
(
Emitter
&
emitter
,
_Precision
precision
)
{
return
emitter
.
SetLocalPrecision
(
precision
);
}
}
}
#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
include/yaml-cpp/emittermanip.h
View file @
5024caa6
...
@@ -135,6 +135,25 @@ namespace YAML
...
@@ -135,6 +135,25 @@ namespace YAML
inline
_Binary
Binary
(
const
unsigned
char
*
data
,
std
::
size_t
size
)
{
inline
_Binary
Binary
(
const
unsigned
char
*
data
,
std
::
size_t
size
)
{
return
_Binary
(
data
,
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
#endif // EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
src/emitter.cpp
View file @
5024caa6
...
@@ -93,6 +93,16 @@ namespace YAML
...
@@ -93,6 +93,16 @@ namespace YAML
{
{
return
m_pState
->
SetPostCommentIndent
(
n
,
GLOBAL
);
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
// SetLocalValue
// . Either start/end a group, or set a modifier locally
// . Either start/end a group, or set a modifier locally
...
@@ -145,6 +155,15 @@ namespace YAML
...
@@ -145,6 +155,15 @@ namespace YAML
return
*
this
;
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
// GotoNextPreAtomicState
// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
bool
Emitter
::
GotoNextPreAtomicState
()
bool
Emitter
::
GotoNextPreAtomicState
()
...
@@ -661,13 +680,22 @@ namespace YAML
...
@@ -661,13 +680,22 @@ namespace YAML
}
}
}
}
void
Emitter
::
PreWriteStreamable
(
std
::
stringstream
&
str
)
void
Emitter
::
PreWriteStreamable
(
std
::
stringstream
&
)
{
{
PreAtomicWrite
();
PreAtomicWrite
();
EmitSeparationIfNecessary
();
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
)
void
Emitter
::
PostWriteIntegralType
(
const
std
::
stringstream
&
str
)
{
{
m_stream
<<
str
.
str
();
m_stream
<<
str
.
str
();
...
...
src/emitterstate.cpp
View file @
5024caa6
#include "emitterstate.h"
#include "emitterstate.h"
#include "yaml-cpp/exceptions.h"
#include "yaml-cpp/exceptions.h"
#include <limits>
namespace
YAML
namespace
YAML
{
{
...
@@ -21,6 +22,8 @@ namespace YAML
...
@@ -21,6 +22,8 @@ namespace YAML
m_seqFmt
.
set
(
Block
);
m_seqFmt
.
set
(
Block
);
m_mapFmt
.
set
(
Block
);
m_mapFmt
.
set
(
Block
);
m_mapKeyFmt
.
set
(
Auto
);
m_mapKeyFmt
.
set
(
Auto
);
m_floatPrecision
.
set
(
6
);
m_doublePrecision
.
set
(
15
);
}
}
EmitterState
::~
EmitterState
()
EmitterState
::~
EmitterState
()
...
@@ -261,5 +264,21 @@ namespace YAML
...
@@ -261,5 +264,21 @@ namespace YAML
return
false
;
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
;
}
}
}
src/emitterstate.h
View file @
5024caa6
...
@@ -145,6 +145,11 @@ namespace YAML
...
@@ -145,6 +145,11 @@ namespace YAML
bool
SetMapKeyFormat
(
EMITTER_MANIP
value
,
FMT_SCOPE
scope
);
bool
SetMapKeyFormat
(
EMITTER_MANIP
value
,
FMT_SCOPE
scope
);
EMITTER_MANIP
GetMapKeyFormat
()
const
{
return
m_mapKeyFmt
.
get
();
}
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:
private:
template
<
typename
T
>
template
<
typename
T
>
...
@@ -169,6 +174,8 @@ namespace YAML
...
@@ -169,6 +174,8 @@ namespace YAML
Setting
<
EMITTER_MANIP
>
m_seqFmt
;
Setting
<
EMITTER_MANIP
>
m_seqFmt
;
Setting
<
EMITTER_MANIP
>
m_mapFmt
;
Setting
<
EMITTER_MANIP
>
m_mapFmt
;
Setting
<
EMITTER_MANIP
>
m_mapKeyFmt
;
Setting
<
EMITTER_MANIP
>
m_mapKeyFmt
;
Setting
<
unsigned
>
m_floatPrecision
;
Setting
<
unsigned
>
m_doublePrecision
;
SettingChanges
m_modifiedSettings
;
SettingChanges
m_modifiedSettings
;
SettingChanges
m_globalModifiedSettings
;
SettingChanges
m_globalModifiedSettings
;
...
...
test/emittertests.cpp
View file @
5024caa6
...
@@ -874,7 +874,24 @@ namespace Test
...
@@ -874,7 +874,24 @@ namespace Test
out
<<
YAML
::
EndSeq
;
out
<<
YAML
::
EndSeq
;
desiredOutput
=
"- a
\n
-
\"
:
\"\n
-
\"\\
x10
\"\n
-
\"\\
n
\"\n
-
\"
\"\n
-
\"\\
t
\"
"
;
desiredOutput
=
"- a
\n
-
\"
:
\"\n
-
\"\\
x10
\"\n
-
\"\\
n
\"\n
-
\"
\"\n
-
\"\\
t
\"
"
;
}
}
void
DefaultPrecision
(
YAML
::
Emitter
&
out
,
std
::
string
&
desiredOutput
)
{
out
<<
YAML
::
BeginSeq
;
out
<<
1.234
f
;
out
<<
3.14159265358979
;
out
<<
YAML
::
EndSeq
;
desiredOutput
=
"- 1.234
\n
- 3.14159265358979"
;
}
void
SetPrecision
(
YAML
::
Emitter
&
out
,
std
::
string
&
desiredOutput
)
{
out
<<
YAML
::
BeginSeq
;
out
<<
YAML
::
FloatPrecision
(
3
)
<<
1.234
f
;
out
<<
YAML
::
DoublePrecision
(
6
)
<<
3.14159265358979
;
out
<<
YAML
::
EndSeq
;
desiredOutput
=
"- 1.23
\n
- 3.14159"
;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting
// incorrect emitting
...
@@ -1094,6 +1111,8 @@ namespace Test
...
@@ -1094,6 +1111,8 @@ namespace Test
RunEmitterTest
(
&
Emitter
::
ImplicitDocStart
,
"implicit doc start"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
ImplicitDocStart
,
"implicit doc start"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
EmptyString
,
"empty string"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
EmptyString
,
"empty string"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
SingleChar
,
"single char"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
SingleChar
,
"single char"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
DefaultPrecision
,
"default precision"
,
passed
,
total
);
RunEmitterTest
(
&
Emitter
::
SetPrecision
,
"set precision"
,
passed
,
total
);
RunEmitterErrorTest
(
&
Emitter
::
ExtraEndSeq
,
"extra EndSeq"
,
passed
,
total
);
RunEmitterErrorTest
(
&
Emitter
::
ExtraEndSeq
,
"extra EndSeq"
,
passed
,
total
);
RunEmitterErrorTest
(
&
Emitter
::
ExtraEndMap
,
"extra EndMap"
,
passed
,
total
);
RunEmitterErrorTest
(
&
Emitter
::
ExtraEndMap
,
"extra EndMap"
,
passed
,
total
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment