Simplified the database.

This commit is contained in:
2024-04-10 18:26:20 -07:00
parent 72347ea9a2
commit 3196c5021e
9 changed files with 131 additions and 279 deletions

View File

@@ -42,11 +42,7 @@ namespace ehs
bool HasVariable(const Str_8& id) const;
bool CreateVariable(Str_8 id, DbType type, const Byte* defaultValue);
bool CreateVariable(Str_8 id, DbType type, UInt_64 size, const Byte* defaultValue);
bool CreateVariable(Str_8 id, DbType type);
bool CreateVariable(DbVarTmpl var);
DbObject *CreateObject();

View File

@@ -1,61 +0,0 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
enum class DbType : UInt_8
{
SINT_64,
UINT_64,
SINT_32,
UINT_32,
SINT_16,
UINT_16,
SINT_8,
UINT_8,
FLOAT,
DOUBLE,
BOOLEAN,
CHAR_32,
CHAR_16,
CHAR_8
};
constexpr UInt_8 DbTypeToSize(const DbType type)
{
switch (type)
{
case DbType::SINT_64:
return 8;
case DbType::UINT_64:
return 8;
case DbType::SINT_32:
return 4;
case DbType::UINT_32:
return 4;
case DbType::SINT_16:
return 2;
case DbType::UINT_16:
return 2;
case DbType::SINT_8:
return 1;
case DbType::UINT_8:
return 1;
case DbType::FLOAT:
return 4;
case DbType::DOUBLE:
return 8;
case DbType::BOOLEAN:
return 1;
case DbType::CHAR_32:
return 4;
case DbType::CHAR_16:
return 2;
case DbType::CHAR_8:
return 1;
default:
return 0;
}
}
}

View File

@@ -1,7 +1,6 @@
#pragma once
#include "ehs/Types.h"
#include "DbType.h"
#include "ehs/Serializer.h"
namespace ehs
@@ -13,20 +12,18 @@ namespace ehs
{
private:
friend class DbObject;
friend class DbVarTmpl;
UInt_64 hashId;
DbObject *parent;
DbVarTmpl *master;
Byte *value;
UInt_64 size;
Byte *data;
public:
~DbVar();
DbVar();
DbVar(UInt_64 hashId, DbVarTmpl *master);
DbVar(UInt_64 hashId, const DbVarTmpl *master);
DbVar(DbVar &&var) noexcept;
@@ -40,14 +37,66 @@ namespace ehs
UInt_64 GetHashId() const;
template<typename T>
void SetValueArray(const T* const newValue, const UInt_64 newSize)
{
size = sizeof(T) * newSize;
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T = Char_8>
void SetValueStr(const T * const newValue)
{
size = sizeof(T) * Str<Char_8, UInt_64>::Len(newValue);
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T = Char_8, typename I = UInt_64>
void SetValueStr(const Str<T, I>& newValue)
{
size = newValue.Size(true);
value = new Byte[size];
Util::Copy(value, &newValue[0], size);
}
template<typename T>
void SetValue(const Byte* newValue)
{
size = sizeof(T);
value = new Byte[size];
Util::Copy(value, newValue, size);
}
template<typename T>
T* GetValueArray() const
{
return (T*)value;
}
template<typename T = Char_8, typename I = UInt_64>
Str<T, I> GetValueStr() const
{
return {(T*)value, size / sizeof(T)};
}
template<typename T>
T GetValue() const
{
return *(T*)value;
}
UInt_64 GetSize() const;
void SetData(UInt_64 newSize, const Byte* newData);
void SetData(const Byte* newData);
Byte* GetData() const;
private:
void Serialize(Serializer<UInt_64> &data) const;

View File

@@ -1,6 +1,5 @@
#pragma once
#include "DbType.h"
#include "ehs/EHS.h"
#include "ehs/Serializer.h"
#include "ehs/Str.h"
@@ -13,26 +12,32 @@ namespace ehs
{
private:
friend class DbTable;
friend class DbVar;
UInt_64 hashId;
Str_8 id;
DbType type;
bool array;
UInt_64 size;
Byte* def;
Array<DbVar*> slaves;
UInt_64 size;
public:
~DbVarTmpl();
DbVarTmpl();
DbVarTmpl(Str_8 id, DbType type, UInt_64 size, const Byte* def);
template<typename T>
DbVarTmpl(Str_8 id, const T* const def, UInt_64 size)
: hashId(id.Hash_64()), id((Str_8&&)id), def(new Byte[sizeof(T) * size]), size(sizeof(T) * size)
{
Util::Copy(this->def, def, this->size);
}
DbVarTmpl(Str_8 id, DbType type, const Byte* def);
template<typename T>
DbVarTmpl(Str_8 id, const T* const def)
: hashId(id.Hash_64()), id((Str_8&&)id), def(new Byte[sizeof(T)]), size(sizeof(T))
{
Util::Copy(this->def, def, this->size);
}
DbVarTmpl(Str_8 id, DbType type);
DbVarTmpl(Str_8 id);
DbVarTmpl(DbVarTmpl&& varTmpl) noexcept;
@@ -42,27 +47,29 @@ namespace ehs
DbVarTmpl& operator=(const DbVarTmpl& varTmpl);
operator Byte *() const;
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
void SetType(DbType newType);
template<typename T>
T* GetDefaultArray() const
{
return (T*)def;
}
DbType GetType() const;
void SetIsArray(bool value);
bool IsArray() const;
template<typename T>
T GetDefault() const
{
return *(T*)def;
}
UInt_64 GetSize() const;
Byte* GetDefault() const;
private:
void UpdateSlave(const DbVar *oldSlave, DbVar *newSlave) const;
void Serialize(Serializer<UInt_64> &data) const;
void Deserialize(Serializer<UInt_64> &data);