From 54d789204fec8478eedd936f9a6d10e4ce5d13dd Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Mon, 7 May 2018 21:17:46 +0100 Subject: [PATCH] more general implementation of the precision interface for serialisers --- lib/serialisation/BaseIO.h | 32 +++++++++++++++++++++++++++++++- lib/serialisation/XmlIO.h | 11 +++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/serialisation/BaseIO.h b/lib/serialisation/BaseIO.h index bc178e0d..dd15e7da 100644 --- a/lib/serialisation/BaseIO.h +++ b/lib/serialisation/BaseIO.h @@ -60,8 +60,14 @@ namespace Grid { void write(const std::string &s, const iVector &output); template void write(const std::string &s, const iMatrix &output); + void scientificFormat(const bool set); + bool isScientific(void); + void setPrecision(const unsigned int prec); + unsigned int getPrecision(void); private: - T *upcast; + T *upcast; + bool scientific_{false}; + unsigned int prec_{0}; }; // Static abstract reader @@ -155,6 +161,30 @@ namespace Grid { { upcast->writeDefault(s, tensorToVec(output)); } + + template + void Writer::scientificFormat(const bool set) + { + scientific_ = set; + } + + template + bool Writer::isScientific(void) + { + return scientific_; + } + + template + void Writer::setPrecision(const unsigned int prec) + { + prec_ = prec; + } + + template + unsigned int Writer::getPrecision(void) + { + return prec_; + } // Reader template implementation template diff --git a/lib/serialisation/XmlIO.h b/lib/serialisation/XmlIO.h index 792505d4..17ccb32e 100644 --- a/lib/serialisation/XmlIO.h +++ b/lib/serialisation/XmlIO.h @@ -103,8 +103,15 @@ namespace Grid { std::ostringstream os; - os.precision(16); - os << std::scientific << std::boolalpha << x; + if (getPrecision()) + { + os.precision(getPrecision()); + } + if (isScientific()) + { + os << std::scientific; + } + os << std::boolalpha << x; pugi::xml_node leaf = node_.append_child(s.c_str()); leaf.append_child(pugi::node_pcdata).set_value(os.str().c_str()); }