Loading...
Searching...
No Matches
bytebuf.h
Go to the documentation of this file.
#ifndef _SINGULAR_LIBTHREAD_BYTEBUFFER_H
#define _SINGULAR_LIBTHREAD_BYTEBUFFER_H
#include <cstddef>
#include <cstdlib>
#include <cstring>
namespace LibThread {
using namespace std;
char *allocate_space (size_t n);
void free_space(size_t n, char *p);
class ByteBuf {
private:
size_t count;
size_t cap;
size_t pos;
char *buf;
public:
ByteBuf() : pos(0), count(0), cap(0) { }
ByteBuf(const ByteBuf &other) :
count(other.count), cap(other.cap), pos(0)
{
buf = allocate_space(cap);
memcpy(buf, other.buf, count);
}
ByteBuf &operator=(const ByteBuf &other) {
count = other.count;
cap = other.cap;
pos = 0;
buf = allocate_space(cap);
memcpy(buf, other.buf, count);
return *this;
}
~ByteBuf() {
free_space(cap, buf);
}
size_t size() { return count; }
void write_bytes(char *p, size_t n) {
if (pos + n >= cap) {
char *newbuf = allocate_space(2 * cap);
memcpy(newbuf, buf, count);
free_space(cap, buf);
cap *= 2;
}
memcpy(buf+pos, p, n);
pos += n;
if (pos >= count)
count = pos;
}
void read_bytes(char *p, size_t n) {
memcpy(p, buf+pos, n);
pos += n;
}
template <typename T>
void write(T &value) {
write_bytes((char *)&value, sizeof(T));
}
template <typename T>
void read(T &value) {
read_bytes((char *)&value, sizeof(T));
}
void seek(size_t p) {
pos = p;
}
};
}
#endif // _SINGULAR_LIBTHREAD_BYTEBUFFER_H
p
int p
Definition cfModGcd.cc:4086
LibThread::ByteBuf::size
size_t size()
Definition bytebuf.h:40
LibThread::ByteBuf::operator=
ByteBuf & operator=(const ByteBuf &other)
Definition bytebuf.h:29
LibThread::ByteBuf::~ByteBuf
~ByteBuf()
Definition bytebuf.h:37
LibThread::ByteBuf::pos
size_t pos
Definition bytebuf.h:19
LibThread::ByteBuf::ByteBuf
ByteBuf(const ByteBuf &other)
Definition bytebuf.h:23
LibThread::ByteBuf::buf
char * buf
Definition bytebuf.h:20
LibThread::ByteBuf::seek
void seek(size_t p)
Definition bytebuf.h:65
LibThread::ByteBuf::count
size_t count
Definition bytebuf.h:17
LibThread::ByteBuf::write_bytes
void write_bytes(char *p, size_t n)
Definition bytebuf.h:41
LibThread::ByteBuf::write
void write(T &value)
Definition bytebuf.h:58
LibThread::ByteBuf::read_bytes
void read_bytes(char *p, size_t n)
Definition bytebuf.h:53
LibThread::ByteBuf::cap
size_t cap
Definition bytebuf.h:18
LibThread::ByteBuf::read
void read(T &value)
Definition bytebuf.h:62
LibThread::ByteBuf::ByteBuf
ByteBuf()
Definition bytebuf.h:22
T
STATIC_VAR jList * T
Definition janet.cc:30
LibThread
Definition bytebuf.h:8
LibThread::free_space
void free_space(size_t n, char *p)
LibThread::allocate_space
char * allocate_space(size_t n)