initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

31
lib/wType/CMakeLists.txt Normal file
View file

@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 2.8.12)
project(type)
set(HEADERS
bytearray.h
object.h
string.h
vocabulary.h
uint64.h
address.h
boolean.h
event.h
vector.h
blob.h
)
set(SOURCES
bytearray.cpp
object.cpp
string.cpp
vocabulary.cpp
uint64.cpp
address.cpp
boolean.cpp
event.cpp
vector.cpp
blob.cpp
)
add_library(wType ${HEADERS} ${SOURCES})
target_link_libraries(wType utils)

326
lib/wType/address.cpp Normal file
View file

@ -0,0 +1,326 @@
#include "address.h"
W::Address::Address():
Object(),
data(new List())
{
}
W::Address::Address(const W::Address::InitList& list):
Object(),
data(new List())
{
InitList::const_iterator itr = list.begin();
InitList::const_iterator end = list.end();
for (; itr != end; ++itr)
{
data->emplace_back(String(*itr));
}
}
W::Address::Address(const W::Address& original):
Object(),
data(new List(*original.data))
{
}
W::Address::~Address()
{
delete data;
}
W::Address& W::Address::operator=(const W::Address& original)
{
if (&original != this)
{
data->clear();
data->insert(data->end(), original.data->begin(), original.data->end());
}
return *this;
}
W::Object::size_type W::Address::length() const
{
return data->size();
}
W::Object::objectType W::Address::getType() const
{
return type;
}
W::Object::StdStr W::Address::toString() const
{
StdStr str;
List::const_iterator itr;
List::const_iterator beg = data->begin();
List::const_iterator end = data->end();
str += '[';
for (itr = beg; itr != end; ++itr)
{
if (itr != beg)
{
str += ", ";
}
str += itr->toString();
}
str += ']';
return str;
}
W::Object* W::Address::copy() const
{
return new Address(*this);
}
void W::Address::serialize(W::ByteArray& out) const
{
out.push32(length());
List::const_iterator itr;
List::const_iterator beg = data->begin();
List::const_iterator end = data->end();
for (itr = beg; itr != end; ++itr)
{
itr->serialize(out);
}
}
void W::Address::deserialize(W::ByteArray& in)
{
data->clear();
size_type length = in.pop32();
for (size_type i = 0; i != length; ++i)
{
data->emplace_back(String());
data->back().deserialize(in);
}
}
bool W::Address::operator<(const W::Address& other) const
{
return *data < *other.data;
}
bool W::Address::operator>(const W::Address& other) const
{
return *data > *other.data;
}
bool W::Address::operator==(const W::Address& other) const
{
return *data == *other.data;
}
bool W::Address::operator!=(const W::Address& other) const
{
return *data != *other.data;
}
bool W::Address::operator<=(const W::Address& other) const
{
return *data <= *other.data;
}
bool W::Address::operator>=(const W::Address& other) const
{
return *data >= *other.data;
}
bool W::Address::begins(const W::Address& other) const
{
if (other.length() > length())
{
return false;
}
List::const_iterator itr_o = other.data->begin();
List::const_iterator end_o = other.data->end();
List::const_iterator itr_i = data->begin();
while (itr_o != end_o)
{
if (*itr_o != *itr_i) {
return false;
}
++itr_o;
++itr_i;
}
return true;
}
bool W::Address::ends(const W::Address& other) const
{
if (other.length() > length())
{
return false;
}
List::const_reverse_iterator itr_o = other.data->rbegin();
List::const_reverse_iterator end_o = other.data->rend();
List::const_reverse_iterator itr_i = data->rbegin();
while (itr_o != end_o)
{
if (*itr_o != *itr_i) {
return false;
}
++itr_o;
++itr_i;
}
return true;
}
bool W::Address::contains(const W::Address& other, int position) const
{
if (other.length() > length() - position)
{
return false;
}
bool res = true;
List::const_iterator itr_o = other.data->begin();
List::const_iterator end_o = other.data->end();
List::const_iterator itr_i = data->begin();
for (int i = 0; i < position; ++i) {
++itr_i;
}
while (res == true && itr_o != end_o)
{
res = *itr_o == *itr_i;
++itr_o;
++itr_i;
}
return res;
}
W::Address& W::Address::operator+=(const W::Address& other)
{
data->insert(data->end(), other.data->begin(), other.data->end());
return *this;
}
W::Address& W::Address::operator+=(const W::String& other)
{
data->push_back(other);
return *this;
}
W::Address& W::Address::operator+=(const W::String::u16string& other)
{
String hop(other);
return operator+=(hop);
}
W::Address W::Address::operator>>(W::Object::size_type count) const
{
W::Address res;
if (count < length())
{
List::const_iterator itr = data->end();
for (size_type i = 0; i != count; ++i)
{
itr--;
}
List::const_iterator beg = data->begin();
res.data->insert(res.data->end(), beg, itr);
}
return res;
}
W::Address W::Address::operator<<(W::Object::size_type count) const
{
W::Address res;
if (count < length())
{
List::const_iterator itr = data->begin();
for (size_type i = 0; i != count; ++i)
{
itr++;
}
List::const_iterator end = data->end();
res.data->insert(res.data->end(), itr, end);
}
return res;
}
W::Address W::Address::operator+(const W::Address& other) const
{
W::Address res;
res += *this;
res += other;
return res;
}
W::Address & W::Address::operator+=(const Uint64& other)
{
data->push_back(String(other.toString()));
return *this;
}
W::Address W::Address::operator+(const Uint64& other) const
{
W::Address res;
res += *this;
res += other;
return res;
}
const W::String& W::Address::front() const
{
return data->front();
}
const W::String& W::Address::back() const
{
return data->back();
}
bool W::Address::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Address&>(other));
} else {
return false;
}
}
W::Object::size_type W::Address::size() const
{
size_type size = 4;
List::const_iterator itr = data->begin();
List::const_iterator end = data->end();
for (; itr != end; ++itr)
{
size += itr->size();
}
return size;
}

