#include "ehs/db/DbVarTmpl.h" #include "ehs/db/DbVar.h" namespace ehs { DbVarTmpl::~DbVarTmpl() { delete[] def; } DbVarTmpl::DbVarTmpl() : hashId(0), type(DbType::SINT_64), array(false), size(0), def(nullptr) { } DbVarTmpl::DbVarTmpl(Str_8 id, const DbType type, const UInt_64 size, const Byte* const def) : hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(true), size(size), def(new Byte[DbTypeToSize(type) * size]) { Util::Copy(this->def, def, DbTypeToSize(type) * size); } DbVarTmpl::DbVarTmpl(Str_8 id, const DbType type, const Byte* const def) : hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(false), size(1), def(new Byte[DbTypeToSize(type)]) { Util::Copy(this->def, def, DbTypeToSize(type)); } DbVarTmpl::DbVarTmpl(DbVarTmpl &&varTmpl) noexcept : hashId(varTmpl.hashId), id((Str_8&&)varTmpl.id), type(varTmpl.type), array(varTmpl.array), size(varTmpl.size), def(varTmpl.def) { varTmpl.hashId = 0; varTmpl.type = DbType::SINT_64; varTmpl.array = false; varTmpl.size = 0; varTmpl.def = nullptr; } DbVarTmpl::DbVarTmpl(const DbVarTmpl &varTmpl) : hashId(varTmpl.hashId), id(varTmpl.id), type(varTmpl.type), array(varTmpl.array), size(varTmpl.size), def(varTmpl.def) { } DbVarTmpl &DbVarTmpl::operator=(DbVarTmpl &&varTmpl) noexcept { if (this == &varTmpl) return *this; delete[] def; hashId = varTmpl.hashId; id = (Str_8&&)varTmpl.id; type = varTmpl.type; array = varTmpl.array; size = varTmpl.size; def = varTmpl.def; slaves = (Array&&)varTmpl.slaves; for (UInt_64 i = 0; i < slaves.Size(); ++i) slaves[i]->master = this; varTmpl.hashId = 0; varTmpl.type = DbType::SINT_64; varTmpl.array = false; varTmpl.size = 0; varTmpl.def = nullptr; return *this; } DbVarTmpl& DbVarTmpl::operator=(const DbVarTmpl& varTmpl) { if (this == &varTmpl) return *this; delete[] def; hashId = varTmpl.hashId; id = varTmpl.id; type = varTmpl.type; array = varTmpl.array; size = varTmpl.size; slaves = varTmpl.slaves; const UInt_64 byteSize = DbTypeToSize(varTmpl.type) * varTmpl.size; def = new Byte[byteSize]; Util::Copy(def, varTmpl.def, byteSize); return *this; } UInt_64 DbVarTmpl::GetHashId() const { return hashId; } void DbVarTmpl::SetId(Str_8 newId) { hashId = newId.Hash_64(); id = (Str_8&&)newId; } Str_8 DbVarTmpl::GetId() const { return id; } void DbVarTmpl::SetType(const DbType newType) { type = newType; } DbType DbVarTmpl::GetType() const { return type; } void DbVarTmpl::SetIsArray(const bool value) { array = value; } bool DbVarTmpl::IsArray() const { return array; } UInt_64 DbVarTmpl::GetSize() const { return size; } Byte* DbVarTmpl::GetDefault() const { return def; } void DbVarTmpl::UpdateSlave(const DbVar* const oldSlave, DbVar* const newSlave) const { for (UInt_64 i = 0; i < slaves.Size(); ++i) if (slaves[i] == oldSlave) slaves[i] = newSlave; } void DbVarTmpl::Serialize(Serializer &data) const { data.WriteStr(id); data.Write(type); data.Write(array); data.Write(size); data.WriteArray(def, DbTypeToSize(type) * size); } void DbVarTmpl::Deserialize(Serializer &data) { id = data.ReadStr(); hashId = id.Hash_64(); type = data.Read(); array = data.Read(); size = data.Read(); UInt_64 byteSize = 0; data.ReadArray(def, &byteSize); def = new Byte[byteSize]; data.ReadArray(def, &byteSize); } }