From b8a11960b42e8715557d8445ef0c70b952c6aad8 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 11 Jul 2024 01:20:50 +0800 Subject: [PATCH] Added ConstStringSetTest.cpp, and first test OK! --- CMakeLists.txt | 8 ++- datatype/ConstStringSetTest.cpp | 50 ++++++++++++++ datatype/IDNameTest.cpp | 115 ++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 datatype/ConstStringSetTest.cpp create mode 100644 datatype/IDNameTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cefc9f9..effd2f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -macro(cm_example_project sub_folder project_name) +macro(cm_example_project sub_folder project_name) target_link_libraries(${project_name} PRIVATE CMCore CMPlatform CMUtil) if(UNIX) @@ -47,6 +47,12 @@ cm_example_project("DataType" Size2Test) add_executable(Uint2HexStrTest datatype/Uint2HexStrTest.cpp) cm_example_project("DataType" Uint2HexStrTest) +add_executable(ConstStringSetTest datatype/ConstStringSetTest.cpp) +cm_example_project("DataType" ConstStringSetTest) + +add_executable(IDNameTest datatype/IDNameTest.cpp) +cm_example_project("DataType" IDNameTest) + #################################################################################################### add_executable(LifetimeTest datatype/LifetimeTest.cpp) diff --git a/datatype/ConstStringSetTest.cpp b/datatype/ConstStringSetTest.cpp new file mode 100644 index 0000000..165f3e2 --- /dev/null +++ b/datatype/ConstStringSetTest.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace hgl; +using namespace std; + +char rand_str[5]; + +int make_rand_str() +{ + int len=rand()%3+1; + + for(int i=0;i csv; + int len; + int id; + + for(int i=0;i<1000;i++) + { + len=make_rand_str(); + + id=cass.AddString(csv,rand_str,len); + + std::cout< +#include + +#include +#include +#include + +namespace hgl +{ + namespace IDNameManager + { + template + bool RegistryIDName(ConstStringView &csv,const std::type_index &index,const SC *name_string) + { + static ObjectMap> css_set; + + ConstStringSet *css; + + if(css_set.KeyExist(index)) + { + css_set.Get(index,css); + } + else + { + css=new ConstStringSet; + css_set.Add(index,css); + } + + return css->AddString(csv,name_string,hgl::strlen(name_string)); + } + } + + /** + * 顺序ID+名称数据结构模板
+ * 按添加进来的名字先后顺序一个个产生连续的序号,所有数据只可读不可写 + */ + template class OrderedIDName + { + protected: + + ConstStringView csv; + + protected: + + void Clear() + { + csv.id=-1; + csv.length=-1; + csv.str=nullptr; + } + + void Update(const SC *name_string) + { + if(!name_string) + { + Clear(); + } + else + { + IDNameManager::RegistryIDName(csv,typeid(*this),name_string); + } + } + + public: + + const int GetID ()const{return csv.id;} ///<获取名称ID + const SC *GetName ()const{return csv.str;} ///<获取名称字符串 + + public: + + OrderedIDName() + { + Clear(); + } + + OrderedIDName(const SC *name_string) + { + Update(name_string); + } + + void operator = (const SC *name_string) + { + Update(name_string); + } + + void operator = (const OrderedIDName &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 IDNameDefine(name,type) using name=OrderedIDName; //使用__COUNTER__是为了让typeid()不同 + + IDNameDefine(AnsiIDName,char) + IDNameDefine(UTF8IDName,char) +}//namespace hgl + +void main() +{ + using namespace hgl; + + UTF8IDName oin_utf8("oin_utf8"); + UTF8IDName oin_u8("oin u8"); + UTF8IDName oin_u8same(oin_u8); + + std::cout<<"oin_utf8: "<