2013-12-28 00:45:40 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstring>
|
|
|
|
#include "util/extensible_object.h"
|
2014-02-24 19:05:42 +00:00
|
|
|
#include "util/list.h"
|
|
|
|
#include "util/buffer.h"
|
2013-12-28 00:45:40 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief Low-tech serializer.
|
|
|
|
The actual functionality is implemented using extensions.
|
|
|
|
*/
|
|
|
|
class serializer_core {
|
|
|
|
std::ostream & m_out;
|
|
|
|
public:
|
|
|
|
serializer_core(std::ostream & out):m_out(out) {}
|
|
|
|
void write_string(char const * str) { m_out.write(str, strlen(str) + 1); }
|
|
|
|
void write_string(std::string const & str) { m_out.write(str.c_str(), str.size() + 1); }
|
2013-12-28 02:07:19 +00:00
|
|
|
void write_unsigned(unsigned i);
|
|
|
|
void write_int(int i);
|
2013-12-28 01:22:15 +00:00
|
|
|
void write_char(char c) { m_out.put(c); }
|
2013-12-28 00:45:40 +00:00
|
|
|
void write_bool(bool b) { m_out.put(b ? 1 : 0); }
|
2013-12-28 03:46:45 +00:00
|
|
|
void write_double(double b);
|
2013-12-28 00:45:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef extensible_object<serializer_core> serializer;
|
|
|
|
|
|
|
|
inline serializer & operator<<(serializer & s, char const * str) { s.write_string(str); return s; }
|
|
|
|
inline serializer & operator<<(serializer & s, std::string const & str) { s.write_string(str); return s; }
|
|
|
|
inline serializer & operator<<(serializer & s, unsigned i) { s.write_unsigned(i); return s; }
|
|
|
|
inline serializer & operator<<(serializer & s, int i) { s.write_int(i); return s; }
|
2013-12-28 01:22:15 +00:00
|
|
|
inline serializer & operator<<(serializer & s, char c) { s.write_char(c); return s; }
|
2013-12-28 00:45:40 +00:00
|
|
|
inline serializer & operator<<(serializer & s, bool b) { s.write_bool(b); return s; }
|
2013-12-28 03:46:45 +00:00
|
|
|
inline serializer & operator<<(serializer & s, double b) { s.write_double(b); return s; }
|
2013-12-28 00:45:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Low-tech serializer.
|
|
|
|
The actual functionality is implemented using extensions.
|
|
|
|
*/
|
|
|
|
class deserializer_core {
|
|
|
|
std::istream & m_in;
|
|
|
|
public:
|
|
|
|
deserializer_core(std::istream & in):m_in(in) {}
|
|
|
|
std::string read_string();
|
2013-12-28 02:07:19 +00:00
|
|
|
unsigned read_unsigned();
|
|
|
|
int read_int();
|
2013-12-28 01:22:15 +00:00
|
|
|
char read_char() { return m_in.get(); }
|
2013-12-28 00:45:40 +00:00
|
|
|
bool read_bool() { return m_in.get() != 0; }
|
2013-12-28 03:46:45 +00:00
|
|
|
double read_double();
|
2013-12-28 00:45:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef extensible_object<deserializer_core> deserializer;
|
|
|
|
|
|
|
|
inline deserializer & operator>>(deserializer & d, std::string & str) { str = d.read_string(); return d; }
|
|
|
|
inline deserializer & operator>>(deserializer & d, unsigned & i) { i = d.read_unsigned(); return d; }
|
|
|
|
inline deserializer & operator>>(deserializer & d, int & i) { i = d.read_int(); return d; }
|
2013-12-28 01:22:15 +00:00
|
|
|
inline deserializer & operator>>(deserializer & d, char & c) { c = d.read_char(); return d; }
|
2013-12-28 00:45:40 +00:00
|
|
|
inline deserializer & operator>>(deserializer & d, bool & b) { b = d.read_bool(); return d; }
|
2013-12-28 03:46:45 +00:00
|
|
|
inline deserializer & operator>>(deserializer & d, double & b) { b = d.read_double(); return d; }
|
2013-12-31 02:05:38 +00:00
|
|
|
|
2014-08-14 17:59:09 +00:00
|
|
|
class corrupted_stream_exception : public exception {
|
|
|
|
public:
|
|
|
|
corrupted_stream_exception();
|
|
|
|
};
|
2014-02-24 19:05:42 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
serializer & write_list(serializer & s, list<T> const & ls) {
|
|
|
|
s << length(ls);
|
|
|
|
for (auto const & e : ls)
|
|
|
|
s << e;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename R>
|
|
|
|
list<T> read_list(deserializer & d, R && t_reader) {
|
|
|
|
unsigned num = d.read_unsigned();
|
|
|
|
buffer<T> ls;
|
|
|
|
for (unsigned i = 0; i < num; i++)
|
|
|
|
ls.push_back(t_reader(d));
|
|
|
|
return to_list(ls.begin(), ls.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
list<T> read_list(deserializer & d) {
|
|
|
|
return read_list<T>(d, [](deserializer & d) { T r; d >> r; return r; });
|
|
|
|
}
|
2013-12-28 00:45:40 +00:00
|
|
|
}
|