70
lib/wType/address.h Normal file
View file

@ -0,0 +1,70 @@
#ifndef ADDRESS_H
#define ADDRESS_H
#include "object.h"
#include "string.h"
#include "uint64.h"
#include <list>
#include <initializer_list>
namespace W
{
class Address:
public Object
{
typedef std::list<String> List;
typedef std::initializer_list<String::u16string> InitList;
public:
Address();
Address(const InitList& list);
Address(const Address& original);
~Address();
Address& operator=(const Address& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
bool operator==(const Object & other) const override;
bool operator<(const Address& other) const;
bool operator>(const Address& other) const;
bool operator<=(const Address& other) const;
bool operator>=(const Address& other) const;
bool operator==(const Address& other) const;
bool operator!=(const Address& other) const;
bool begins(const Address& other) const;
bool ends(const Address& other) const;
bool contains(const Address& other, int position) const;
Address& operator+=(const Address& other);
Address& operator+=(const String& other);
Address& operator+=(const Uint64& other);
Address& operator+=(const String::u16string& other);
Address operator>>(size_type count) const;
Address operator<<(size_type count) const;
Address operator+(const Address& other) const;
Address operator+(const Uint64& other) const;
static const objectType type = address;
const String& front() const;
const String& back() const;
private:
List *data;
};
}
#endif // ADDRESS_H

146
lib/wType/blob.cpp Normal file
View file

@ -0,0 +1,146 @@
#include "blob.h"
#include <arpa/inet.h>
#include <string>
W::Blob::Blob():
W::Object(),
hasData(false),
dataSize(0),
data(0),
qDataView()
{
}
W::Blob::Blob(uint32_t size, char* p_data):
W::Object(),
hasData(true),
dataSize(size),
data(p_data),
qDataView(QByteArray::fromRawData(p_data, size))
{
}
W::Blob::Blob(const W::Blob& original):
W::Object(),
hasData(original.data),
dataSize(original.dataSize),
data(0),
qDataView()
{
if (hasData) {
data = new char[dataSize];
std::copy(original.data, original.data + dataSize, data);
qDataView = QByteArray::fromRawData(data, dataSize);
}
}
W::Blob::~Blob()
{
if (hasData) {
delete [] data;
}
}
W::Blob & W::Blob::operator=(const W::Blob& original)
{
if (&original != this)
{
if (hasData) {
delete [] data;
qDataView = QByteArray();
}
hasData = original.hasData;
dataSize = original.dataSize;
if (hasData) {
data = new char[dataSize];
std::copy(original.data, original.data + dataSize, data);
qDataView = QByteArray::fromRawData(data, dataSize);
}
}
return *this;
}
W::Object * W::Blob::copy() const
{
return new W::Blob(*this);
}
W::Object::objectType W::Blob::getType() const
{
return type;
}
W::Object::size_type W::Blob::length() const
{
return dataSize;
}
void W::Blob::serialize(W::ByteArray& out) const
{
out.push32(length());
for (uint32_t i = 0; i < dataSize; ++i) {
out.push8(data[i]);
}
}
void W::Blob::deserialize(W::ByteArray& in)
{
if (hasData) {
delete [] data;
qDataView = QByteArray();
}
dataSize = in.pop32();
if (dataSize > 0) {
hasData = true;
data = new char[dataSize];
for (uint32_t i = 0; i < dataSize; ++i) {
data[i] = in.pop8();
}
qDataView = QByteArray::fromRawData(data, dataSize);
} else {
hasData = false;
}
}
W::Object::StdStr W::Blob::toString() const
{
return "Blob <" + std::to_string(dataSize) + ">";
}
bool W::Blob::operator==(const W::Blob& other) const
{
if (dataSize != other.dataSize) {
return false;
} else {
bool equals = true;
uint64_t i = 0;
while (equals && i < dataSize) {
equals = data[i] == other.data[i]; //TODO not sure about the c++ syntax if i'm comparing values or addresses this time
++i;
}
return equals;
}
}
bool W::Blob::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Blob&>(other));
} else {
return false;
}
}
W::Object::size_type W::Blob::size() const
{
return dataSize + 4;
}
const QByteArray & W::Blob::byteArray() const
{
return qDataView;
}

46
lib/wType/blob.h Normal file
View file

