Added IDName.cpp/.h
This commit is contained in:
parent
1b5ee2a877
commit
0f236d9bc0
100
inc/hgl/type/IDName.h
Normal file
100
inc/hgl/type/IDName.h
Normal file
@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/ConstStringSet.h>
|
||||
#include<typeinfo>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template<typename SC>
|
||||
bool RegistryIDName(const size_t hash_code,ConstStringView<SC> &csv,const SC *name_string,const int name_length);
|
||||
|
||||
/**
|
||||
* 顺序ID+名称数据结构模板<br>
|
||||
* 按添加进来的名字先后顺序一个个产生连续的序号,所有数据只可读不可写
|
||||
*/
|
||||
template<typename SC,int CLASS_COUNTER> class OrderedIDName
|
||||
{
|
||||
public:
|
||||
|
||||
using SelfClass=OrderedIDName<SC,CLASS_COUNTER>;
|
||||
|
||||
protected:
|
||||
|
||||
ConstStringView<SC> csv;
|
||||
|
||||
protected:
|
||||
|
||||
void Clear()
|
||||
{
|
||||
csv.id=-1;
|
||||
csv.length=-1;
|
||||
csv.str=nullptr;
|
||||
}
|
||||
|
||||
void Update(const SC *name_string,const int name_length)
|
||||
{
|
||||
if(name_length<=0)
|
||||
{
|
||||
Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
RegistryIDName<SC>(typeid(*this).hash_code(),csv,name_string,name_length);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
static const size_t GetClassID () ///<获取类ID
|
||||
{
|
||||
static SelfClass self;
|
||||
static size_t class_id=typeid(self).hash_code();
|
||||
|
||||
return class_id;
|
||||
}
|
||||
|
||||
const int GetID ()const{return csv.id;} ///<获取名称ID
|
||||
const SC * GetName ()const{return csv.GetString();} ///<获取名称字符串
|
||||
const int GetNameLength ()const{return csv.length;} ///<获取名称字符串长度
|
||||
|
||||
public:
|
||||
|
||||
OrderedIDName()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
OrderedIDName(const SC *name_string)
|
||||
{
|
||||
Update(name_string,hgl::strlen(name_string));
|
||||
}
|
||||
|
||||
void operator = (const SC *name_string)
|
||||
{
|
||||
Update(name_string,hgl::strlen(name_string));
|
||||
}
|
||||
|
||||
void operator = (const String<SC> &name_string)
|
||||
{
|
||||
Update(name_string.c_str(),name_string.Length());
|
||||
}
|
||||
|
||||
void operator = (const SelfClass &id_name)
|
||||
{
|
||||
hgl_cpy(csv,id_name.csv);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
const int Comp(const OrderedIDName &oin)const{return GetID()-oin.GetID();}
|
||||
|
||||
CompOperator(const OrderedIDName &,Comp)
|
||||
};//class IDName
|
||||
|
||||
#define DefineIDName(name,type) using name=OrderedIDName<type,__COUNTER__>; //使用__COUNTER__是为了让typeid()不同
|
||||
|
||||
DefineIDName(AnsiIDName, char)
|
||||
DefineIDName(WideIDName, wchar_t)
|
||||
DefineIDName(UTF8IDName, u8char)
|
||||
DefineIDName(UTF16IDName, u16char)
|
||||
DefineIDName(OSIDName, os_char)
|
||||
}//namespace hgl
|
@ -15,6 +15,14 @@ SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_HEADER_FILES}
|
||||
|
||||
SET(TYPE_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/type)
|
||||
|
||||
SET(TYPE_IDNAME_HEADER_FILES ${TYPE_INCLUDE_PATH}/ConstStringSet.h
|
||||
${TYPE_INCLUDE_PATH}/IDName.h)
|
||||
|
||||
SET(TYPE_IDNAME_SOURCE_FILES Text/ConstStringSetSaveToTextStream.cpp
|
||||
Type/IDName.cpp)
|
||||
|
||||
SOURCE_GROUP("DataType\\IDName" FILES ${TYPE_IDNAME_HEADER_FILES} ${TYPE_IDNAME_SOURCE_FILES})
|
||||
|
||||
SET(TYPE_COLLECTION_SOURCE ${TYPE_INCLUDE_PATH}/Collection.h
|
||||
Type/Collection.cpp)
|
||||
|
||||
@ -74,7 +82,6 @@ SET(STRING_HEADER_FILES ${TYPE_INCLUDE_PATH}/String.h
|
||||
${TYPE_INCLUDE_PATH}/SplitString.h
|
||||
${TYPE_INCLUDE_PATH}/MergeString.h
|
||||
${TYPE_INCLUDE_PATH}/StdString.h
|
||||
${TYPE_INCLUDE_PATH}/ConstStringSet.h
|
||||
)
|
||||
|
||||
SET(TEXT_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/Endian.h
|
||||
@ -85,8 +92,7 @@ SET(TEXT_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/Endian.h
|
||||
|
||||
SET(TEXT_SOURCE_FILES Text/Endian.cpp
|
||||
Text/CodePage.cpp
|
||||
Text/UnicodeBlocks.cpp
|
||||
Text/ConstStringSetSaveToTextStream.cpp)
|
||||
Text/UnicodeBlocks.cpp)
|
||||
|
||||
SOURCE_GROUP("Text\\String" FILES ${STRING_HEADER_FILES}
|
||||
Text/StringList.cpp)
|
||||
@ -237,6 +243,9 @@ add_cm_library(CMCore "CM" ${CORE_PLATFORM_HEADER_FILES}
|
||||
|
||||
${IO_SOURCE_FILES}
|
||||
|
||||
${TYPE_IDNAME_HEADER_FILES}
|
||||
${TYPE_IDNAME_SOURCE_FILES}
|
||||
|
||||
${MATH_HEADER_FILES}
|
||||
${MATH_SOURCE_FILES}
|
||||
|
||||
|
30
src/Type/IDName.cpp
Normal file
30
src/Type/IDName.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include<hgl/type/IDName.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template<typename SC>
|
||||
bool RegistryIDName(const size_t hash_code,ConstStringView<SC> &csv,const SC *name_string,const int name_length)
|
||||
{
|
||||
static ObjectMap<size_t,ConstStringSet<SC>> css_map;
|
||||
|
||||
ConstStringSet<SC> *css;
|
||||
|
||||
if(!css_map.Get(hash_code,css))
|
||||
{
|
||||
css=new ConstStringSet<SC>;
|
||||
css_map.Add(hash_code,css);
|
||||
}
|
||||
|
||||
return(css->AddString(csv,name_string,name_length)>=0);
|
||||
}
|
||||
|
||||
#define REGISTRY_ID_NAME(type) bool RegistryIDName_##type(const size_t hash_code,ConstStringView<type> &csv,const type *name,const int length){return RegistryIDName(hash_code,csv,name,length);}
|
||||
|
||||
REGISTRY_ID_NAME(char)
|
||||
REGISTRY_ID_NAME(wchar_t)
|
||||
REGISTRY_ID_NAME(u8char)
|
||||
REGISTRY_ID_NAME(u16char)
|
||||
REGISTRY_ID_NAME(os_char)
|
||||
|
||||
#undef REGISTRY_ID_NAME
|
||||
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user