Added Reader.
This commit is contained in:
parent
2cba29d8e1
commit
3b3e5431c0
4 changed files with 68 additions and 0 deletions
1
env/env.pro
vendored
1
env/env.pro
vendored
|
@ -20,6 +20,7 @@ HEADERS += \
|
|||
writer.hpp \
|
||||
thread.hpp \
|
||||
message_std.hpp \
|
||||
reader.hpp \
|
||||
|
||||
SOURCES += \
|
||||
assert.cpp \
|
||||
|
|
49
env/reader.hpp
vendored
Normal file
49
env/reader.hpp
vendored
Normal 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
14
env/writer.hpp
vendored
|
@ -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
4
std/cmath.hpp
Normal file
|
@ -0,0 +1,4 @@
|
|||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
using std::fabs;
|
Reference in a new issue