@ -0,0 +1,46 @@
#ifndef BLOB_H
#define BLOB_H
#include "object.h"
#include <QtCore/QByteArray>
namespace W
{
class Blob: public Object
{
public:
Blob();
Blob(uint32_t size, char* p_data);
Blob(const Blob& original);
~Blob();
Blob& operator=(const Blob& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
bool operator==(const Blob& other) const;
static const objectType type = blob;
const QByteArray& byteArray() const;
protected:
bool hasData;
uint32_t dataSize;
char* data;
QByteArray qDataView;
};
}
#endif // BLOB_H

146
lib/wType/boolean.cpp Normal file
View file

@ -0,0 +1,146 @@
#include "boolean.h"
W::Boolean::Boolean():
Object(),
data(false)
{
}
W::Boolean::Boolean(bool value):
Object(),
data(value)
{
}
W::Boolean::Boolean(const W::Boolean& original):
Object(),
data(original.data)
{
}
W::Boolean::~Boolean()
{
}
W::Boolean& W::Boolean::operator=(const W::Boolean& original)
{
data = original.data;
return *this;
}
W::Boolean& W::Boolean::operator=(bool original)
{
data = original;
return *this;
}
W::Object::StdStr W::Boolean::toString() const
{
StdStr str;
if (data)
{
str = "true";
}
else
{
str = "false";
}
return str;
}
W::Object::objectType W::Boolean::getType() const
{
return Boolean::type;
}
W::Object::size_type W::Boolean::length() const
{
return 1;
}
W::Object* W::Boolean::copy() const
{
return new Boolean(*this);
}
bool W::Boolean::operator>(const W::Boolean& other) const
{
return data > other.data;
}
bool W::Boolean::operator<(const W::Boolean& other) const
{
return data < other.data;
}
bool W::Boolean::operator==(const W::Boolean& other) const
{
return data == other.data;
}
bool W::Boolean::operator!=(const W::Boolean& other) const
{
return data != other.data;
}
bool W::Boolean::operator<=(const W::Boolean& other) const
{
return data <= other.data;
}
bool W::Boolean::operator>=(const W::Boolean& other) const
{
return data >= other.data;
}
void W::Boolean::serialize(W::ByteArray& out) const
{
uint8_t val;
if (data)
{
val = 253;
}
else
{
val = 0;
}
out.push8(val);
}
void W::Boolean::deserialize(W::ByteArray& in)
{
uint8_t val = in.pop8();
if (val == 253)
{
data = true;
}
else
{
data = false;
}
}
W::Boolean::operator bool() const
{
return data;
}
bool W::Boolean::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Boolean&>(other));
} else {
return false;
}
}
W::Object::size_type W::Boolean::size() const
{
return 1;
}

