From 6f94f954bb9cb88931c467e8e2389512d5d2065d Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Fri, 6 Nov 2009 03:24:12 +0000 Subject: [PATCH] Overloaded more integral types for emitting --- include/emitter.h | 27 ++++++++++++++++++++++++++- src/emitter.cpp | 18 +++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/include/emitter.h b/include/emitter.h index c13b9cc..259d899 100644 --- a/include/emitter.h +++ b/include/emitter.h @@ -46,7 +46,12 @@ namespace YAML // overloads of write Emitter& Write(const std::string& str); Emitter& Write(const char *str); - Emitter& Write(int i); + Emitter& Write(int value) { return WriteIntegralType(value); } + Emitter& Write(unsigned int value) { return WriteIntegralType(value); } + Emitter& Write(short value) { return WriteIntegralType(value); } + Emitter& Write(unsigned short value) { return WriteIntegralType(value); } + Emitter& Write(long value) { return WriteIntegralType(value); } + Emitter& Write(unsigned long value) { return WriteIntegralType(value); } Emitter& Write(bool b); Emitter& Write(float f); Emitter& Write(double d); @@ -56,6 +61,13 @@ namespace YAML Emitter& Write(const _Comment& comment); Emitter& Write(const _Null& null); + private: + void PreWriteIntegralType(std::stringstream& str); + void PostWriteIntegralType(const std::stringstream& str); + + template + Emitter& WriteIntegralType(T value); + private: enum ATOMIC_TYPE { AT_SCALAR, AT_SEQ, AT_BLOCK_SEQ, AT_FLOW_SEQ, AT_MAP, AT_BLOCK_MAP, AT_FLOW_MAP }; @@ -76,6 +88,19 @@ namespace YAML std::auto_ptr m_pState; }; + template + inline Emitter& Emitter::WriteIntegralType(T value) + { + if(!good()) + return *this; + + std::stringstream str; + PreWriteIntegralType(str); + str << value; + PostWriteIntegralType(str); + return *this; + } + // overloads of insertion template inline Emitter& operator << (Emitter& emitter, T v) { diff --git a/src/emitter.cpp b/src/emitter.cpp index e987ad0..bb5f80b 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -556,17 +556,13 @@ namespace YAML return Write(std::string(str)); } - - Emitter& Emitter::Write(int i) + + void Emitter::PreWriteIntegralType(std::stringstream& str) { - if(!good()) - return *this; - PreAtomicWrite(); EmitSeparationIfNecessary(); EMITTER_MANIP intFmt = m_pState->GetIntFormat(); - std::stringstream str; switch(intFmt) { case Dec: str << std::dec; @@ -574,18 +570,18 @@ namespace YAML case Hex: str << std::hex; break; - case Oct: + case Oct: str << std::oct; break; default: assert(false); } - - str << i; + } + + void Emitter::PostWriteIntegralType(const std::stringstream& str) + { m_stream << str.str(); - PostAtomicWrite(); - return *this; } Emitter& Emitter::Write(bool b)