commit 20374057310bdefae9e7f1289f28ef364c4c58a5 Author: qingjiao Date: Sat Dec 6 17:11:34 2025 +0800 V01 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Reflection.iml b/.idea/Reflection.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/Reflection.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..de4eee6 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,348 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4c02e7e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CCClassFactory.cpp b/CCClassFactory.cpp new file mode 100644 index 0000000..1ddb837 --- /dev/null +++ b/CCClassFactory.cpp @@ -0,0 +1,53 @@ +#include "CCClassFactory.h" + +CTL::ClassFactory::ClassFactory() { + +} + +CTL::ClassFactory::~ClassFactory() { + +} + +CTL::ClassFactory * CTL::ClassFactory::getInstance() { + static ClassFactory instance; + return &instance; +} + +void CTL::ClassFactory::register_class(const std::string &name, const std::function &create_func) { + m_map[name] = create_func; +} + +CTL::Object * CTL::ClassFactory::create_class(const std::string &name) { + const auto it = m_map.find(name); + if (it != m_map.end()) { + return it->second(); + } + return nullptr; +} + +void CTL::ClassFactory::register_info(const std::string &name, const std::string &infoName, + const std::string &type,const size_t offset) { + m_map_info[name].push_back(new ClassFactoryInfo(infoName, type, offset)); +} + +int CTL::ClassFactory::get_info_count(const std::string &className) { + return m_map_info[className].size(); +} + +CTL::ClassFactoryInfo * CTL::ClassFactory::get_info(const std::string &className, const int pos) { + const int size = m_map_info[className].size(); + if (pos < size || pos >= size) { + return nullptr; + } + return m_map_info[className][pos]; +} + +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) { + return item; + } + } + return nullptr; +} diff --git a/CCClassFactory.h b/CCClassFactory.h new file mode 100644 index 0000000..d95c89e --- /dev/null +++ b/CCClassFactory.h @@ -0,0 +1,36 @@ +#ifndef REFLECTION_CC_CLASS_FACTORY_H +#define REFLECTION_CC_CLASS_FACTORY_H + +#include +#include +#include +#include "iostream" +#include "CCClassFactoryInfo.h" +#include "CCObject.h" + +namespace CTL { + class ClassFactory { + std::map> m_map; + std::map> m_map_info; + ClassFactory(); + ~ClassFactory(); + public: + static ClassFactory* getInstance(); + void register_class(const std::string& name, const std::function &create_func); + Object* create_class(const std::string& name); + void register_info(const std::string& name, const std::string& infoName, const std::string& type,size_t offset); + int get_info_count(const std::string& className); + ClassFactoryInfo* get_info(const std::string& className, int pos); + ClassFactoryInfo* get_info(const std::string& className, const std::string& infoName); + }; + + #define CC_CREATE_CLASS(className) (className*)CTL::ClassFactory::getInstance()->create_class(#className) + + #define CC_CREATE_CLASS_FROM_STRING(classNameStr) \ + [](const std::string& str) -> void* { \ + return CTL::ClassFactory::getInstance()->create_class(str); \ + }(classNameStr) +} + + +#endif diff --git a/CCClassFactoryInfo.h b/CCClassFactoryInfo.h new file mode 100644 index 0000000..83b962c --- /dev/null +++ b/CCClassFactoryInfo.h @@ -0,0 +1,34 @@ +#ifndef REFLECTION_CC_CLASS_FACTORY_INFO_H +#define REFLECTION_CC_CLASS_FACTORY_INFO_H + +namespace CTL { + class ClassFactoryInfo { + + public: + //------------------------------------------------------------------------------------------- + // 默认构造函数 + ClassFactoryInfo() = default; + // 基本构造函数 + explicit ClassFactoryInfo(const std::string& name) { + Name = name; + } + explicit ClassFactoryInfo(const std::string& name, const std::string& typeName, const size_t offset) { + Name = name; + this->typeName = typeName; + this->offset = offset; + } + ~ClassFactoryInfo() { + + } + //------------------------------------------------------------------------------------------- + std::string Name; + std::string typeName; + size_t offset = 0; + //------------------------------------------------------------------------------------------- + private: + + //------------------------------------------------------------------------------------------- + }; +} + +#endif \ No newline at end of file diff --git a/CCClassRegister.cpp b/CCClassRegister.cpp new file mode 100644 index 0000000..7501819 --- /dev/null +++ b/CCClassRegister.cpp @@ -0,0 +1,14 @@ +#include "CCClassRegister.h" + + +CTL::ClassRegister::ClassRegister(const std::string &name, const std::function& create) { + ClassFactory::getInstance()->register_class(name,create); +} + +CTL::ClassRegister::ClassRegister(const std::string &name, const std::string &infoName, + const std::string &type, const size_t offset) { + const auto c = ClassFactory::getInstance(); + c->register_info(name,infoName,type,offset); +} + + diff --git a/CCClassRegister.h b/CCClassRegister.h new file mode 100644 index 0000000..220a3fa --- /dev/null +++ b/CCClassRegister.h @@ -0,0 +1,34 @@ +#ifndef REFLECTION_CC_CLASS_REGISTER_H +#define REFLECTION_CC_CLASS_REGISTER_H + +#include "CCClassFactory.h" +#include "CCObject.h" + +namespace CTL { + class ClassRegister { + public: + ClassRegister(const std::string &name, const std::function& create); + ClassRegister(const std::string& name, const std::string& infoName, const std::string& type,size_t offset); + }; + +#define CC_Register_CLASS(className) \ + inline CTL::Object * create_##className() \ + { \ + CTL::Object* obj = new className(); \ + return obj; \ + } \ + inline CTL::ClassRegister classRegister##className(#className, create_##className); + +#define CC_Register_INFO_t(className,infoName,infoType) \ + inline className className##infoName; \ + inline CTL::ClassRegister classRegister##className##infoName(#className,#infoName,#infoType,\ + (size_t)(&(className##infoName.infoName)) - (size_t)(&className##infoName)); + +#define CC_Register_INFO(className,infoName) \ + CC_Register_INFO_t(className, infoName, decltype(className::infoName)) + + + +} + +#endif \ No newline at end of file diff --git a/CCObject.cpp b/CCObject.cpp new file mode 100644 index 0000000..d7f0d03 --- /dev/null +++ b/CCObject.cpp @@ -0,0 +1,24 @@ +#include "CCObject.h" +#include "iostream" +#include +#include + + +bool CTL::Object::equals(const Object *other) { + return this == other; // 默认比较内存地址 +} + +size_t CTL::Object::hashCode() const noexcept { + return std::hash{}(this); +} + +std::string CTL::Object::toString() const { + char buffer[64]; + snprintf(buffer, sizeof(buffer), "Object@%p", static_cast(this)); + return buffer; +} + +std::ostream & CTL::operator<<(std::ostream &os, const Object &map) { + os << map.toString(); + return os; +} diff --git a/CCObject.h b/CCObject.h new file mode 100644 index 0000000..76d6cfb --- /dev/null +++ b/CCObject.h @@ -0,0 +1,43 @@ +#ifndef CC_OBJECT_H +#define CC_OBJECT_H +#pragma once + +#include +#include +#include +#include "mutex" +#include "CCClassFactoryInfo.h" + +namespace CTL { + class Object { + inline static std::mutex Mutex_Object; + std::string MemoryID; + public: + + private: + + public: + Object() = default; + virtual ~Object() = default; // 虚析构函数确保正确析构 + // 比较两个对象是否逻辑相等(默认实现为地址比较) + virtual bool equals(const Object* other); + // 生成基于对象地址的哈希值 + [[nodiscard]] virtual size_t hashCode() const noexcept; + // 返回对象的字符串表示(默认格式:类名@地址) + [[nodiscard]] virtual std::string toString() const; + friend std::ostream& operator<<(std::ostream& os, const Object& map); + template + static std::shared_ptr create(Args&&... args) { + return std::make_shared(std::forward(args)...); + } + int get_info_count(); + ClassFactoryInfo* get_info(int pos); + ClassFactoryInfo* get_info(const std::string& infoName); + template + T get(const std::string& infoName); + template + void set(const std::string& infoName, T value); + }; +} + +#endif \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..92438f0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 4.0) +project(Reflection) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(Reflection main.cpp + CCClassFactory.cpp + CCClassFactory.h + CCClassRegister.cpp + CCClassRegister.h + CCClassFactoryInfo.h + CCObject.cpp + CCObject.h) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..13b0786 --- /dev/null +++ b/main.cpp @@ -0,0 +1,18 @@ +#include "CCClassFactory.h" +#include "CCClassRegister.h" + +class Demo :public CTL::Object{ +public: + int a = 0; + std::string b; +}; + +CC_Register_CLASS(Demo); +CC_Register_INFO(Demo, a); +CC_Register_INFO(Demo, b); + +int main() { + const auto factory = CTL::ClassFactory::getInstance(); + const auto a = factory->create_class("Demo"); + return 0; +}