49
lib/wType/boolean.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef BOOLEAN_H
#define BOOLEAN_H
#include "object.h"
namespace W
{
class Boolean:
public Object
{
public:
Boolean();
explicit Boolean(bool value);
Boolean(const Boolean& original);
~Boolean();
Boolean& operator=(const Boolean& original);
Boolean& operator=(bool original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
bool operator<(const Boolean& other) const;
bool operator>(const Boolean& other) const;
bool operator<=(const Boolean& other) const;
bool operator>=(const Boolean& other) const;
bool operator==(const Boolean& other) const;
bool operator!=(const Boolean& other) const;
static const objectType type = boolean;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
operator bool() const;
private:
bool data;
};
}
#endif // BOOLEAN_H

167
lib/wType/bytearray.cpp Normal file
View file

@ -0,0 +1,167 @@
#include "bytearray.h"
#include <arpa/inet.h>
W::ByteArray::ByteArray(size_type size):
data(new Container(size)),
shiftBegin(0),
shiftEnd(0),
referenceMode(false)
{
}
W::ByteArray::ByteArray(const W::ByteArray& original):
data(new Container(*(original.data))),
shiftBegin(original.shiftBegin),
shiftEnd(original.shiftEnd),
referenceMode(original.referenceMode)
{
}
W::ByteArray::~ByteArray()
{
delete data;
}
W::ByteArray& W::ByteArray::operator=(const W::ByteArray& original)
{
if (&original != this)
{
delete data;
data = new Container(*(original.data));
shiftBegin = original.shiftBegin;
shiftEnd = original.shiftEnd;
referenceMode = original.referenceMode;
}
return *this;
}
W::ByteArray::size_type W::ByteArray::size() const
{
return shiftEnd - shiftBegin;
}
void W::ByteArray::push8(uint8_t integer)
{
if (referenceMode) {
//TODO not sure
}
data->at(shiftEnd) = integer;
++shiftEnd;
}
void W::ByteArray::push16(uint16_t integer)
{
uint16_t n16char = htons(integer);
push8(((uint8_t*)&n16char)[0]);
push8(((uint8_t*)&n16char)[1]);
}
void W::ByteArray::push32(uint32_t integer)
{
uint32_t converted = htonl(integer);
for (uint8_t i(0); i < 4; ++i)
{
push8(((uint8_t*)&converted)[i]);
}
}
void W::ByteArray::push64(uint64_t integer)
{
uint32_t h = integer >> 32;
uint32_t l = integer & 0x00000000FFFFFFFF;
uint32_t convertedh = htonl(h);
for (uint8_t i(0); i < 4; ++i)
{
push8(((uint8_t*)&convertedh)[i]);
}
uint32_t convertedl = htonl(l);
for (uint8_t i(0); i < 4; ++i)
{
push8(((uint8_t*)&convertedl)[i]);
}
}
uint8_t W::ByteArray::pop8()
{
uint8_t byte = data->at(shiftBegin);
++shiftBegin;
return byte;
}
uint16_t W::ByteArray::pop16()
{
uint8_t h = pop8();
uint8_t l = pop8();
uint8_t src[2] = {h, l};
return ntohs(*((uint16_t*) src));
}
uint32_t W::ByteArray::pop32()
{
uint8_t src[4]; //TODO optimize?
for (uint8_t i(0); i < 4; ++i)
{
src[i] = pop8();
}
return ntohl(*((uint32_t*) src));
}
uint64_t W::ByteArray::pop64()
{
uint8_t srch[4] = {0,0,0,0};
uint8_t srcl[4] = {0,0,0,0};
for (uint8_t i(0); i < 4; ++i)
{
srch[i] = pop8();
}
for (uint8_t i(0); i < 4; ++i)
{
srcl[i] = pop8();
}
uint64_t h = ntohl(*((uint32_t*) srch));
uint32_t l = ntohl(*((uint32_t*) srcl));
return (h << 32) | l;
}
char * W::ByteArray::getData()
{
return data->data(); //TODO not actually sure about this, may be need to check first about shifts and refence?
}
W::ByteArray::size_type W::ByteArray::maxSize() const
{
return data->size();
}
bool W::ByteArray::filled() const
{
return shiftEnd == data->size();
}
W::ByteArray::size_type W::ByteArray::fill(const char* data, W::ByteArray::size_type size, W::ByteArray::size_type shift)
{
size_type i = shift;
while (i < size && !filled()) {
push8(data[i]);
++i;
}
return i;
}

49
lib/wType/bytearray.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef BYTEARRAY_H
#define BYTEARRAY_H
#include <vector>
#include <stdint.h>
namespace W
{
class ByteArray
{
friend class Socket;
typedef std::vector<char> Container;
public:
typedef uint32_t size_type;
ByteArray(size_type size);
ByteArray(const ByteArray& original);
~ByteArray();
ByteArray& operator=(const ByteArray& original);
void push8(uint8_t integer);
void push16(uint16_t integer);
void push32(uint32_t integer);
void push64(uint64_t integer);
uint8_t pop8();
uint16_t pop16();
uint32_t pop32();
uint64_t pop64();
size_type size() const;
size_type maxSize() const;
bool filled() const;
size_type fill(const char* data, size_type size, size_type shift = 0);
char* getData();
private:
Container *data;
size_type shiftBegin;
size_type shiftEnd;
bool referenceMode;
};
}
#endif // BYTEARRAY_H

179
lib/wType/event.cpp Normal file
View file

@ -0,0 +1,179 @@
#include "event.h"
W::Event::Event():
Object(),
system(false),
destination(),
sender(0),
data(0)
{
}
W::Event::Event(const W::Address& p_addr, const W::Object& p_data, bool p_system):
Object(),
system(p_system),
destination(p_addr),
sender(0),
data(p_data.copy())
{
}
W::Event::Event(const W::Address& p_addr, W::Object* p_data, bool p_system):
Object(),
system(p_system),
destination(p_addr),
sender(0),
data(p_data)
{
}
W::Event::Event(const W::Event& original):
Object(),
system(original.system),
destination(original.destination),
sender(original.sender),
data(original.data->copy())
{
}
W::Event::~Event()
{
delete data;
}
W::Event& W::Event::operator=(const W::Event& original)
{
if (&original != this)
{
delete data;
system = original.system;
destination = original.destination;
sender = original.sender;
data = original.data->copy();
}
return *this;
}
W::Object* W::Event::copy() const
{
return new Event(*this);
}
W::Object::objectType W::Event::getType() const
{
return Event::type;
}
W::Object::size_type W::Event::length() const
{
size_type my_size = 2;
my_size += destination.length();
my_size += data->length();
return my_size;
}
W::Object::StdStr W::Event::toString() const
{
StdStr result;
result += "{";
result += "system: ";
result += system.toString();
result += ", desitnation: ";
result += destination.toString();
result += ", sender: ";
result += sender.toString();
result += ", data: ";
result += data->toString();
result += "}";
return result;
}
void W::Event::serialize(W::ByteArray& out) const
{
system.serialize(out);
if (!system)
{
destination.serialize(out);
sender.serialize(out);
}
out.push8(data->getType());
data->serialize(out);
}
void W::Event::deserialize(W::ByteArray& in)
{
system.deserialize(in);
if (!system)
{
destination.deserialize(in);
sender.deserialize(in);
}
delete data;
data = Object::fromByteArray(in);
}
bool W::Event::isSystem() const
{
return system;
}
const W::Address& W::Event::getDestination() const
{
return destination;
}
uint64_t W::Event::getSenderId() const
{
return sender;
}
const W::Object& W::Event::getData() const
{
return *data;
}
void W::Event::setSenderId(const W::Uint64& senderId)
{
sender = senderId;
}
void W::Event::setSenderId(uint64_t senderId)
{
sender = Uint64(senderId);
}
bool W::Event::operator==(const W::Object& other) const
{
if (sameType(other)) {
const W::Event& oev = static_cast<const W::Event&>(other);
return destination == oev.destination && system == oev.system && sender == oev.sender && *data == *(oev.data);
} else {
return false;
}
}
W::Object::size_type W::Event::size() const
{
size_type sz = system.size() + data->size() + 1;
if (!system)
{
sz += destination.size() + sender.size();
}
return sz;
}

53
lib/wType/event.h Normal file
View file

@ -0,0 +1,53 @@
#ifndef EVENT_H
#define EVENT_H
#include "object.h"
#include "address.h"
#include "uint64.h"
#include "boolean.h"
namespace W
{
class Event:
public Object
{
public:
Event();
Event(const Address& p_addr, const Object& p_data, bool p_system = false);
Event(const Address& p_addr, Object* p_data, bool p_system = false);
Event(const Event& original);
~Event();
Event& operator=(const Event& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
static const objectType type = event;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
bool isSystem() const;
const Address& getDestination() const;
uint64_t getSenderId() const;
const Object& getData() const;
void setSenderId(const Uint64& senderId);
void setSenderId(uint64_t senderId);
private:
Boolean system;
Address destination;
Uint64 sender;
Object* data;
};
}
#endif // EVENT_H

106
lib/wType/object.cpp Normal file
View file

@ -0,0 +1,106 @@
#include "object.h"
#include "string.h"
#include "vocabulary.h"
#include "uint64.h"
#include "address.h"
#include "boolean.h"
#include "event.h"
#include "vector.h"
#include "blob.h"
#include <arpa/inet.h>
//#include <stdint.h>
W::Object::Object()
{
}
W::Object::~Object()
{
}
W::Object* W::Object::fromByteArray(ByteArray& in)
{
uint32_t type = in.pop8();
Object *answer;
switch (type)
{
case string:
answer = new String();
break;
case vocabulary:
answer = new Vocabulary();
break;
case uint64:
answer = new Uint64();
break;
case address:
answer = new Address();
break;
case boolean:
answer = new Boolean();
break;
case event:
answer = new Event();
break;
case vector:
answer = new Vector();
break;
case blob:
answer = new Blob();
break;
}
answer->deserialize(in);
return answer;
}
bool W::Object::sameType(const W::Object& other) const
{
return getType() == other.getType();
}
bool W::Object::operator!=(const W::Object& other) const
{
return !operator==(other);
}
W::Object::StdStr W::Object::getTypeName(W::Object::objectType type)
{
switch (type) {
case string:
return "String";
case vocabulary:
return "Vocabulary";
case uint64:
return "Uint64";
case address:
return "Address";
case boolean:
return "Boolean";
case event:
return "Event";
case vector:
return "Vector";
case blob:
return "Blob";
}
}

55
lib/wType/object.h Normal file
View file

@ -0,0 +1,55 @@
#ifndef WOBJCET_H
#define WOBJCET_H
#include <string>
#include <stdint.h>
#include "bytearray.h"
namespace W {
class Object
{
friend class ByteArray;
public:
enum objectType
{
string,
vocabulary,
uint64,
address,
boolean,
event,
vector,
blob
};
typedef uint64_t size_type;
typedef std::string StdStr;
Object();
virtual ~Object();
virtual StdStr toString() const = 0;
virtual Object* copy() const = 0;
virtual size_type length() const = 0; //object specific size - like amount of subelements or amount of characters
virtual size_type size() const = 0; //object size in bytes
virtual bool operator==(const Object& other) const = 0;
virtual bool operator!=(const Object& other) const; //TODO may be make it also pure virtual?
virtual objectType getType() const = 0;
virtual void serialize(ByteArray& out) const = 0;
virtual void deserialize(ByteArray& in) = 0;
static StdStr getTypeName(objectType type);
protected:
bool sameType(const Object& other) const;
public:
static Object* fromByteArray(ByteArray& in);
};
}
#endif // WOBJCET_H

208
lib/wType/string.cpp Normal file
View file

@ -0,0 +1,208 @@
#include "string.h"
#include <codecvt>
#include <locale>
W::String::String():
Object(),
data(new u16string())
{
}
W::String::String(const u16string& p_data):
Object(),
data(new u16string(p_data))
{
}
W::String::String(const char16_t* p_data):
Object(),
data(new u16string(p_data))
{
}
W::String::String(const W::String& original):
Object(),
data(new u16string(*original.data))
{
}
W::String::String(const StdStr p_data):
Object(),
data(0)
{
std::wstring_convert<std::codecvt_utf8<char16_t>,char16_t> convert;
std::u16string u16 = convert.from_bytes(p_data);
data = new u16string(u16);
}
W::String::~String()
{
delete data;
}
W::String& W::String::operator=(const W::String& original)
{
if (&original != this)
{
delete data;
data = new u16string(*(original.data));
}
return *this;
}
W::Object* W::String::copy() const
{
return new String(*this);
}
W::Object::StdStr W::String::toString() const
{
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convertor;
StdStr result = convertor.to_bytes(*data);
return result;
}
W::Object::size_type W::String::length() const
{
return data->size();
}
void W::String::serialize(W::ByteArray& out) const
{
out.push32(length());;
u16string::const_iterator itr = data->begin();
u16string::const_iterator end = data->end();
for(; itr != end; ++itr)
{
out.push16(*itr);
}
}
void W::String::deserialize(W::ByteArray& in)
{
data->clear();
size_type length = in.pop32();
for (size_type i = 0; i != length; ++i)
{
data->push_back(in.pop16());
}
}
W::Object::objectType W::String::getType() const
{
return String::type;
}
bool W::String::operator<(const W::String& other) const
{
return (*data) < *(other.data);
}
bool W::String::operator>(const W::String& other) const
{
return (*data) > *(other.data);
}
bool W::String::operator<=(const W::String& other) const
{
return (*data) <= *(other.data);
}
bool W::String::operator>=(const W::String& other) const
{
return (*data) >= *(other.data);
}
bool W::String::operator!=(const W::String& other) const
{
return (*data) != *(other.data);
}
bool W::String::operator==(const W::String& other) const
{
return (*data) == *(other.data);
}
bool W::String::operator==(const char16_t* other) const
{
return *data == other;
}
bool W::String::operator!=(const char16_t* other) const
{
return *data != other;
}
W::String::operator u16string() const
{
return *data;
}
W::String & W::String::operator+=(int number)
{
StdStr str = std::to_string(number);;
std::wstring_convert<std::codecvt_utf8<char16_t>,char16_t> convert;
std::u16string u16 = convert.from_bytes(str);
(*data) += u16;
return *this;
}
W::String W::String::operator+(const W::String& other) const
{
return W::String((*data) + *(other.data));
}
W::String & W::String::operator+=(const W::String& other)
{
(*data) += *(other.data);
return *this;
}
W::String::size_type W::String::findFirstOf(const W::String& str) const
{
return data->find_first_of(*(str.data));
}
W::String::size_type W::String::findLastOf(const W::String& str) const
{
return data->find_last_of(*(str.data));
}
W::String W::String::substr(size_type start, size_type length) const
{
return W::String(data->substr(start, length));
}
uint64_t W::String::toUint64() const
{
return std::stoull(toString());
}
bool W::String::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::String&>(other));
} else {
return false;
}
}
W::Object::size_type W::String::size() const
{
return data->size() * 2 + 4;
}

