This commit is contained in:
qingjiao 2025-12-06 17:31:53 +08:00
parent 2037405731
commit 5bb7fa1387
7 changed files with 74 additions and 16 deletions

View File

@ -45,7 +45,7 @@ CTL::ClassFactoryInfo * CTL::ClassFactory::get_info(const std::string &className
CTL::ClassFactoryInfo * CTL::ClassFactory::get_info(const std::string &className, const std::string &infoName) {
const auto info = m_map_info[className];
for (auto &item : info) {
if (item->Name == infoName) {
if (item->getName() == infoName) {
return item;
}
}

View File

@ -30,6 +30,22 @@ namespace CTL {
[](const std::string& str) -> void* { \
return CTL::ClassFactory::getInstance()->create_class(str); \
}(classNameStr)
template<typename T>
T Object::get(const std::string &infoName) {
const auto factory = ClassFactory::getInstance();
const auto info = factory->get_info(m_class_Name, infoName);
const size_t offset = info->getOffset();
return *reinterpret_cast<T *>(reinterpret_cast<unsigned char *>(this) + offset);
}
template<typename T>
void Object::set(const std::string& infoName, T value) {
const auto factory = ClassFactory::getInstance();
const auto info = factory->get_info(m_class_Name, infoName);
const size_t offset = info->getOffset();
*reinterpret_cast<T *>(reinterpret_cast<char *>(this) + offset) = value;
}
}

View File

@ -3,7 +3,9 @@
namespace CTL {
class ClassFactoryInfo {
std::string Name;
std::string typeName;
size_t offset = 0;
public:
//-------------------------------------------------------------------------------------------
// 默认构造函数
@ -17,13 +19,17 @@ namespace CTL {
this->typeName = typeName;
this->offset = offset;
}
~ClassFactoryInfo() {
}
~ClassFactoryInfo() = default;
//-------------------------------------------------------------------------------------------
std::string Name;
std::string typeName;
size_t offset = 0;
[[nodiscard]] std::string getName() const {
return Name;
}
[[nodiscard]] std::string getTypeName() const {
return typeName;
}
[[nodiscard]] size_t getOffset() const {
return offset;
}
//-------------------------------------------------------------------------------------------
private:

View File

@ -15,6 +15,7 @@ namespace CTL {
inline CTL::Object * create_##className() \
{ \
CTL::Object* obj = new className(); \
obj->set_class_name(#className); \
return obj; \
} \
inline CTL::ClassRegister classRegister##className(#className, create_##className);

View File

@ -3,6 +3,8 @@
#include <chrono>
#include <random>
#include "CCClassFactory.h"
bool CTL::Object::equals(const Object *other) {
return this == other; // 默认比较内存地址
@ -18,6 +20,29 @@ std::string CTL::Object::toString() const {
return buffer;
}
void CTL::Object::set_class_name(const std::string &name) {
this->m_class_Name = name;
}
std::string CTL::Object::get_class_name() const{
return m_class_Name;
}
int CTL::Object::get_info_count() const {
const auto factory = ClassFactory::getInstance();
return factory->get_info_count(this->m_class_Name);
}
CTL::ClassFactoryInfo * CTL::Object::get_info(const int pos) const {
const auto factory = ClassFactory::getInstance();
return factory->get_info(this->m_class_Name, pos);
}
CTL::ClassFactoryInfo * CTL::Object::get_info(const std::string &infoName) const {
const auto factory = ClassFactory::getInstance();
return factory->get_info(this->m_class_Name, infoName);
}
std::ostream & CTL::operator<<(std::ostream &os, const Object &map) {
os << map.toString();
return os;

View File

@ -12,31 +12,39 @@ namespace CTL {
class Object {
inline static std::mutex Mutex_Object;
std::string MemoryID;
std::string m_class_Name;
public:
private:
public:
//-------------------------------------------------------------------------------------
Object() = default;
virtual ~Object() = default; // 虚析构函数确保正确析构
virtual ~Object() = default;
//-------------------------------------------------------------------------------------
// 比较两个对象是否逻辑相等(默认实现为地址比较)
virtual bool equals(const Object* other);
// 生成基于对象地址的哈希值
[[nodiscard]] virtual size_t hashCode() const noexcept;
// 返回对象的字符串表示(默认格式:类名@地址)
[[nodiscard]] virtual std::string toString() const;
void set_class_name(const std::string& name);
std::string get_class_name() const;
//-------------------------------------------------------------------------------------
int get_info_count() const;
ClassFactoryInfo* get_info(int pos) const;
ClassFactoryInfo* get_info(const std::string& infoName) const;
template<typename T>
T get(const std::string& infoName);
template<typename T>
void set(const std::string& infoName, T value);
//-------------------------------------------------------------------------------------
friend std::ostream& operator<<(std::ostream& os, const Object& map);
template<typename T = Object, typename... Args>
static std::shared_ptr<T> create(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
int get_info_count();
ClassFactoryInfo* get_info(int pos);
ClassFactoryInfo* get_info(const std::string& infoName);
template<typename T>
T get(const std::string& infoName);
template<typename T>
void set(const std::string& infoName, T value);
//-------------------------------------------------------------------------------------
};
}

View File

@ -14,5 +14,7 @@ CC_Register_INFO(Demo, b);
int main() {
const auto factory = CTL::ClassFactory::getInstance();
const auto a = factory->create_class("Demo");
a->set("a",2);
const auto as = a->get<int>("a");
return 0;
}