2 #ifndef HOBBES_UTIL_CODEC_HPP_INCLUDED 3 #define HOBBES_UTIL_CODEC_HPP_INCLUDED 13 #define PRIM_CODEC(T) \ 14 inline void encode(T x, std::ostream& out) { out.write((const char*)&x, sizeof(x)); } \ 15 inline void decode(T* x, std::istream& in) { in.read((char*)x, sizeof(*x)); } 27 inline void encode(
const std::string& x, std::ostream&
out) {
29 out.write(x.c_str(), x.size());
32 inline void decode(std::string* x, std::istream&
in) {
37 in.read(&((*x)[0]), n);
41 inline void encode(
const std::vector<T>& xs, std::ostream&
out) {
43 for (
typename std::vector<T>::const_iterator x = xs.begin(); x != xs.end(); ++x) {
49 inline void decode(std::vector<T>* xs, std::istream&
in) {
53 for (
size_t i = 0; i < sz; ++i) {
60 template <
typename U,
typename V>
61 inline void encode(
const std::pair<U, V>& p, std::ostream&
out) {
66 template <
typename U,
typename V>
67 inline void decode(std::pair<U, V>* p, std::istream&
in) {
72 template <
typename K,
typename V>
73 inline void encode(
const std::map<K, V>&
m, std::ostream&
out) {
75 for (
typename std::map<K, V>::const_iterator p = m.begin(); p != m.end(); ++p) {
80 template <
typename K,
typename V>
81 inline void decode(std::map<K, V>*
m, std::istream&
in) {
85 for (
size_t i = 0; i < sz; ++i) {
89 (*m)[p.first] = p.second;
94 inline void fdread(
int fd,
char* x,
size_t len) {
99 ssize_t di =
read(fd, x + i, len - i);
102 if (errno != EINTR) {
103 throw std::runtime_error(
"Couldn't read pipe: " + std::string(strerror(errno)));
105 }
else if (di == 0) {
106 throw std::runtime_error(
"Process read error (closed pipe)");
113 inline void fdread(
int fd,
unsigned char* x,
size_t len) {
114 fdread(fd, (
char*)x, len);
118 fdread(fd, x,
sizeof(
char));
122 fdread(fd, x,
sizeof(uint8_t));
126 fdread(fd, (
char*)x,
sizeof(
int));
129 inline void fdread(
int fd, uint32_t* x) {
130 fdread(fd, (
char*)x,
sizeof(uint32_t));
134 fdread(fd, (
char*)x,
sizeof(
size_t));
137 inline void fdread(
int fd, std::string* x) {
141 fdread(fd, &((*x)[0]), n);
144 template <
typename T>
145 inline void fdread(
int fd, std::vector<T>* xs) {
149 fdread(fd, &((*xs)[0]), n);
152 inline void fdwrite(
int fd,
const char* x,
size_t len) {
155 ssize_t c =
write(fd, x + i, len - i);
157 throw std::runtime_error(
"Couldn't write to pipe: " + std::string(strerror(errno)));
163 inline void fdwrite(
int fd,
const unsigned char* x,
size_t len) {
164 fdwrite(fd, (
const char*)x, len);
168 fdwrite(fd, (
char*)&x,
sizeof(
char));
172 fdwrite(fd, (
char*)&x,
sizeof(uint8_t));
176 fdwrite(fd, (
char*)&x,
sizeof(
int));
180 fdwrite(fd, (
char*)&x,
sizeof(uint32_t));
184 fdwrite(fd, (
char*)&x,
sizeof(
size_t));
187 inline void fdwrite(
int fd,
const std::string& x) {
189 fdwrite(fd, x.data(), x.size());
192 template <
typename T>
193 inline void fdwrite(
int fd,
const std::vector<T>& xs) {
195 if (xs.size() > 0)
fdwrite(fd, &xs[0], xs.size());
void encode(const PrimitivePtr &, std::ostream &)
Definition: expr.C:1674
void fdwrite(int fd, const char *x, size_t len)
Definition: codec.H:152
void decode(PrimitivePtr *, std::istream &)
Definition: expr.C:1678
void write(imagefile *f, const T &x)
Definition: file.C:188
void fdread(int fd, char *x, size_t len)
Definition: codec.H:94
#define out
Definition: netio.H:19
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127
bool in(T x, const std::set< T > &xs)
Definition: array.H:47
void read(const imagefile *f, T *x)
Definition: file.C:215
bool unmarkBadFD(int fd)
Definition: funcdefs.C:486