65
lib/wType/string.h Normal file
View file

@ -0,0 +1,65 @@
#ifndef W_STRING_H
#define W_STRING_H
#include "object.h"
namespace W{
class String : public Object
{
public:
typedef std::u16string u16string;
String();
explicit String(const u16string& p_data);
explicit String(const char16_t* p_data);
explicit String(const StdStr p_data);
String(const String& original);
~String();
String& operator=(const String& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
bool operator<(const String& other) const;
bool operator>(const String& other) const;
bool operator<=(const String& other) const;
bool operator>=(const String& other) const;
bool operator==(const String& other) const;
bool operator!=(const String& other) const;
bool operator==(const char16_t* other) const;
bool operator!=(const char16_t* other) const;
static const objectType type = string;
operator u16string() const;
uint64_t toUint64() const;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
String& operator+=(int);
String& operator+=(const String& other);
String operator+(const String& other) const;
size_type findLastOf(const W::String& str) const;
size_type findFirstOf(const W::String& str) const;
String substr(size_type start, size_type length = u16string::npos) const;
private:
u16string* data;
};
}
#endif // W_STRING_H

114
lib/wType/uint64.cpp Normal file
View file

@ -0,0 +1,114 @@
#include "uint64.h"
#include <string>
W::Uint64::Uint64():
Object(),
data(0)
{
}
W::Uint64::Uint64(const uint64_t& original):
Object(),
data(original)
{
}
W::Uint64::Uint64(const W::Uint64& original):
Object(),
data(original.data)
{
}
W::Uint64::~Uint64()
{
}
W::Uint64& W::Uint64::operator=(const W::Uint64& original)
{
data = original.data;
return *this;
}
W::Object::StdStr W::Uint64::toString() const
{
return std::to_string(data);
}
W::Object* W::Uint64::copy() const
{
return new W::Uint64(*this);
}
W::Object::size_type W::Uint64::length() const
{
return 1;
}
W::Object::size_type W::Uint64::size() const
{
return 8;
}
W::Object::objectType W::Uint64::getType() const
{
return type;
}
bool W::Uint64::operator<(const W::Uint64& other) const
{
return data < other.data;
}
bool W::Uint64::operator>(const W::Uint64& other) const
{
return data > other.data;
}
bool W::Uint64::operator==(const W::Uint64& other) const
{
return data == other.data;
}
bool W::Uint64::operator!=(const W::Uint64& other) const
{
return data != other.data;
}
bool W::Uint64::operator<=(const W::Uint64& other) const
{
return data <= other.data;
}
bool W::Uint64::operator>=(const W::Uint64& other) const
{
return data >= other.data;
}
void W::Uint64::serialize(W::ByteArray& out) const
{
out.push64(data);
}
void W::Uint64::deserialize(W::ByteArray& in)
{
data = in.pop64();
}
W::Uint64::operator uint64_t() const
{
return data;
}
bool W::Uint64::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Uint64&>(other));
} else {
return false;
}
}

