From 8c76767711b4730971b00213923cd7386ac501a5 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 3 Jun 2019 22:32:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0db/Field=E7=AD=89=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 +- inc/hgl/Str.h | 223 ++++++++++++++++++++++++++++++++++ inc/hgl/db/Field.h | 98 +++++++++++++++ inc/hgl/db/FieldType.h | 84 +++++++++++++ src/CMakeLists.txt | 2 +- src/SceneGraph/CMakeLists.txt | 20 +-- src/Util/CMakeLists.txt | 15 ++- src/Util/Field.cpp | 9 ++ src/Util/FieldType.cpp | 167 +++++++++++++++++++++++++ src/Util/FieldTypeConvert.h | 32 +++++ 10 files changed, 642 insertions(+), 16 deletions(-) create mode 100644 inc/hgl/db/Field.h create mode 100644 inc/hgl/db/FieldType.h create mode 100644 src/Util/Field.cpp create mode 100644 src/Util/FieldType.cpp create mode 100644 src/Util/FieldTypeConvert.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7977fe74..fd3a5315 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows") include_directories(${Vulkan_INCLUDE_DIRS}) -# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/jsoncpp/include) -# add_subdirectory(3rdpty/jsoncpp) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/jsoncpp/include) + add_subdirectory(3rdpty/jsoncpp) elseif(CMAKE_SYSTEM_NAME STREQUAL "Android") add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR) elseif(UNIX) @@ -37,13 +37,13 @@ add_definitions(-DMATH_AVX) add_definitions(-DGLFW_INCLUDE_VULKAN) SET(ULRE ULRE.Base -# ULRE.Util + ULRE.Util ULRE.RenderDevice.Vulkan ULRE.SceneGraph ULRE.Platform MathGeoLib spirv-cross-core -# jsoncpp_lib + jsoncpp_lib ${RENDER_LIBRARY} ${Vulkan_LIBRARIES}) diff --git a/inc/hgl/Str.h b/inc/hgl/Str.h index 952e8325..c1c9e2e3 100644 --- a/inc/hgl/Str.h +++ b/inc/hgl/Str.h @@ -1275,6 +1275,229 @@ namespace hgl *dst=0; } + /** + * 复制一个字符串,并将字符串全部转换成小写 + */ + template + uint lower_cpy(T *target,const T *source) + { + if(!target||!source)return 0; + uint count=0; + + while(*source) + { + if(*source>='A'&&*source<='Z') + *target=*source+32; + else + *target=*source; + + ++target; + ++source; + ++count; + } + + *target=0; + return count; + } + + /** + * 复制一个字符串,并将字符串全部转换成大写 + */ + template + uint upper_cpy(T *target,const T *source) + { + if(!target||!source)return 0; + uint count=0; + + while(*source) + { + if(*source>='a'&&*source<='z') + *target=*source-32; + else + *target=*source; + + ++target; + ++source; + ++count; + } + + *target=0; + return count; + } + + /** + * 复制一个字符串,并将字符串全部转换成小写 + */ + template + uint lower_cpy(T *target,const T *source,int source_max) + { + if(!target||!source)return 0; + uint count=0; + + while(*source&&source_max>0) + { + if(*source>='A'&&*source<='Z') + *target=*source+32; + else + *target=*source; + + ++target; + ++source; + ++count; + --source_max; + } + + *target=0; + + return count; + } + + /** + * 复制一个字符串,并将字符串全部转换成大写 + */ + template + uint upper_cpy(T *target,const T *source,int source_max) + { + if(!target||!source)return 0; + uint count=0; + + while(*source&&source_max>0) + { + if(*source>='a'&&*source<='z') + *target=*source-32; + else + *target=*source; + + ++target; + ++source; + ++count; + --source_max; + } + + *target=0; + return count; + } + + /** + * 复制一个字符串,将字符串全部转换成小写,并清除前后的空格 + */ + template + uint lower_clip_cpy(T *target,const T *source) + { + if(!target||!source)return 0; + uint count=0; + + while(*source) + { + if(*source!=' ') + { + if(*source>='A'&&*source<='Z') + *target=*source+32; + else + *target=*source; + + ++target; + ++count; + } + + ++source; + } + + *target=0; + + return count; + } + + /** + * 复制一个字符串,并将字符串全部转换成大写 + */ + template + uint upper_clip_cpy(T *target,const T *source) + { + if(!target||!source)return 0; + uint count=0; + + while(*source) + { + if(*source!=' ') + { + if(*source>='a'&&*source<='z') + *target=*source-32; + else + *target=*source; + + ++target; + ++count; + } + + ++source; + } + + *target=0; + return count; + } + + /** + * 复制一个字符串,将字符串全部转换成小写,并清除前后的空格 + */ + template + uint lower_clip_cpy(T *target,const T *source,int source_max) + { + if(!target||!source)return 0; + uint count=0; + + while(*source&&source_max>0) + { + if(*source!=' ') + { + if(*source>='A'&&*source<='Z') + *target=*source+32; + else + *target=*source; + + ++target; + ++count; + } + + ++source; + --source_max; + } + + *target=0; + + return count; + } + + /** + * 复制一个字符串,并将字符串全部转换成大写 + */ + template + uint upper_clip_cpy(T *target,const T *source,int source_max) + { + if(!target||!source)return 0; + uint count=0; + + while(*source&&source_max>0) + { + if(*source!=' ') + { + if(*source>='a'&&*source<='z') + *target=*source-32; + else + *target=*source; + + ++target; + ++count; + } + + ++source; + --source_max; + } + + *target=0; + return count; + } + /** * 统计在字符串中某个字符的出现次数 * @param str 字符串 diff --git a/inc/hgl/db/Field.h b/inc/hgl/db/Field.h new file mode 100644 index 00000000..56f18024 --- /dev/null +++ b/inc/hgl/db/Field.h @@ -0,0 +1,98 @@ +#ifndef HGL_DB_FIELD_INCLUDE +#define HGL_DB_FIELD_INCLUDE + +#include +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 class FieldSingleValue:public Field + { + public: + + using Field::Field; + virtual ~FieldSingleValue()=default; + };//template 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 class FieldArray:public FieldArrayBase + { + public: + + using FieldArrayBase::FieldArrayBase; + virtual ~FieldArray()=default; + };//template class FieldArray:public FieldArrayBase + + #define FIELD_TYPE_DEFINE(type_name,type) using Field##type_name=FieldSingleValue; \ + using Field##type_name##Array=FieldArray; + + 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 diff --git a/inc/hgl/db/FieldType.h b/inc/hgl/db/FieldType.h new file mode 100644 index 00000000..a3bb3a31 --- /dev/null +++ b/inc/hgl/db/FieldType.h @@ -0,0 +1,84 @@ +#ifndef HGL_DB_FIELD_TYPE_INCLUDE +#define HGL_DB_FIELD_TYPE_INCLUDE + +#include +#include +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 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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b246f65c..15f8a3f7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ add_subdirectory(Base) -#add_subdirectory(Util) +add_subdirectory(Util) add_subdirectory(RenderDevice) add_subdirectory(SceneGraph) add_subdirectory(Platform) diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 7d049bdf..ecd127b2 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -1,29 +1,29 @@ SET(SCENE_GRAPH_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/AABox.h ${ROOT_INCLUDE_PATH}/hgl/graph/Camera.h ${ROOT_INCLUDE_PATH}/hgl/graph/Light.h - ${ROOT_INCLUDE_PATH}/hgl/graph/SceneDB.h + ${ROOT_INCLUDE_PATH}/hgl/graph/SceneDB.h ${ROOT_INCLUDE_PATH}/hgl/graph/SceneNode.h ${ROOT_INCLUDE_PATH}/hgl/graph/SceneOrient.h ${ROOT_INCLUDE_PATH}/hgl/graph/RenderableInstance.h - ${ROOT_INCLUDE_PATH}/hgl/graph/RenderList.h + ${ROOT_INCLUDE_PATH}/hgl/graph/RenderList.h ${ROOT_INCLUDE_PATH}/hgl/graph/VertexBufferCreater.h ${ROOT_INCLUDE_PATH}/hgl/graph/VertexBuffer.h ${ROOT_INCLUDE_PATH}/hgl/graph/InlineGeometry.h - ${ROOT_INCLUDE_PATH}/hgl/graph/Mesh.h - ${ROOT_INCLUDE_PATH}/hgl/graph/Material.h - ${ROOT_INCLUDE_PATH}/hgl/graph/TextureType.h - ) + ${ROOT_INCLUDE_PATH}/hgl/graph/Mesh.h + ${ROOT_INCLUDE_PATH}/hgl/graph/Material.h + ${ROOT_INCLUDE_PATH}/hgl/graph/TextureType.h + ) SET(SCENE_GRAPH_SOURCE AABox.cpp Camera.cpp RenderList.cpp - SceneDB.cpp + SceneDB.cpp SceneNode.cpp SceneOrient.cpp InlineGeometry.cpp - Material.cpp - Mesh.cpp - SceneFile.cpp) + Material.cpp + Mesh.cpp + SceneFile.cpp) SOURCE_GROUP("Header Files" FILES ${SCENE_GRAPH_HEADER}) SOURCE_GROUP("Source Files" FILES ${SCENE_GRAPH_SOURCE}) diff --git a/src/Util/CMakeLists.txt b/src/Util/CMakeLists.txt index af7725c9..85a7ba3b 100644 --- a/src/Util/CMakeLists.txt +++ b/src/Util/CMakeLists.txt @@ -1 +1,14 @@ -add_library(ULRE.Util STATIC JsonTool.cpp) +SET(UTIL_HEADER ${ROOT_INCLUDE_PATH}/hgl/util/JsonTool.h + ${ROOT_INCLUDE_PATH}/hgl/db/FieldType.h + ${ROOT_INCLUDE_PATH}/hgl/db/Field.h) + +SET(UTIL_SOURCE Field.cpp + FieldTypeConvert.h + FieldType.cpp + JsonTool.cpp) + +SOURCE_GROUP("Header Files" FILES ${UTIL_HEADER}) +SOURCE_GROUP("Source Files" FILES ${UTIL_SOURCE}) + +add_library(ULRE.Util STATIC ${UTIL_HEADER} + ${UTIL_SOURCE}) diff --git a/src/Util/Field.cpp b/src/Util/Field.cpp new file mode 100644 index 00000000..402360b5 --- /dev/null +++ b/src/Util/Field.cpp @@ -0,0 +1,9 @@ +#include + +namespace hgl +{ + namespace db + { + + }//namespace db +}//namespace hgl diff --git a/src/Util/FieldType.cpp b/src/Util/FieldType.cpp new file mode 100644 index 00000000..ebabc0e1 --- /dev/null +++ b/src/Util/FieldType.cpp @@ -0,0 +1,167 @@ +#include +namespace hgl +{ + namespace db + { + namespace + { + template + struct FieldTypeNameConvert + { + const T name[32]; + uint name_length; + FieldBaseType type; + }; + + constexpr int field_size[(uint)FieldBaseType::RANGE_SIZE]={ 1, + 1,2,4,8, + 1,2,4,8, + 4,8, + 2,4, + 0,0}; + + /** + * 字段类型功能类 + */ + template class FieldTypeClass + { + static FieldTypeNameConvert filed_type_name_convert[]; + + public: + + static FieldBaseType ParseType(const T *str,const uint str_size) + { + T lower_name[32]; + + const uint name_len=lower_clip_cpy(lower_name,str,str_size); + + if(name_len==0) + return FieldBaseType::ERROR_TYPE; + + const FieldTypeNameConvert *p=filed_type_name_convert; + + for(uint i=(uint)FieldBaseType::BEGIN_RANGE;i<=(uint)FieldBaseType::END_RANGE;i++) + { + if(name_len==p->name_length) + { + if(hgl::strcmp(p->name,str,p->name_length)==0) + return(p->type); + } + + ++p; + } + + return FieldBaseType::ERROR_TYPE; + } + + static bool ParseArrayType(FieldArray &fa,const T *str) + { + if(!str)return(false); + + const T *p=hgl::strchr(str,','); + + if(!p) + return(false); + + fa.base_type=ParseType(str,p-str); + + if(fa.base_type==FieldBaseType::ERROR_TYPE) + return(false); + + T num[32]; + + lower_clip_cpy(num,p+1); + + return stou(num,fa.count); + } + };//template class FieldTypeClass + + using FieldTypeClassU8=FieldTypeClass; + using FieldTypeClassU16=FieldTypeClass; + + #define FIELD_TYPE_CONVERY(name,type) {U8_TEXT(name),sizeof(name)-1,FieldBaseType::type} + template<> FieldTypeNameConvert FieldTypeClass::filed_type_name_convert[]= + #include"FieldTypeConvert.h" + #undef FIELD_TYPE_CONVERY + + #define FIELD_TYPE_CONVERY(name,type) {U16_TEXT(name),sizeof(name)-1,FieldBaseType::type} + template<> FieldTypeNameConvert FieldTypeClass::filed_type_name_convert[]= + #include"FieldTypeConvert.h" + #undef FIELD_TYPE_CONVERY + }//namespace + + const int GetFieldSize(const FieldBaseType fbt) + { + if(fbtFieldBaseType::END_RANGE) + return -1; + + return field_size[(uint)fbt]; + } + + const int GetFieldSize(const FieldType *ft) + { + if(!ft)return(-1); + if(ft->base_typebase_type>FieldBaseType::END_RANGE) + return -1; + + if(ft->base_typebase_type); + + if(ft->base_type==FieldBaseType::FixedArray) + { + const FieldArray *fa=(const FieldArray *)ft; + + if(fa->count==0)return 0; + + return GetFieldSize(fa->base_type)*fa->count; + } + + if(ft->base_type==FieldBaseType::VarArray) + return 0; + + if(ft->base_type==FieldBaseType::Struct) + { + const FieldStruct *fs=(const FieldStruct *)ft; + + const uint count=fs->field_list.GetCount(); + FieldType **sft=fs->field_list.GetData(); + + uint total=0; + int size; + for(uint i=0;i