mirror of https://github.com/SeanOMik/libki.git
dml: Add header files
This commit is contained in:
parent
ddbeb492b6
commit
54c1e509f6
|
@ -8,10 +8,14 @@ set_target_properties(${PROJECT_NAME}
|
|||
CXX_STANDARD 11
|
||||
)
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
INTERFACE
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
add_subdirectory("src/dml")
|
||||
|
||||
option(KI_BUILD_EXAMPLES "Determines whether to build examples." ON)
|
||||
if (KI_BUILD_EXAMPLES)
|
||||
add_subdirectory("examples")
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
#pragma once
|
||||
#include "FieldBase.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace ki
|
||||
{
|
||||
namespace dml
|
||||
{
|
||||
template <typename ValueT>
|
||||
class Field : public FieldBase
|
||||
{
|
||||
friend Record;
|
||||
public:
|
||||
ValueT get_value() const;
|
||||
void set_value(ValueT value);
|
||||
|
||||
Field<ValueT> *clone(const Record &record) const;
|
||||
|
||||
void write_to(std::ostream &ostream) const;
|
||||
void read_from(std::istream &istream);
|
||||
size_t get_size() const;
|
||||
protected:
|
||||
Field(std::string name, const Record &record)
|
||||
: FieldBase(name, record)
|
||||
{
|
||||
m_type_hash = typeid(ValueT).hash_code();
|
||||
m_value = ValueT();
|
||||
}
|
||||
private:
|
||||
ValueT m_value;
|
||||
};
|
||||
|
||||
typedef Field<BYT> BytField;
|
||||
typedef Field<UBYT> UBytField;
|
||||
typedef Field<SHRT> ShrtField;
|
||||
typedef Field<USHRT> UShrtField;
|
||||
typedef Field<INT> IntField;
|
||||
typedef Field<UINT> UIntField;
|
||||
typedef Field<STR> StrField;
|
||||
typedef Field<WSTR> WStrField;
|
||||
typedef Field<FLT> FltField;
|
||||
typedef Field<DBL> DblField;
|
||||
typedef Field<GID> GidField;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "../util/Serializable.h"
|
||||
|
||||
namespace ki
|
||||
{
|
||||
namespace dml
|
||||
{
|
||||
class Record;
|
||||
|
||||
class FieldBase : public util::Serializable
|
||||
{
|
||||
friend Record;
|
||||
public:
|
||||
const Record &get_record() const;
|
||||
std::string get_name() const;
|
||||
bool is_transferable() const;
|
||||
|
||||
template <typename ValueT>
|
||||
bool is_type() const
|
||||
{
|
||||
return (typeid(ValueT).hash_code() == m_type_hash);
|
||||
}
|
||||
protected:
|
||||
size_t m_type_hash;
|
||||
|
||||
FieldBase(std::string name, const Record& record);
|
||||
|
||||
void set_name(std::string name);
|
||||
void set_transferable(bool transferable);
|
||||
|
||||
virtual FieldBase *clone(const Record &record) const = 0;
|
||||
private:
|
||||
const Record &m_record;
|
||||
std::string m_name;
|
||||
bool m_transferable;
|
||||
};
|
||||
|
||||
typedef std::vector<FieldBase *> FieldList;
|
||||
typedef std::map<std::string, FieldBase *> FieldNameMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
#include "FieldBase.h"
|
||||
#include "Field.h"
|
||||
|
||||
namespace ki
|
||||
{
|
||||
namespace dml
|
||||
{
|
||||
class Record : public util::Serializable
|
||||
{
|
||||
public:
|
||||
Record();
|
||||
Record(const Record &record);
|
||||
|
||||
bool has_field(std::string name) const;
|
||||
|
||||
template <typename ValueT>
|
||||
bool has_field(std::string name) const
|
||||
{
|
||||
if (!has_field(name))
|
||||
return false;
|
||||
return m_field_map[name]->is_type<ValueT>();
|
||||
}
|
||||
|
||||
template <typename ValueT>
|
||||
Field<ValueT> &get_field(std::string name) const
|
||||
{
|
||||
if (has_field<ValueT>(name))
|
||||
return dynamic_cast<Field<ValueT> *>(m_fields[name]);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename ValueT>
|
||||
Field<ValueT> &add_field(std::string name, bool transferable = true)
|
||||
{
|
||||
if (has_field<ValueT>(name))
|
||||
return nullptr;
|
||||
Field<ValueT> *field = new Field<ValueT>(name, *this);
|
||||
auto *base = static_cast<FieldBase *>(field);
|
||||
m_fields.push_back(base);
|
||||
m_field_map.insert({ base->get_name(), base });
|
||||
return field;
|
||||
}
|
||||
|
||||
size_t get_field_count() const;
|
||||
|
||||
FieldList::iterator fields_begin() const;
|
||||
FieldList::iterator fields_end() const;
|
||||
|
||||
void write_to(std::ostream &ostream) const;
|
||||
void read_from(std::istream &istream);
|
||||
size_t get_size() const;
|
||||
private:
|
||||
FieldList m_fields;
|
||||
FieldNameMap m_field_map;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace ki
|
||||
{
|
||||
namespace dml
|
||||
{
|
||||
typedef int8_t BYT;
|
||||
typedef uint8_t UBYT;
|
||||
typedef int16_t SHRT;
|
||||
typedef uint16_t USHRT;
|
||||
typedef int32_t INT;
|
||||
typedef uint32_t UINT;
|
||||
typedef std::string STR;
|
||||
typedef std::wstring WSTR;
|
||||
typedef float FLT;
|
||||
typedef double DBL;
|
||||
typedef uint64_t GID;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
#include <istream>
|
||||
|
||||
namespace ki
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
class Serializable
|
||||
{
|
||||
public:
|
||||
virtual void write_to(std::ostream &ostream) const = 0;
|
||||
virtual void read_from(std::istream &istream) = 0;
|
||||
virtual size_t get_size() const = 0;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
target_sources(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/src/dml/FieldBase.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/Record.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/BytField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/UBytField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/ShrtField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/UShrtField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/IntField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/UIntField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/StrField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/WStrField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/FltField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/DblField.cpp
|
||||
${PROJECT_SOURCE_DIR}/src/dml/types/GidField.cpp
|
||||
)
|
Loading…
Reference in New Issue