49
lib/wType/uint64.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef UINT64_H
#define UINT64_H
#include <stdint.h>
#include "object.h"
namespace W
{
class Uint64:
public Object
{
public:
Uint64();
explicit Uint64(const uint64_t& original);
Uint64(const Uint64& original);
~Uint64();
Uint64& operator=(const Uint64& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
bool operator<(const Uint64& other) const;
bool operator>(const Uint64& other) const;
bool operator<=(const Uint64& other) const;
bool operator>=(const Uint64& other) const;
bool operator==(const Uint64& other) const;
bool operator!=(const Uint64& other) const;
static const objectType type = uint64;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
operator uint64_t() const;
private:
uint64_t data;
};
}
#endif // UINT64_H

191
lib/wType/vector.cpp Normal file
View file

@ -0,0 +1,191 @@
#include "vector.h"
#include <string>
W::Vector::Vector():
Object(),
data(new Vec())
{
}
W::Vector::Vector(const W::Vector& original):
Object(),
data(new Vec())
{
data->reserve(original.data->capacity());
Vec::const_iterator itr = original.data->begin();
Vec::const_iterator end = original.data->end();
for (; itr != end; ++itr)
{
data->push_back((*itr)->copy());
}
}
W::Vector::~Vector()
{
clear();
delete data;
}
W::Vector& W::Vector::operator=(const W::Vector& original)
{
if (&original != this)
{
clear();
Vec::const_iterator itr = original.data->begin();
Vec::const_iterator end = original.data->end();
for (; itr != end; ++itr)
{
data->push_back((*itr)->copy());
}
}
return *this;
}
void W::Vector::clear()
{
Vec::iterator itr = data->begin();
Vec::iterator end = data->end();
for (; itr != end; ++itr)
{
delete (*itr);
}
data->clear();
}
W::Object::StdStr W::Vector::toString() const
{
StdStr result;
Vec::const_iterator itr;
Vec::const_iterator beg = data->begin();
Vec::const_iterator end = data->end();
result += "[";
for (itr = beg; itr != end; ++itr)
{
if (itr != beg)
{
result += ", ";
}
result += (*itr)->toString();
}
result += "]";
return result;
}
W::Object* W::Vector::copy() const
{
return new Vector(*this);
}
W::Object::size_type W::Vector::length() const
{
return data->size();
}
W::Object::size_type W::Vector::size() const
{
size_type size = 4;
Vec::const_iterator itr = data->begin();
Vec::const_iterator end = data->end();
for (; itr != end; ++itr)
{
size += (*itr)->size() + 1;
}
return size;
}
W::Object::objectType W::Vector::getType() const
{
return Vector::type;
}
void W::Vector::serialize(W::ByteArray& out) const
{
out.push32(length());
Vec::const_iterator itr = data->begin();
Vec::const_iterator end = data->end();
for (; itr != end; ++itr)
{
out.push8((*itr)->getType());
(*itr)->serialize(out);
}
}
void W::Vector::deserialize(W::ByteArray& in)
{
data->clear();
size_type length = in.pop32();
for (size_type i = 0; i != length; ++i)
{
Object* value = Object::fromByteArray(in);
data->push_back(value);
}
}
void W::Vector::push(const W::Object& value)
{
data->push_back(value.copy());
}
void W::Vector::push(W::Object* value)
{
data->push_back(value);
}
const W::Object& W::Vector::at(uint64_t index) const
{
if (index >= length()) {
throw NoElement(index);
}
return *(data->at(index));
}
W::Vector::NoElement::NoElement(uint64_t index):
Exception(),
key(index)
{
}
std::string W::Vector::NoElement::getMessage() const
{
std::string msg("No element has been found by index: ");
msg += std::to_string(key);
return msg;
}
bool W::Vector::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Vector&>(other));
} else {
return false;
}
}
bool W::Vector::operator==(const W::Vector& other) const
{
bool equals = data->size() == other.data->size();
int i = 0;
while (equals && i != data->size()) {
equals = data->at(i) == other.data->at(i);
++i;
}
return equals;
}

