Added Reader.

This commit is contained in:
vng 2013-08-06 17:36:27 +03:00
parent 2cba29d8e1
commit 3b3e5431c0
4 changed files with 68 additions and 0 deletions

1
env/env.pro vendored
View file

@ -20,6 +20,7 @@ HEADERS += \
writer.hpp \
thread.hpp \
message_std.hpp \
reader.hpp \
SOURCES += \
assert.cpp \

49
env/reader.hpp vendored Normal file
View file

@ -0,0 +1,49 @@
#pragma once
#include "file_handle.hpp"
namespace rd
{
class Reader
{
public:
virtual ~Reader() {}
virtual void Read(void * p, size_t size) = 0;
template <typename T> void Read(T & t)
{
Read(static_cast<void *>(&t), sizeof(T));
}
void Read(string & s)
{
uint32_t size;
Read(size);
s.resize(size);
Read(&s[0], size);
}
};
class SequenceFileReader : public Reader
{
typedef file::FileHandle HandleT;
HandleT m_file;
public:
SequenceFileReader(string const & name)
: m_file(name, HandleT::READ)
{
}
using Reader::Read;
virtual void Read(void * p, size_t size)
{
m_file.Read(p, size);
}
};
}

14
env/writer.hpp vendored
View file

@ -11,8 +11,21 @@ class Writer
public:
virtual ~Writer() {}
virtual void Write(void const * p, size_t size) = 0;
template <typename T> void Write(T const & t)
{
Write(static_cast<void const *>(&t), sizeof(T));
}
void Write(string const & s)
{
size_t const count = s.size();
Write(static_cast<uint32_t>(count));
Write(s.c_str(), count);
}
};
class FileWriter : public Writer
{
typedef file::FileHandle HandleT;
@ -24,6 +37,7 @@ public:
{
}
using Writer::Write;
virtual void Write(void const * p, size_t size)
{
m_file.Write(p, size);

4
std/cmath.hpp Normal file
View file

@ -0,0 +1,4 @@
#pragma once
#include <cmath>
using std::fabs;