Added IDNameTest.cpp, first test OK!

This commit is contained in:
hyzboy 2024-07-12 02:51:59 +08:00
parent b8a11960b4
commit 6d5612b8f2

View File

@ -1,115 +1,19 @@
#include<hgl/type/ConstStringSet.h>
#include<hgl/type/Map.h>
#include<typeinfo>
#include<typeindex>
#include<hgl/type/IDName.h>
#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);
UTF8IDName id1("utf8_1");
UTF8IDName id2("utf8_2");
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;
}
AnsiIDName id3("ansi_1");
AnsiIDName id4("ansi_2");
std::cout<<id1.GetID()<<":"<<id1.GetName()<<", ClassID: "<<std::hex<<id1.GetClassID()<<std::endl;
std::cout<<id2.GetID()<<":"<<id2.GetName()<<", ClassID: "<<std::hex<<id2.GetClassID()<<std::endl;
std::cout<<id3.GetID()<<":"<<id3.GetName()<<", ClassID: "<<std::hex<<id3.GetClassID()<<std::endl;
std::cout<<id4.GetID()<<":"<<id4.GetName()<<", ClassID: "<<std::hex<<id4.GetClassID()<<std::endl;
}