60
lib/wType/vector.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef VECTOR_H
#define VECTOR_H
#include "object.h"
#include <utils/exception.h>
#include <vector>
#include <stdint.h>
namespace W
{
class Vector:
public Object
{
typedef std::vector<Object*> Vec;
public:
Vector();
Vector(const Vector& original);
~Vector();
Vector& operator=(const Vector& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
bool operator==(const W::Vector & other) const;
void clear();
void push(const Object& value);
void push(Object* value);
const Object& at(uint64_t index) const;
static const objectType type = vector;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
class NoElement:
Utils::Exception
{
public:
NoElement(uint64_t index);
std::string getMessage() const;
private:
uint64_t key;
};
private:
Vec *data;
};
}
#endif // VECTOR_H

296
lib/wType/vocabulary.cpp Normal file
View file

@ -0,0 +1,296 @@
#include "vocabulary.h"
W::Vocabulary::Vocabulary():
Object(),
data(new Map())
{
}
W::Vocabulary::Vocabulary(const W::Vocabulary& original):
Object(),
data(new Map())
{
Map::const_iterator itr = original.data->begin();
Map::const_iterator end = original.data->end();
for (; itr != end; ++itr)
{
(*data)[itr->first] = itr->second->copy();
}
}
W::Vocabulary::~Vocabulary()
{
clear();
delete data;
}
W::Vocabulary& W::Vocabulary::operator=(const W::Vocabulary& original)
{
if (&original != this)
{
clear();
Map::const_iterator itr = original.data->begin();
Map::const_iterator end = original.data->end();
for (; itr != end; ++itr)
{
(*data)[itr->first] = itr->second->copy();
}
}
return *this;
}
void W::Vocabulary::clear()
{
Map::iterator itr = data->begin();
Map::iterator end = data->end();
for (; itr != end; ++itr)
{
delete itr->second;
}
data->clear();
}
W::Object::StdStr W::Vocabulary::toString() const
{
StdStr result;
Map::const_iterator itr;
Map::const_iterator beg = data->begin();
Map::const_iterator end = data->end();
result += "{";
for (itr = beg; itr != end; ++itr)
{
if (itr != beg)
{
result += ", ";
}
result += itr->first.toString();
result += ": ";
result += itr->second->toString();
}
result += "}";
return result;
}
W::Object* W::Vocabulary::copy() const
{
return new Vocabulary(*this);
}
W::Object::size_type W::Vocabulary::length() const
{
return data->size();
}
W::Object::size_type W::Vocabulary::size() const
{
size_type size = 4;
Map::const_iterator itr = data->begin();
Map::const_iterator end = data->end();
for (; itr != end; ++itr)
{
size += itr->first.size();
size += itr->second->size() + 1;
}
return size;
}
W::Object::objectType W::Vocabulary::getType() const
{
return Vocabulary::type;
}
void W::Vocabulary::serialize(W::ByteArray& out) const
{
out.push32(length());
Map::const_iterator itr = data->begin();
Map::const_iterator end = data->end();
for (; itr != end; ++itr)
{
itr->first.serialize(out);
out.push8(itr->second->getType());
itr->second->serialize(out);
}
}
void W::Vocabulary::deserialize(W::ByteArray& in)
{
data->clear();
size_type length = in.pop32();
for (size_type i = 0; i != length; ++i)
{
String key;
key.deserialize(in);
Object* value = Object::fromByteArray(in);
(*data)[key] = value;
}
}
void W::Vocabulary::insert(const W::String::u16string& key, const W::Object& value)
{
String strKey(key);
insert(strKey, value);
}
void W::Vocabulary::insert(const W::String& key, const W::Object& value)
{
Map::const_iterator itr = data->find(key);
if (itr != data->end())
{
delete itr->second;
}
(*data)[key] = value.copy();
}
void W::Vocabulary::insert(const String::u16string& key, W::Object* value)
{
String strKey(key);
insert(strKey, value);
}
void W::Vocabulary::insert(const W::String& key, W::Object* value)
{
Map::const_iterator itr = data->find(key);
if (itr != data->end())
{
delete itr->second;
}
(*data)[key] = value;
}
const W::Object& W::Vocabulary::at(const String::u16string& key) const
{
String strKey(key);
return at(strKey);
}
const W::Object& W::Vocabulary::at(const W::String& key) const
{
Map::const_iterator itr = data->find(key);
if (itr == data->end())
{
throw NoElement(key);
}
return *(itr->second);
}
bool W::Vocabulary::has(const String::u16string& key) const
{
String strKey(key);
return has(strKey);
}
bool W::Vocabulary::has(const W::String& key) const
{
Map::const_iterator itr = data->find(key);
return itr != data->end();
}
void W::Vocabulary::erase(const String::u16string& key)
{
String strKey(key);
erase(strKey);
}
void W::Vocabulary::erase(const W::String& key)
{
Map::const_iterator itr = data->find(key);
if (itr == data->end())
{
throw NoElement(key);
}
data->erase(itr);
}
W::Vector W::Vocabulary::keys() const
{
Vector keys;
Map::iterator itr = data->begin();
Map::iterator end = data->end();
for (; itr != end; ++itr)
{
keys.push(itr->first);
}
return keys;
}
W::Vocabulary::NoElement::NoElement(const W::String& p_key):
Exception(),
key(p_key)
{
}
std::string W::Vocabulary::NoElement::getMessage() const
{
std::string msg("No element has been found by key: ");
msg += key.toString();
return msg;
}
bool W::Vocabulary::operator==(const W::Object& other) const
{
if (sameType(other)) {
return operator==(static_cast<const W::Vocabulary&>(other));
} else {
return false;
}
}
bool W::Vocabulary::operator==(const W::Vocabulary& other) const
{
bool equal = data->size() == other.data->size();
Map::const_iterator mItr = data->begin();
Map::const_iterator oItr = other.data->begin();
while (equal && mItr != data->end()) {
equal = (mItr->first == oItr->first) && (mItr->second == oItr->second);
++mItr;
++oItr;
}
return equal;
}
W::Vocabulary * W::Vocabulary::extend(const W::Vocabulary& first, const W::Vocabulary& second)
{
W::Vocabulary* vc = static_cast<W::Vocabulary*>(first.copy());
Map::const_iterator itr = second.data->begin();
Map::const_iterator end = second.data->end();
for (; itr != end; ++itr) {
vc->data->operator[](itr->first) = itr->second;
}
return vc;
}

72
lib/wType/vocabulary.h Normal file
View file

@ -0,0 +1,72 @@
#ifndef VOCABULARY_H
#define VOCABULARY_H
#include "object.h"
#include "string.h"
#include "vector.h"
#include <utils/exception.h>
#include <map>
namespace W
{
class Vocabulary:
public Object
{
typedef std::map<String, Object*> Map;
public:
Vocabulary();
Vocabulary(const Vocabulary& original);
~Vocabulary();
Vocabulary& operator=(const Vocabulary& original);
StdStr toString() const override;
Object* copy() const override;
size_type length() const override;
size_type size() const override;
objectType getType() const override;
bool operator==(const W::Object & other) const override;
bool operator==(const W::Vocabulary& other) const;
void clear();
void insert(const String::u16string& key, const Object& value);
void insert(const String& key, const Object& value);
void insert(const String::u16string& key, Object* value);
void insert(const String& key, Object* value);
const Object& at(const String::u16string& key) const;
const Object& at(const String& key) const;
bool has(const String::u16string& key) const;
bool has(const String& key) const;
void erase(const String::u16string& key);
void erase(const String& key);
Vector keys() const;
static const objectType type = vocabulary;
void serialize(ByteArray& out) const override;
void deserialize(ByteArray& in) override;
static W::Vocabulary* extend(const W::Vocabulary& first, const W::Vocabulary& second);
class NoElement:
Utils::Exception
{
public:
NoElement(const String& p_key);
std::string getMessage() const;
private:
String key;
};
private:
Map *data;
};
}
#endif // VOCABULARY_H