|
|
|
|
|
#ifndef __Pickler |
|
#define __Pickler |
|
|
|
#include <iostream> |
|
#include <string> |
|
#include <vector> |
|
#include <map> |
|
#include "tpt_typedefs.h" |
|
#include "num_read_write.h" |
|
#include <cassert> |
|
|
|
namespace tpt |
|
{ |
|
|
|
|
|
::uint64_t getFileSize(const std::string& fname); |
|
|
|
|
|
|
|
|
|
|
|
|
|
void binwrite(std::ostream& out, char data); |
|
void binwrite(std::ostream& out, unsigned char data); |
|
void binwrite(std::ostream& out, unsigned short data); |
|
void binwrite(std::ostream& out, unsigned int data); |
|
void binwrite(std::ostream& out, unsigned long data); |
|
void binwrite(std::ostream& out, size_t data); |
|
void binwrite(std::ostream& out, unsigned long long data); |
|
void binwrite(std::ostream& out, std::string const& data); |
|
void binwrite(std::ostream& out, float data); |
|
|
|
void binread(std::istream& in, char &data); |
|
void binread(std::istream& in, unsigned char &data); |
|
void binread(std::istream& in, unsigned short &data); |
|
void binread(std::istream& in, unsigned int &data); |
|
void binread(std::istream& in, unsigned long &data); |
|
void binread(std::istream& in, size_t &data); |
|
void binread(std::istream& in, unsigned long long &data); |
|
void binread(std::istream& in, std::string &data); |
|
void binread(std::istream& in, float &data); |
|
|
|
char const *binread(char const* p, uint16_t& buf); |
|
char const *binread(char const* p, uint32_t& buf); |
|
char const *binread(char const* p, uint64_t& buf); |
|
char const *binread(char const* p, float& buf); |
|
#ifdef __clang__ |
|
char const *binread(char const* p, size_t& buf); |
|
#endif |
|
|
|
std::ostream& write(std::ostream& out, char x); |
|
std::ostream& write(std::ostream& out, unsigned char x); |
|
std::ostream& write(std::ostream& out, short x); |
|
std::ostream& write(std::ostream& out, unsigned short x); |
|
std::ostream& write(std::ostream& out, long x); |
|
std::ostream& write(std::ostream& out, size_t x); |
|
std::ostream& write(std::ostream& out, float x); |
|
|
|
std::istream& read(std::istream& in, char& x); |
|
std::istream& read(std::istream& in, unsigned char& x); |
|
std::istream& read(std::istream& in, short& x); |
|
std::istream& read(std::istream& in, unsigned short& x); |
|
std::istream& read(std::istream& in, long& x); |
|
std::istream& read(std::istream& in, size_t& x); |
|
std::istream& read(std::istream& in, float& x); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename K, typename V> |
|
void binwrite(std::ostream& out, std::pair<K,V> const& data); |
|
|
|
template<typename K, typename V> |
|
void binread(std::istream& in, std::pair<K,V>& data); |
|
|
|
template<typename K, typename V> |
|
char const* binread(char const* p, std::pair<K,V>& data); |
|
|
|
template<typename V> |
|
char const* binread(char const* p, std::vector<V>& v); |
|
|
|
|
|
template<typename K, typename V> |
|
char const* binread(char const* p, std::pair<K,V>& data) |
|
{ |
|
#ifdef VERIFY_TIGHT_PACKING |
|
assert(p); |
|
#endif |
|
p = binread(p,data.first); |
|
p = binread(p,data.second); |
|
return p; |
|
} |
|
|
|
template<typename V> |
|
char const* binread(char const* p, std::vector<V>& v) |
|
{ |
|
size_t vsize; |
|
#ifdef VERIFY_TIGHT_PACKING |
|
assert(p); |
|
#endif |
|
p = binread(p, vsize); |
|
v.resize(vsize); |
|
for (size_t i = 0; i < vsize; ++i) |
|
p = binread(p,v[i]); |
|
return p; |
|
} |
|
|
|
template<typename T> |
|
T read(std::istream& in) |
|
{ |
|
T ret; |
|
read(in,ret); |
|
return ret; |
|
} |
|
|
|
template<typename T> |
|
T binread(std::istream& in) |
|
{ |
|
T ret; |
|
binread(in,ret); |
|
return ret; |
|
} |
|
|
|
|
|
template<typename T> |
|
void |
|
binwrite(std::ostream& out, std::vector<T> const& data) |
|
{ |
|
binwrite(out,data.size()); |
|
for (size_t i = 0; i < data.size(); i++) |
|
{ binwrite(out,data[i]); } |
|
} |
|
|
|
template<typename T> |
|
void |
|
binread(std::istream& in, std::vector<T>& data) |
|
{ |
|
size_t s; |
|
binread(in,s); |
|
data.resize(s); |
|
for (size_t i = 0; i < s; i++) |
|
{ binread(in,data[i]); } |
|
} |
|
|
|
template<typename K, typename V> |
|
void |
|
binread(std::istream& in, std::map<K,V>& data) |
|
{ |
|
size_t s; K k; V v; |
|
binread(in,s); |
|
data.clear(); |
|
|
|
|
|
for (size_t i = 0; i < s; i++) |
|
{ |
|
binread(in,k); |
|
binread(in,v); |
|
data[k] = v; |
|
|
|
} |
|
} |
|
|
|
template<typename K, typename V> |
|
void |
|
binwrite(std::ostream& out, std::map<K,V> const& data) |
|
{ |
|
binwrite(out,data.size()); |
|
for (typename std::map<K,V>::const_iterator m = data.begin(); |
|
m != data.end(); m++) |
|
{ |
|
binwrite(out,m->first); |
|
binwrite(out,m->second); |
|
} |
|
} |
|
|
|
template<typename K, typename V> |
|
void |
|
binwrite(std::ostream& out, std::pair<K,V> const& data) |
|
{ |
|
binwrite(out,data.first); |
|
binwrite(out,data.second); |
|
} |
|
|
|
template<typename K, typename V> |
|
void |
|
binread(std::istream& in, std::pair<K,V>& data) |
|
{ |
|
binread(in,data.first); |
|
binread(in,data.second); |
|
} |
|
|
|
|
|
template<typename WHATEVER> |
|
char const* |
|
binread(char const* p, WHATEVER* buf) |
|
{ |
|
#ifdef VERIFY_TIGHT_PACKING |
|
assert(p); |
|
#endif |
|
return binread(p,*buf); |
|
} |
|
|
|
} |
|
#endif |
|
|