Added ConstStringSetTest.cpp, and first test OK!

This commit is contained in:
hyzboy 2024-07-11 01:20:50 +08:00
parent f7a4c8c333
commit b8a11960b4
3 changed files with 172 additions and 1 deletions

View File

@ -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) target_link_libraries(${project_name} PRIVATE CMCore CMPlatform CMUtil)
if(UNIX) if(UNIX)
@ -47,6 +47,12 @@ cm_example_project("DataType" Size2Test)
add_executable(Uint2HexStrTest datatype/Uint2HexStrTest.cpp) add_executable(Uint2HexStrTest datatype/Uint2HexStrTest.cpp)
cm_example_project("DataType" Uint2HexStrTest) 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) add_executable(LifetimeTest datatype/LifetimeTest.cpp)

View File

@ -0,0 +1,50 @@
#include<hgl/type/ConstStringSet.h>
#include<iostream>
using namespace hgl;
using namespace std;
char rand_str[5];
int make_rand_str()
{
int len=rand()%3+1;
for(int i=0;i<len;i++)
{
int v=rand()%(26+26+10);
if(v<26)
rand_str[i]=v%26+'a';
else
if(v<52)
rand_str[i]=(v-26)%26+'A';
else
rand_str[i]=(v-52)%10+'0';
}
rand_str[len]=0;
return len;
}
void main()
{
ConstAnsiStringSet cass;
ConstStringView<char> 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<<i<<" : "<<id<<" : "<<rand_str<<std::endl;
}
std::cout<<"save to filename: csv.txt"<<std::endl;
SaveToTextFile(OS_TEXT("csv.txt"),&cass,false,true);
SaveToTextFile(OS_TEXT("csv_id.txt"),&cass,true,true);
}

115
datatype/IDNameTest.cpp Normal file
View File

@ -0,0 +1,115 @@
#include<hgl/type/ConstStringSet.h>
#include<hgl/type/Map.h>
#include<typeinfo>
#include<typeindex>
#include<iostream>
namespace hgl
{
namespace IDNameManager
{
template<typename SC>
bool RegistryIDName(ConstStringView<SC> &csv,const std::type_index &index,const SC *name_string)
{
static ObjectMap<std::type_index,ConstStringSet<SC>> css_set;
ConstStringSet<SC> *css;
if(css_set.KeyExist(index))
{
css_set.Get(index,css);
}
else
{
css=new ConstStringSet<SC>;
css_set.Add(index,css);
}
return css->AddString(csv,name_string,hgl::strlen(name_string));
}
}
/**
* ID+<br>
*
*/
template<typename SC,int CLASS_COUNTER> class OrderedIDName
{
protected:
ConstStringView<SC> 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<SC>(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<SC,CLASS_COUNTER> &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<type,__COUNTER__>; //使用__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: "<<oin_utf8.GetID()<<std::endl;
std::cout<<"oin_u8: "<<oin_u8.GetID()<<std::endl;
std::cout<<"oin_u8same: "<<oin_u8same.GetID()<<std::endl;
}