增加db/Field等定义
This commit is contained in:
98
inc/hgl/db/Field.h
Normal file
98
inc/hgl/db/Field.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#ifndef HGL_DB_FIELD_INCLUDE
|
||||
#define HGL_DB_FIELD_INCLUDE
|
||||
|
||||
#include<hgl/db/FieldType.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace db
|
||||
{
|
||||
/**
|
||||
* 字段基类
|
||||
*/
|
||||
class Field
|
||||
{
|
||||
FieldType *field_type;
|
||||
|
||||
public:
|
||||
|
||||
const FieldType *GetFieldType()const{return field_type;} ///<获取字段类型
|
||||
|
||||
public:
|
||||
|
||||
Field(FieldType *ft):field_type(ft){}
|
||||
virtual ~Field()
|
||||
{
|
||||
if(field_type)
|
||||
delete field_type;
|
||||
}
|
||||
};//class Field
|
||||
|
||||
/**
|
||||
* 单个数据字段模板基类
|
||||
*/
|
||||
template<typename T> class FieldSingleValue:public Field
|
||||
{
|
||||
public:
|
||||
|
||||
using Field::Field;
|
||||
virtual ~FieldSingleValue()=default;
|
||||
};//template<typename T> class FieldSingleValue:public Field
|
||||
|
||||
class FieldArrayBase:public Field
|
||||
{
|
||||
protected:
|
||||
|
||||
FieldArrayType *array_type;
|
||||
|
||||
public:
|
||||
|
||||
FieldArrayBase(FieldArrayType *fa):Field(fa),array_type(fa){}
|
||||
virtual ~FieldArrayBase()=default;
|
||||
|
||||
const FieldBaseType GetMemberType ()const{return array_type?array_type->GetMemberType():FieldBaseType::ERROR_TYPE;}
|
||||
const uint64 GetLength ()const{return array_type?array_type->GetLength():-1;}
|
||||
};//class FieldVarArray:public Field
|
||||
|
||||
template<typename T> class FieldArray:public FieldArrayBase
|
||||
{
|
||||
public:
|
||||
|
||||
using FieldArrayBase::FieldArrayBase;
|
||||
virtual ~FieldArray()=default;
|
||||
};//template<typename T> class FieldArray:public FieldArrayBase
|
||||
|
||||
#define FIELD_TYPE_DEFINE(type_name,type) using Field##type_name=FieldSingleValue<type>; \
|
||||
using Field##type_name##Array=FieldArray<type>;
|
||||
|
||||
FIELD_TYPE_DEFINE(Bool, bool)
|
||||
|
||||
FIELD_TYPE_DEFINE(Int8, int8)
|
||||
FIELD_TYPE_DEFINE(Int16, int16)
|
||||
FIELD_TYPE_DEFINE(Int32, int32)
|
||||
FIELD_TYPE_DEFINE(Int64, int64)
|
||||
|
||||
FIELD_TYPE_DEFINE(Uint8, uint8)
|
||||
FIELD_TYPE_DEFINE(Uint16, uint16)
|
||||
FIELD_TYPE_DEFINE(Uint32, uint32)
|
||||
FIELD_TYPE_DEFINE(Uint64, uint64)
|
||||
|
||||
FIELD_TYPE_DEFINE(Float, float)
|
||||
FIELD_TYPE_DEFINE(Double, double)
|
||||
|
||||
FIELD_TYPE_DEFINE(Char, char)
|
||||
FIELD_TYPE_DEFINE(UTF16, u16char)
|
||||
|
||||
#undef FIELD_TYPE_DEFINE
|
||||
|
||||
class FieldStruct:public Field
|
||||
{
|
||||
FieldStructType *struct_type;
|
||||
|
||||
public:
|
||||
|
||||
FieldStruct(FieldStructType *fs):Field(fs),struct_type(fs){}
|
||||
virtual ~FieldStruct()=default;
|
||||
};//class FieldStruct:public Field
|
||||
}//namespace db
|
||||
}//namespace hgl
|
||||
#endif//HGL_DB_FIELD_INCLUDE
|
84
inc/hgl/db/FieldType.h
Normal file
84
inc/hgl/db/FieldType.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#ifndef HGL_DB_FIELD_TYPE_INCLUDE
|
||||
#define HGL_DB_FIELD_TYPE_INCLUDE
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
#include<hgl/type/StringList.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace db
|
||||
{
|
||||
/**
|
||||
* 字段基本类型枚举
|
||||
*/
|
||||
enum class FieldBaseType
|
||||
{
|
||||
Bool=0,
|
||||
|
||||
Int8, Int16, Int32, Int64,
|
||||
Uint8, Uint16, Uint32, Uint64,
|
||||
|
||||
Float,
|
||||
Double,
|
||||
|
||||
UTF16LE,
|
||||
UTF32LE,
|
||||
|
||||
Array, ///<阵列
|
||||
Struct, ///<结构体
|
||||
|
||||
BEGIN_RANGE =Bool,
|
||||
END_RANGE =Struct,
|
||||
RANGE_SIZE =END_RANGE-BEGIN_RANGE+1,
|
||||
ERROR_TYPE=0xFF
|
||||
};//enum class FieldBaseType
|
||||
|
||||
struct FieldType
|
||||
{
|
||||
FieldBaseType base_type;
|
||||
|
||||
public:
|
||||
|
||||
FieldType():base_type(FieldBaseType::ERROR_TYPE){}
|
||||
FieldType(const FieldBaseType &bt):base_type(bt){}
|
||||
virtual ~FieldType()=default;
|
||||
|
||||
FieldBaseType GetFieldType()const{return base_type;}
|
||||
};
|
||||
|
||||
struct FieldArrayType:public FieldType
|
||||
{
|
||||
FieldBaseType member_type;
|
||||
uint64 length;
|
||||
|
||||
public:
|
||||
|
||||
FieldArrayType():FieldType(FieldBaseType::Array),member_type(FieldBaseType::ERROR_TYPE),length(0){}
|
||||
FieldArrayType(const FieldBaseType &fbt,const uint64 &c):FieldType(FieldBaseType::Array),member_type(fbt),length(c){}
|
||||
FieldArrayType(const FieldArrayType &fa):FieldType(FieldBaseType::Array),member_type(fa.member_type),length(fa.length){}
|
||||
virtual ~FieldArrayType()=default;
|
||||
|
||||
const FieldBaseType GetMemberType ()const{return member_type;}
|
||||
const uint64 GetLength ()const{return length;}
|
||||
};//struct FieldArrayType
|
||||
|
||||
struct FieldStructType:public FieldType
|
||||
{
|
||||
ObjectList<FieldType> field_list;
|
||||
|
||||
public:
|
||||
|
||||
FieldStructType():FieldType(FieldBaseType::Struct){}
|
||||
virtual ~FieldStructType()=default;
|
||||
};//struct FieldStructType
|
||||
|
||||
const int GetFieldSize(const FieldBaseType fbt);
|
||||
const int GetFieldSize(const FieldType *ft);
|
||||
|
||||
FieldBaseType ParseFieldType(const char *str);
|
||||
FieldBaseType ParseFieldType(const u16char *str);
|
||||
|
||||
bool ParseArrayFieldType(FieldArrayType &,const char *);
|
||||
bool ParseArrayFieldType(FieldArrayType &,const u16char *);
|
||||
}//namespace db
|
||||
}//namespace hgl
|
||||
#endif//HGL_DB_FIELD_TYPE_INCLUDE
|
Reference in New Issue
Block a user