From f1b5b15cd0260b5ea244208e6b6319bb75773bc6 Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Tue, 17 Dec 2024 00:38:21 +0800 Subject: [PATCH] feat: add TypedStorage --- src/ll/api/base/Alias.h | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/ll/api/base/Alias.h b/src/ll/api/base/Alias.h index 9736896cc4..6511739dab 100644 --- a/src/ll/api/base/Alias.h +++ b/src/ll/api/base/Alias.h @@ -6,37 +6,43 @@ #include namespace ll { -template -struct CArray { - using type = T[N]; -}; -template -struct CArray { - using type = T[]; -}; -template -using CArrayT = typename CArray::type; -template +template struct UntypedStorage { - alignas(Align) std::byte data[Len]; + std::byte data[Size]; template [[nodiscard]] T& as() & { - return *reinterpret_cast(this); + return *reinterpret_cast(data); } template [[nodiscard]] T const& as() const& { - return *reinterpret_cast(this); + return *reinterpret_cast(data); } template [[nodiscard]] T&& as() && { - return std::move(*reinterpret_cast(this)); + return std::move(*reinterpret_cast(data)); } template [[nodiscard]] T const&& as() const&& { - return std::move(*reinterpret_cast(this)); + return std::move(*reinterpret_cast(data)); } }; +template +struct TypedStorage { + std::byte data[Size]; + + [[nodiscard]] T* operator->() { return reinterpret_cast(data); } + [[nodiscard]] T const* operator->() const { return reinterpret_cast(data); } + [[nodiscard]] T& get() & { return *reinterpret_cast(data); } + [[nodiscard]] T const& get() const& { return *reinterpret_cast(data); } + [[nodiscard]] T&& get() && { return std::move(*reinterpret_cast(data)); } + [[nodiscard]] T const&& get() const&& { return std::move(*reinterpret_cast(data)); } + [[nodiscard]] T& operator*() & { return get(); } + [[nodiscard]] T const& operator*() const& { return get(); } + [[nodiscard]] T&& operator*() && { return std::move(get()); } + [[nodiscard]] T const&& operator*() const&& { return std::move(get()); } +}; + } // namespace ll