diff options
Diffstat (limited to 'src/lib/netlist/plib/pstream.h')
-rw-r--r-- | src/lib/netlist/plib/pstream.h | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index 83c514da2e1..10b2e9a82a5 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -12,6 +12,8 @@ #include "pfmtlog.h" #include "pexception.h" +#include <vector> + namespace plib { // ----------------------------------------------------------------------------- // pstream: things common to all streams @@ -371,6 +373,76 @@ protected: private: }; +// ----------------------------------------------------------------------------- +// pbinary_writer_t: writer on top of ostream +// ----------------------------------------------------------------------------- + +class pbinary_writer : plib::nocopyassignmove +{ +public: + explicit pbinary_writer(postream &strm) : m_strm(strm) {} + virtual ~pbinary_writer() {} + + template <typename T> + void write(const T val) + { + m_strm.write(&val, sizeof(T)); + } + + void write(const pstring s) + { + write(s.blen()); + m_strm.write(s.c_str(), s.blen()); + } + + template <typename T> + void write(const std::vector<T> val) + { + std::size_t sz = val.size(); + write(sz); + m_strm.write(val.data(), sizeof(T) * sz); + } + +private: + postream &m_strm; +}; + +class pbinary_reader : plib::nocopyassignmove +{ +public: + explicit pbinary_reader(pistream &strm) : m_strm(strm) {} + virtual ~pbinary_reader() {} + + template <typename T> + void read(T &val) + { + m_strm.read(&val, sizeof(T)); + } + + void read( pstring &s) + { + std::size_t sz = 0; + read(sz); + pstring::mem_t *buf = new pstring::mem_t[sz+1]; + m_strm.read(buf, sz); + buf[sz] = 0; + s = pstring(buf, pstring::UTF8); + delete [] buf; + } + + template <typename T> + void read(std::vector<T> &val) + { + std::size_t sz = 0; + read(sz); + val.resize(sz); + m_strm.read(val.data(), sizeof(T) * sz); + } + +private: + pistream &m_strm; +}; + } #endif /* PSTREAM_H_ */ |