From 5ebf9528ee4e507fbae247db4b1552f4c21fb887 Mon Sep 17 00:00:00 2001 From: ninemine <106434473+NINEMINEsigma@users.noreply.github.com> Date: Mon, 16 Jun 2025 11:24:48 +0800 Subject: [PATCH] BS 0.0.1 --- Convention/[Runtime]/Config.hpp | 195 +++++++++++++++++++++ Convention/[Runtime]/Generics/Sequence.hpp | 41 +++++ 2 files changed, 236 insertions(+) diff --git a/Convention/[Runtime]/Config.hpp b/Convention/[Runtime]/Config.hpp index 57bc2f6..a01dc4f 100644 --- a/Convention/[Runtime]/Config.hpp +++ b/Convention/[Runtime]/Config.hpp @@ -1805,4 +1805,199 @@ namespace Convention #pragma endregion +#pragma region instance + +namespace Convention +{ + /** + * @brief 智能指针(共享) + */ + template + using SharedPtr = std::shared_ptr; + + /** + * @brief 交付给UniquePtr的删除器 + */ + template class _Alloc> + struct DefaultDelete + { + constexpr DefaultDelete() noexcept = default; + + template , int> = 0> + _CONSTEXPR23 DefaultDelete(const DefaultDelete<_Ty2, _Alloc>&) noexcept {} + + _CONSTEXPR23 void operator()(_Ty* _Ptr) const noexcept /* strengthened */ + { + // delete a pointer + static_assert(0 < sizeof(_Ty), "can't delete an incomplete type"); + static _Alloc<_Ty> alloc; + alloc.destroy(_Ptr); + alloc.deallocate(_Ptr, 1); + } + }; + + /** + * @brief 智能指针(完全所有权) + */ + template> + using UniquePtr = std::unique_ptr; + + /** + * @brief 智能指针(弱持有) + */ + template + using WeakPtr = std::weak_ptr; + + + /** + * @brief 支持内存控制的实体 + * @tparam T 目标类型 + * @tparam Allocator 内存管理器 + * @tparam IsUnique 指示智能指针类型 + */ + template< + typename T, + template class Allocator = std::allocator, + bool IsUnique = false + > + class instance + : public std::conditional_t< + IsUnique, + UniquePtr>, + SharedPtr + > + { + private: + using _SharedPtr = SharedPtr; + using _UniquePtr = UniquePtr>; + using _Mybase = std::conditional_t; + private: + /** + * @brief 获取内存管理器 + */ + static _MyAlloc& GetStaticMyAllocator() + { + static _MyAlloc alloc; + return alloc; + } + template + static T* BuildMyPtr(Args&&... args) + { + T* ptr = GetStaticMyAllocator().allocate(1); + GetStaticMyAllocator().construct(ptr, std::forward(args)...); + return ptr + } + static void _DestoryMyPtr(_In_ T* ptr) + { + GetStaticMyAllocator().destroy(ptr); + GetStaticMyAllocator().deallocate(ptr, 1); + } + public: + /** + * @brief 任意匹配的构造函数 + */ + template + instance(Args&&... args) : _Mybase(_UniquePtr(std::forward(args)...)) {} + virtual ~instance() {} + + /** + * @brief 是否为空指针 + */ + bool IsEmpty() const noexcept + { + return this->get() != nullptr; + } + + /** + * @brief 读取值(引用方式) + */ + T& ReadValue() + { + return *(this->get()); + } + using _MyMoveableOther = std::conditional_t>&&, SharedPtr>; + instance& WriteValue(_MyMoveableOther ptr) + { + if constexpr (IsUnique) + *this = std::move(ptr); + else + *this = ptr; + return *this; + } + /** + * @brief 设置值(引用方式) + * @tparam Arg 传递值 + */ + template, size_t> = 0> + T& WriteValue(Arg&& value) + { + if (this->IsEmpty()) + { + *this = _UniquePtr(BuildMyPtr(std::forward(value))); + } + else + { + *(this->get()) = std::forward(value); + } + return this->ReadValue(); + } + /** + * @brief 读取const值(引用方式) + */ + const T& ReadConstValue() const + { + return *(this->get()); + } + + /** + * @brief 拷贝赋值函数 + */ + virtual instance& operator=(const instance& value) noexcept + { + if constexpr (IsUnique) + { + this->WriteValue(value.ReadConstValue()); + } + else + { + _Mybase::operator=(value); + } + return *this; + } + /** + * @brief 移动赋值函数 + */ + virtual instance& operator=(instance&& value) noexcept + { + _Mybase::operator=(std::move(value)); + return *this; + } + }; + + /** + * @brief 类栈实体 + * @tparam T 目标类型 + * @tparam Allocator 内存管理器 + */ + template< + typename T, + template class Allocator = std::allocator + > + using meta = instance; + + /** + * @brief 类引用实体 + * @tparam T 目标类型 + * @tparam Allocator 内存管理器 + */ + template< + typename T, + template class Allocator = std::allocator + > + using object = instance; +} + +#pragma endregion + + #endif // !Convention_Runtime_Config_hpp diff --git a/Convention/[Runtime]/Generics/Sequence.hpp b/Convention/[Runtime]/Generics/Sequence.hpp index 6bc4928..a6c886f 100644 --- a/Convention/[Runtime]/Generics/Sequence.hpp +++ b/Convention/[Runtime]/Generics/Sequence.hpp @@ -18,6 +18,47 @@ namespace Convention namespace Generics { + namespace Collections + { + + /** + * @brief 支持可枚举类型的迭代 + * @tparam ReadValueType 读出元素的类型 + * @version BS 0.0.1 + */ + template + struct IEnumerator + { + virtual ~IEnumerator() {} + /** + * @brief 获取位于枚举器当前位置中的元素 + * @return The element in the collection at the current position of the enumerator. + */ + virtual ReadValueType ReadValue() const abstract; + + /** + * @brief 将枚举器前进到下一个元素 + * + * @return 如果枚举器成功前进到下一个元素返回true, + * 否则枚举器到达无效的位置或枚举器已失效 + */ + virtual bool MoveNext() abstract; + }; + + /** + * @brief 可枚举对象 + * @tparam Enumerator 枚举器 + * @version BS 0.0.1 + */ + template + struct IEnumerable + { + virtual Enumerator begin() abstract; + virtual Enumerator end() abstract; + }; + + } + /** * @brief 序列性状, 用于静态判断 * @tparam Sequence 序列类型