V01
This commit is contained in:
parent
2037405731
commit
5bb7fa1387
@ -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) {
|
CTL::ClassFactoryInfo * CTL::ClassFactory::get_info(const std::string &className, const std::string &infoName) {
|
||||||
const auto info = m_map_info[className];
|
const auto info = m_map_info[className];
|
||||||
for (auto &item : info) {
|
for (auto &item : info) {
|
||||||
if (item->Name == infoName) {
|
if (item->getName() == infoName) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,6 +30,22 @@ namespace CTL {
|
|||||||
[](const std::string& str) -> void* { \
|
[](const std::string& str) -> void* { \
|
||||||
return CTL::ClassFactory::getInstance()->create_class(str); \
|
return CTL::ClassFactory::getInstance()->create_class(str); \
|
||||||
}(classNameStr)
|
}(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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
namespace CTL {
|
namespace CTL {
|
||||||
class ClassFactoryInfo {
|
class ClassFactoryInfo {
|
||||||
|
std::string Name;
|
||||||
|
std::string typeName;
|
||||||
|
size_t offset = 0;
|
||||||
public:
|
public:
|
||||||
//-------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------
|
||||||
// 默认构造函数
|
// 默认构造函数
|
||||||
@ -17,13 +19,17 @@ namespace CTL {
|
|||||||
this->typeName = typeName;
|
this->typeName = typeName;
|
||||||
this->offset = offset;
|
this->offset = offset;
|
||||||
}
|
}
|
||||||
~ClassFactoryInfo() {
|
~ClassFactoryInfo() = default;
|
||||||
|
|
||||||
}
|
|
||||||
//-------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------
|
||||||
std::string Name;
|
[[nodiscard]] std::string getName() const {
|
||||||
std::string typeName;
|
return Name;
|
||||||
size_t offset = 0;
|
}
|
||||||
|
[[nodiscard]] std::string getTypeName() const {
|
||||||
|
return typeName;
|
||||||
|
}
|
||||||
|
[[nodiscard]] size_t getOffset() const {
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
//-------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,7 @@ namespace CTL {
|
|||||||
inline CTL::Object * create_##className() \
|
inline CTL::Object * create_##className() \
|
||||||
{ \
|
{ \
|
||||||
CTL::Object* obj = new className(); \
|
CTL::Object* obj = new className(); \
|
||||||
|
obj->set_class_name(#className); \
|
||||||
return obj; \
|
return obj; \
|
||||||
} \
|
} \
|
||||||
inline CTL::ClassRegister classRegister##className(#className, create_##className);
|
inline CTL::ClassRegister classRegister##className(#className, create_##className);
|
||||||
|
|||||||
25
CCObject.cpp
25
CCObject.cpp
@ -3,6 +3,8 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
#include "CCClassFactory.h"
|
||||||
|
|
||||||
|
|
||||||
bool CTL::Object::equals(const Object *other) {
|
bool CTL::Object::equals(const Object *other) {
|
||||||
return this == other; // 默认比较内存地址
|
return this == other; // 默认比较内存地址
|
||||||
@ -18,6 +20,29 @@ std::string CTL::Object::toString() const {
|
|||||||
return buffer;
|
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) {
|
std::ostream & CTL::operator<<(std::ostream &os, const Object &map) {
|
||||||
os << map.toString();
|
os << map.toString();
|
||||||
return os;
|
return os;
|
||||||
|
|||||||
24
CCObject.h
24
CCObject.h
@ -12,31 +12,39 @@ namespace CTL {
|
|||||||
class Object {
|
class Object {
|
||||||
inline static std::mutex Mutex_Object;
|
inline static std::mutex Mutex_Object;
|
||||||
std::string MemoryID;
|
std::string MemoryID;
|
||||||
|
std::string m_class_Name;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
//-------------------------------------------------------------------------------------
|
||||||
Object() = default;
|
Object() = default;
|
||||||
virtual ~Object() = default; // 虚析构函数确保正确析构
|
virtual ~Object() = default;
|
||||||
|
//-------------------------------------------------------------------------------------
|
||||||
// 比较两个对象是否逻辑相等(默认实现为地址比较)
|
// 比较两个对象是否逻辑相等(默认实现为地址比较)
|
||||||
virtual bool equals(const Object* other);
|
virtual bool equals(const Object* other);
|
||||||
// 生成基于对象地址的哈希值
|
// 生成基于对象地址的哈希值
|
||||||
[[nodiscard]] virtual size_t hashCode() const noexcept;
|
[[nodiscard]] virtual size_t hashCode() const noexcept;
|
||||||
// 返回对象的字符串表示(默认格式:类名@地址)
|
// 返回对象的字符串表示(默认格式:类名@地址)
|
||||||
[[nodiscard]] virtual std::string toString() const;
|
[[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);
|
friend std::ostream& operator<<(std::ostream& os, const Object& map);
|
||||||
template<typename T = Object, typename... Args>
|
template<typename T = Object, typename... Args>
|
||||||
static std::shared_ptr<T> create(Args&&... args) {
|
static std::shared_ptr<T> create(Args&&... args) {
|
||||||
return std::make_shared<T>(std::forward<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);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
main.cpp
2
main.cpp
@ -14,5 +14,7 @@ CC_Register_INFO(Demo, b);
|
|||||||
int main() {
|
int main() {
|
||||||
const auto factory = CTL::ClassFactory::getInstance();
|
const auto factory = CTL::ClassFactory::getInstance();
|
||||||
const auto a = factory->create_class("Demo");
|
const auto a = factory->create_class("Demo");
|
||||||
|
a->set("a",2);
|
||||||
|
const auto as = a->get<int>("a");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user