From afb206c8d1f104688d29e7208d76309741e0a6c0 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Sun, 29 Jun 2025 21:49:20 +0800 Subject: [PATCH] =?UTF-8?q?BS=200.1.0=20File=E6=9C=AA=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Convention/[Runtime]/Architecture.hpp | 69 ++--- Convention/[Runtime]/Math.hpp | 52 ++-- Convention/[Runtime]/Plugins.hpp | 369 +------------------------- Runtime-Modules-README.md | 200 -------------- 4 files changed, 44 insertions(+), 646 deletions(-) delete mode 100644 Runtime-Modules-README.md diff --git a/Convention/[Runtime]/Architecture.hpp b/Convention/[Runtime]/Architecture.hpp index 3cc214f..d86b72d 100644 --- a/Convention/[Runtime]/Architecture.hpp +++ b/Convention/[Runtime]/Architecture.hpp @@ -74,60 +74,19 @@ namespace Convention } }; - template class ElementPtr = std::shared_ptr> class DependenceModel : public IConvertModel { private: - std::vector>> queries; + std::vector*> queries; public: - DependenceModel(std::vector>> queries) :__init(queries) {} + DependenceModel(std::vector*> queries) :__init(queries) {} bool ConvertTo() { for (auto&& query : queries) { - if ((*query).ConvertTo() == false) - return false; - } - return true; - } - - decltype(auto) begin() noexcept - { - return queries.begin(); - } - - decltype(auto) end() noexcept - { - return queries.end(); - } - - virtual void Load(std::string_view data) override - { - throw std::runtime_error("NotImplementedException"); - } - - virtual string Save() override - { - throw std::runtime_error("NotImplementedException"); - } - }; - - template<> - class DependenceModel - : public IConvertModel - { - private: - std::vector> queries; - public: - DependenceModel(std::vector> queries) :__init(queries) {} - - bool ConvertTo() - { - for (auto&& query : queries) - { - if (query.ConvertTo() == false) + if (query->ConvertTo() == false) return false; } return true; @@ -177,10 +136,13 @@ namespace Convention #pragma region Objects Registered private: + + std::map>>> ImplTypeQuery; + std::set RegisterHistory; std::map UncompleteTargets; std::map> Completer; - std::map> Dependences; + std::map Dependences; std::map Childs; class TypeQuery @@ -247,6 +209,7 @@ namespace Convention { Childs[complete] = UncompleteTargets[complete]; UncompleteTargets.erase(complete); + ImplTypeQuery.erase(complete); } } @@ -260,12 +223,14 @@ namespace Convention RegisterHistory.insert(slot.hash_code()); Completer[slot.hash_code()] = completer; UncompleteTargets[slot.hash_code()] = target; - std::vector>> dependenceModel; + std::vector*> dependenceModel; for (auto&& type : dependences) { - dependenceModel.push_back(std::make_shared(new TypeQuery(slot))); + auto cur = std::make_shared(new TypeQuery(slot)); + ImplTypeQuery[slot.hash_code()].push_back(cur); + dependenceModel.push_back(cur.get()); } - Dependences[slot.hash_code()] = DependenceModel<>(dependenceModel); + Dependences[slot.hash_code()] = DependenceModel(dependenceModel); std::set buffer; while (InternalRegisteringComplete(buffer)) InternalRegisteringUpdate(buffer); @@ -320,11 +285,11 @@ namespace Convention public: Listening(const std::function& action, TypeID type) - : __init(action),__init(type){ } + : __init(action), __init(type) {} Listening(const std::function& action, Type type) : __init(action), type(type.hash_code()) {} - void StopListening() + void StopListening() const { if (SingletonModel::Instance().SignalListener.count(type)) SingletonModel::Instance().SignalListener[type].erase(action); @@ -349,7 +314,7 @@ namespace Convention template Listening AddListener(std::enable_if_t, std::function> listener) { - return AddListener(typeof(Signal), listener); + return AddListener(typeid(Signal), listener); } void SendMessage(Type slot, const ISignal& signal) @@ -366,7 +331,7 @@ namespace Convention template void SendMessage(std::enable_if_t, const Signal&> signal) { - return SendMessage(signal.GetType(), signal); + return SendMessage(typeid(Signal), signal); } #pragma endregion diff --git a/Convention/[Runtime]/Math.hpp b/Convention/[Runtime]/Math.hpp index 3f0e93e..452c16b 100644 --- a/Convention/[Runtime]/Math.hpp +++ b/Convention/[Runtime]/Math.hpp @@ -8,43 +8,43 @@ namespace Convention { namespace Math { - // Mathematical constants - template - struct Constants - { - static constexpr T PI = static_cast(3.14159265358979323846); - static constexpr T E = static_cast(2.71828182845904523536); - static constexpr T SQRT2 = static_cast(1.41421356237309504880); - static constexpr T SQRT3 = static_cast(1.73205080756887729353); - static constexpr T LN2 = static_cast(0.69314718055994530942); - static constexpr T LN10 = static_cast(2.30258509299404568402); - }; - // Basic math utilities template - constexpr T Abs(const T& value) + inline T Abs(const T& value) { - return value < T{} ? -value : value; + return std::abs(value); } template - constexpr T Min(const T& a, const T& b) + inline T Min(const T& a, const T& b) { - return a < b ? a : b; + return std::min(a, b); } - template - constexpr T Max(const T& a, const T& b) - { - return a > b ? a : b; - } + template + inline T Min(const T& a, const Args&... args) + { + return std::min(a, Min(args...)); + } template - constexpr T Clamp(const T& value, const T& min, const T& max) + inline T Max(const T& a, const T& b) { - return Min(Max(value, min), max); + return std::max(a, b); } + template + inline T Max(const T& a, const Args&... args) + { + return std::max(a, Max(args...)); + } + + template + constexpr T Clamp(const T& value, const T2& min, const T3& max) + { + return Min(Max(value, min), max); + } + template constexpr T Sign(const T& value) { @@ -117,13 +117,13 @@ namespace Convention template constexpr T DegToRad(const T& degrees) { - return degrees * Constants::PI / static_cast(180); + return degrees * std::_Pi_val / static_cast(180); } template constexpr T RadToDeg(const T& radians) { - return radians * static_cast(180) / Constants::PI; + return radians * static_cast(180) / std::_Pi_val; } // Logarithmic functions @@ -291,4 +291,4 @@ namespace Convention } } -#endif // Convention_Runtime_Math_hpp \ No newline at end of file +#endif // Convention_Runtime_Math_hpp diff --git a/Convention/[Runtime]/Plugins.hpp b/Convention/[Runtime]/Plugins.hpp index 77b4174..4a622de 100644 --- a/Convention/[Runtime]/Plugins.hpp +++ b/Convention/[Runtime]/Plugins.hpp @@ -3,377 +3,10 @@ #define Convention_Runtime_Plugins_hpp #include "Config.hpp" -#include "Architecture.hpp" namespace Convention { - // Priority Queue implementation for plugin priorities - template - class PriorityQueue - { - public: - enum class Comparator - { - Less = -1, - Equal = 0, - Greater = 1 - }; - private: - std::vector elements; - std::function compare_func; - Comparator comparator; - - public: - PriorityQueue(Comparator comp = Comparator::Less, size_t capacity = 1) - : comparator(comp) - { - elements.reserve(std::max(capacity, size_t{1})); - - compare_func = [comp](const T& a, const T& b) -> int { - if constexpr (std::is_arithmetic_v) { - int result = (a > b) - (a < b); - return result * static_cast(comp); - } else { - if (a < b) return -1 * static_cast(comp); - if (b < a) return 1 * static_cast(comp); - return 0; - } - }; - } - - template - PriorityQueue(Compare comp, size_t capacity = 1) - : compare_func(comp) - { - elements.reserve(std::max(capacity, size_t{1})); - } - - // Basic properties - size_t Size() const { return elements.size(); } - size_t Count() const { return Size(); } - size_t Capacity() const { return elements.capacity(); } - bool IsEmpty() const { return elements.empty(); } - - const T& Top() const - { - if (IsEmpty()) throw std::runtime_error("Queue is empty"); - return elements[0]; - } - - // Core operations - void Enqueue(const T& value) - { - elements.push_back(value); - ShiftUp(elements.size() - 1); - } - - void Enqueue(T&& value) - { - elements.push_back(std::move(value)); - ShiftUp(elements.size() - 1); - } - - T Dequeue() - { - if (IsEmpty()) throw std::runtime_error("Queue is empty"); - - T result = std::move(elements[0]); - elements[0] = std::move(elements.back()); - elements.pop_back(); - - if (!IsEmpty()) { - ShiftDown(0); - } - - return result; - } - - bool TryDequeue(T& result) - { - if (IsEmpty()) { - return false; - } - result = Dequeue(); - return true; - } - - const T& Peek() const - { - return Top(); - } - - // Utility functions - bool Contains(const T& item) const - { - return std::find(elements.begin(), elements.end(), item) != elements.end(); - } - - void Clear() - { - elements.clear(); - } - - std::vector ToArray() const - { - return elements; - } - - void TrimExcess() - { - if (elements.size() < elements.capacity() * 0.9) { - elements.shrink_to_fit(); - } - } - - void EnsureCapacity(size_t minCapacity) - { - if (elements.capacity() < minCapacity) { - elements.reserve(minCapacity); - } - } - - private: - void ShiftUp(size_t index) - { - while (index > 0) { - size_t parent = (index - 1) / 2; - if (compare_func(elements[index], elements[parent]) >= 0) break; - - std::swap(elements[index], elements[parent]); - index = parent; - } - } - - void ShiftDown(size_t index) - { - size_t size = elements.size(); - while (true) { - size_t left = 2 * index + 1; - size_t right = 2 * index + 2; - size_t smallest = index; - - if (left < size && compare_func(elements[left], elements[smallest]) < 0) { - smallest = left; - } - if (right < size && compare_func(elements[right], elements[smallest]) < 0) { - smallest = right; - } - - if (smallest == index) break; - - std::swap(elements[index], elements[smallest]); - index = smallest; - } - } - }; - - // Plugin base interface - class IPlugin - { - public: - virtual ~IPlugin() = default; - virtual std::string GetName() const = 0; - virtual std::string GetVersion() const = 0; - virtual bool Initialize() = 0; - virtual void Shutdown() = 0; - virtual int GetPriority() const { return 0; } - }; - - // Plugin information - struct PluginInfo - { - std::string name; - std::string version; - std::string description; - std::vector dependencies; - int priority = 0; - bool enabled = true; - }; - - // Plugin manager interface - class PluginManager - { - public: - static PluginManager& GetInstance() - { - static PluginManager instance; - return instance; - } - - // Plugin registration (interface only) - template - bool RegisterPlugin() - { - static_assert(std::is_base_of_v, "T must inherit from IPlugin"); - - // TODO: Implement plugin registration - // This would typically involve: - // 1. Creating instance of T - // 2. Calling Initialize() - // 3. Adding to plugin registry - // 4. Managing dependencies - - throw std::runtime_error("Plugin system implementation required"); - } - - bool LoadPlugin(const std::string& pluginPath) - { - // TODO: Implement dynamic plugin loading - // This would typically involve: - // 1. Loading shared library (.dll/.so/.dylib) - // 2. Finding plugin entry point - // 3. Creating plugin instance - // 4. Registering plugin - - throw std::runtime_error("Dynamic plugin loading implementation required"); - } - - bool UnloadPlugin(const std::string& pluginName) - { - // TODO: Implement plugin unloading - throw std::runtime_error("Plugin unloading implementation required"); - } - - std::vector GetLoadedPlugins() const - { - // TODO: Return list of loaded plugins - return {}; - } - - template - T* GetPlugin(const std::string& name) const - { - // TODO: Find plugin by name and cast to type T - throw std::runtime_error("Plugin retrieval implementation required"); - } - - bool IsPluginLoaded(const std::string& name) const - { - // TODO: Check if plugin is loaded - return false; - } - - void EnablePlugin(const std::string& name) - { - // TODO: Enable plugin - } - - void DisablePlugin(const std::string& name) - { - // TODO: Disable plugin - } - - // Plugin discovery - std::vector DiscoverPlugins(const std::string& directory) const - { - // TODO: Scan directory for plugin files - throw std::runtime_error("Plugin discovery implementation required"); - } - - // Event system integration - template - void BroadcastEvent(const EventType& event) - { - // TODO: Send event to all registered plugins - // Integration with Architecture's event system - } - - private: - std::map> loaded_plugins; - std::map plugin_info; - PriorityQueue> priority_queue; - - PluginManager() = default; - }; - - // Platform-specific plugin utilities - namespace Platform - { - // Windows-specific functionality (interface only) - class WindowsPlugin : public IPlugin - { - public: - std::string GetName() const override { return "WindowsPlugin"; } - std::string GetVersion() const override { return "1.0.0"; } - - bool Initialize() override - { - // TODO: Initialize Windows-specific features - return true; - } - - void Shutdown() override - { - // TODO: Cleanup Windows-specific resources - } - - // Windows-specific methods (interface only) - bool ShowMessageBox(const std::string& title, const std::string& message) - { - // TODO: Implement using Windows API - throw std::runtime_error("Windows API implementation required"); - } - - std::string GetSystemInfo() - { - // TODO: Get Windows system information - throw std::runtime_error("Windows API implementation required"); - } - }; - - // Linux-specific functionality (interface only) - class LinuxPlugin : public IPlugin - { - public: - std::string GetName() const override { return "LinuxPlugin"; } - std::string GetVersion() const override { return "1.0.0"; } - - bool Initialize() override - { - // TODO: Initialize Linux-specific features - return true; - } - - void Shutdown() override - { - // TODO: Cleanup Linux-specific resources - } - - // Linux-specific methods (interface only) - std::string GetSystemInfo() - { - // TODO: Get Linux system information - throw std::runtime_error("Linux API implementation required"); - } - }; - - // Factory for platform-specific plugins - std::shared_ptr CreatePlatformPlugin() - { - if (PlatformIndicator::IsPlatformWindows) { - return std::make_shared(); - } else if (PlatformIndicator::IsPlatformLinux) { - return std::make_shared(); - } - - return nullptr; - } - } - - // Plugin helper macros - #define DECLARE_PLUGIN(ClassName) \ - extern "C" Convention::IPlugin* CreatePlugin() { \ - return new ClassName(); \ - } \ - extern "C" void DestroyPlugin(Convention::IPlugin* plugin) { \ - delete plugin; \ - } - - #define REGISTER_PLUGIN(ClassName) \ - bool Register##ClassName() { \ - return Convention::PluginManager::GetInstance().RegisterPlugin(); \ - } } -#endif // Convention_Runtime_Plugins_hpp \ No newline at end of file +#endif // Convention_Runtime_Plugins_hpp diff --git a/Runtime-Modules-README.md b/Runtime-Modules-README.md deleted file mode 100644 index c1d120d..0000000 --- a/Runtime-Modules-README.md +++ /dev/null @@ -1,200 +0,0 @@ -# Convention-CPP Runtime 模块说明 - -本文档描述了基于 `Convention-Template` 为 `Convention-CPP` 实现的Runtime模块。 - -## 📁 模块结构 - -### 完整实现的模块 - -#### 1. **File.hpp** - 文件操作工具 -- **ToolFile类**: 面向对象的文件系统操作 -- **完整功能**: 文件读写、路径操作、目录管理 -- **链式操作**: 支持流畅的API调用 -- **跨平台**: 基于std::filesystem - -**核心功能**: -- 文件存在性检查、类型判断 -- 文本/二进制文件读写 -- 目录遍历和管理 -- 路径组合操作符 `|` -- 文件对话框接口(需平台实现) - -#### 2. **GlobalConfig.hpp** - 全局配置管理 -- **GlobalConfig类**: 配置文件管理和日志系统 -- **ProjectConfig类**: 项目级配置特化 -- **JSON支持**: 基于nlohmann/json -- **完整功能**: 配置读写、日志记录 - -**核心功能**: -- 键值对配置管理 -- 自动配置文件生成 -- 时间戳日志系统 -- 迭代器支持(foreach循环) -- 文件操作集成 - -#### 3. **Math.hpp** - 数学工具库 -- **完整实现**: 常用数学函数和工具 -- **模板化**: 支持多种数值类型 -- **高性能**: 基于标准库优化 - -**核心功能**: -- 数学常量和基础运算 -- 三角函数、指数对数函数 -- 插值函数(Lerp, InverseLerp) -- 随机数生成器 -- 浮点数比较工具 - -### 接口实现的模块 - -#### 4. **Web.hpp** - 网络工具模块 -- **ToolURL类**: URL解析和操作 -- **HttpClient类**: HTTP客户端接口 -- **接口状态**: 完整API设计,需HTTP库实现 - -**设计功能**: -- URL验证和属性解析 -- 文件类型检测 -- HTTP GET/POST方法 -- 异步下载功能 -- URL编码/解码 - -**实现要求**: 需要libcurl或类似HTTP库 - -#### 5. **Plugins.hpp** - 插件系统 -- **PriorityQueue类**: 完整的优先队列实现 -- **IPlugin接口**: 插件基类定义 -- **PluginManager类**: 插件管理器接口 - -**设计功能**: -- 动态插件加载/卸载 -- 插件依赖管理 -- 平台特定插件支持 -- 事件系统集成 -- 优先级队列调度 - -**实现要求**: 需要动态库加载机制 - -#### 6. **EasySave.hpp** - 序列化系统 -- **EasySave类**: 主要序列化接口 -- **EasySaveSettings**: 配置结构 -- **接口状态**: 基础JSON实现,完整功能需扩展 - -**设计功能**: -- 多格式序列化(JSON/Binary) -- 加密和压缩支持 -- 缓存系统 -- 备份/恢复机制 -- 键值管理 - -**实现要求**: 需要加密库和压缩库 - -### 继承的模块 - -#### 7. **Config.hpp** - 基础配置(已存在) -- **完整实现**: 平台判断、字符串工具、内存管理 -- **基础设施**: 为其他模块提供基础功能 - -#### 8. **Architecture.hpp** - 核心架构(已存在) -- **完整实现**: 依赖注入、事件系统、时间线管理 -- **设计模式**: 单例、依赖注入、观察者模式 - -## 🔧 编译配置 - -### 依赖库 -- **标准库**: C++17 std::filesystem -- **可选依赖**: nlohmann/json(配置管理) -- **待实现依赖**: - - libcurl(Web模块) - - OpenSSL/AES库(EasySave加密) - - zlib(EasySave压缩) - -### CMake配置 -已在主CMakeLists.txt中添加依赖查找: -```cmake -find_package(nlohmann_json QUIET) -``` - -## 🚀 使用示例 - -### 文件操作 -```cpp -#include "Convention/[Runtime]/File.hpp" -using namespace Convention; - -ToolFile file("data/config.txt"); -if (file.Exists()) { - auto content = file.LoadAsText(); - // 处理内容 -} - -// 链式操作 -ToolFile dataDir("assets"); -auto configFile = dataDir | "config" | "settings.json"; -``` - -### 配置管理 -```cpp -#include "Convention/[Runtime]/GlobalConfig.hpp" -using namespace Convention; - -GlobalConfig config("./data"); -config["username"] = "player1"; -config["level"] = 5; -config.SaveProperties(); - -// 日志记录 -config.Log("Info", "Game started"); -``` - -### 数学运算 -```cpp -#include "Convention/[Runtime]/Math.hpp" -using namespace Convention; - -auto result = Math::Sin(Math::DegToRad(90.0f)); -auto random_value = Math::RandomRange(1, 100); -bool is_equal = Math::Equal(3.14f, Math::Constants::PI, 0.01f); -``` - -## 📋 实现状态总结 - -| 模块 | 状态 | 完成度 | 备注 | -|------|------|--------|------| -| Config | ✅ 完整 | 100% | 已存在 | -| Architecture | ✅ 完整 | 100% | 已存在 | -| File | ✅ 完整 | 95% | 平台对话框需实现 | -| GlobalConfig | ✅ 完整 | 100% | 功能完整 | -| Math | ✅ 完整 | 100% | 功能完整 | -| Web | 🔧 接口 | 30% | 需HTTP库实现 | -| Plugins | 🔧 接口 | 40% | 需动态加载实现 | -| EasySave | 🔧 接口 | 20% | 需加密压缩实现 | - -## 🎯 下一步实现建议 - -### 高优先级 -1. **完善Web模块**: 集成libcurl实现HTTP功能 -2. **完善EasySave**: 实现完整的序列化系统 -3. **文件对话框**: 实现平台特定的文件选择功能 - -### 中优先级 -1. **插件系统**: 实现动态库加载机制 -2. **加密支持**: 为EasySave添加AES加密 -3. **压缩支持**: 为EasySave添加Gzip压缩 - -### 低优先级 -1. **性能优化**: 针对关键路径进行优化 -2. **单元测试**: 为各模块添加测试用例 -3. **文档完善**: 添加详细的API文档 - -## 📚 设计原则 - -本实现遵循以下设计原则: - -1. **模块化**: 每个模块功能独立,依赖关系清晰 -2. **类型安全**: 大量使用模板和类型检查 -3. **RAII**: 资源管理遵循RAII原则 -4. **异常安全**: 提供清晰的错误处理机制 -5. **跨平台**: 基于标准库,支持主要平台 -6. **向前兼容**: 接口设计考虑未来扩展性 - -这个实现为Convention-CPP提供了完整的Runtime基础设施,为后续开发提供了坚实的基础。 \ No newline at end of file