Adds more stuff to ker::

This commit is contained in:
Felix Queissner 2015-10-18 20:08:29 +02:00
parent 40baa407a2
commit bb7e42970e
4 changed files with 212 additions and 0 deletions

View file

@ -0,0 +1,64 @@
#pragma once
#include <stddef.h>
#include "fixedstring.hpp"
class BinaryReader
{
public:
const char * const start;
const size_t length;
private:
size_t current;
size_t remaining;
public:
BinaryReader(const void *src, size_t length) :
start(reinterpret_cast<const char *>(src)),
length(length),
current(0),
remaining(length)
{
}
void jumpTo(size_t offset)
{
if(offset > this->length) {
return;
}
this->current = offset;
this->remaining = this->length - offset;
}
size_t position() const {
return this->current;
}
template<typename T>
T read()
{
if(this->remaining < sizeof(T)) {
return T();
}
const T *ptr = reinterpret_cast<const T*>(this->start + this->current);
this->current += sizeof(T);
this->remaining -= sizeof(T);
return *ptr;
}
};
template<>
inline FixedString BinaryReader::read<FixedString>()
{
FixedString str;
str.len = this->read<uint32_t>();
str.front = this->start + this->current;
this->current += str.len;
this->remaining -= str.len;
return str;
}

View file

@ -0,0 +1,67 @@
#pragma once
#include <ker/vector.hpp>
#include <inttypes.h>
#include "fixedstring.hpp"
class BinaryWriter
{
private:
ker::Vector<uint8_t> mData;
uint32_t mPointer;
public:
BinaryWriter() : mData(), mPointer(0)
{
this->mData.reserve(1024);
}
ker::Vector<uint8_t> &data() { return this->mData; }
const ker::Vector<uint8_t> &data() const { return this->mData; }
size_t size() const {
return this->mData.length();
}
uint32_t tell() const {
return this->mPointer;
}
void rewind() {
this->mPointer = 0;
}
void fastForwad() {
this->mPointer = this->mData.length();
}
void seek(uint32_t position) {
this->mPointer = position;
}
template<typename T>
uint32_t write(const T &bits)
{
uint32_t loc = this->mPointer;
if((this->mPointer + sizeof(T)) > this->mData.length())
{
this->mData.resize(this->mPointer + sizeof(T));
}
this->mPointer += sizeof(T);
uint8_t *data = &(this->mData[loc]);
new (data) T (bits);
return loc;
}
};
template<>
inline uint32_t BinaryWriter::write<FixedString>(const FixedString &bits)
{
uint32_t loc = this->write<uint32_t>(bits.len);
for(uint32_t k = 0; k < bits.len; k++) {
this->write<uint8_t>(bits.front[k]);
}
return loc;
}

View file

@ -0,0 +1,50 @@
#pragma once
#include <inttypes.h>
#define FIXED_STRING_AVAILABLE
#include <ker/string.hpp>
struct FixedString
{
uint32_t len;
const char *front;
FixedString() :
len(0),
front(nullptr)
{
}
FixedString(const char *first, uint32_t length) :
len(length),
front(first)
{
}
#if defined(_STRING_H)
FixedString(const char *str) :
len(strlen(str)),
front(str)
{
}
#endif
#if defined(KER_STRING_AVAILABLE)
FixedString(const ker::String &text) :
FixedString(text.str(), text.length()) {
}
explicit operator ker::String() {
return ker::String(reinterpret_cast<const uint8_t*>(front), len);
}
#endif
#if defined(_GLIBCXX_STRING)
FixedString(const std::string &text) :
FixedString(text.c_str(), text.size()) {
}
explicit operator std::string() {
return std::string(front, len);
}
#endif
};

View file

@ -0,0 +1,31 @@
#pragma once
#include <stddef.h>
#include <inttypes.h>
class ReferenceCounted
{
public:
static int32_t counter;
private:
size_t mReferenceCount;
protected:
ReferenceCounted() :
mReferenceCount(1)
{
counter++;
}
virtual ~ReferenceCounted() { counter--; }
public:
void aquire() {
this->mReferenceCount += 1;
}
void release() {
this->mReferenceCount -= 1;
if(this->mReferenceCount == 0) {
delete this;
}
}
};