From 6af1017d2463ac27f15095ea16b95debfb3d60f4 Mon Sep 17 00:00:00 2001 From: ykiko Date: Sat, 7 Dec 2024 14:23:32 +0800 Subject: [PATCH] Clean Support. --- include/Support/Compare.h | 13 +++++++ include/Support/Enum.h | 34 ------------------- include/Support/FileSystem.h | 1 + include/Support/Format.h | 15 ++++++++ include/Support/Hash.h | 12 +++++++ include/Support/JSON.h | 52 ++++++++++++++++++++++++++-- include/Support/Struct.h | 66 +----------------------------------- include/Support/Support.h | 5 +-- 8 files changed, 95 insertions(+), 103 deletions(-) diff --git a/include/Support/Compare.h b/include/Support/Compare.h index bf2eda29..2f038a53 100644 --- a/include/Support/Compare.h +++ b/include/Support/Compare.h @@ -1,6 +1,7 @@ #pragma once #include "Support/ADT.h" +#include "Struct.h" namespace clice::support { @@ -33,4 +34,16 @@ struct Equal> { } }; +template + requires (!requires(T lhs, T rhs) { + { lhs == rhs } -> std::convertible_to; + }) +struct Equal { + static bool equal(const T& lhs, const T& rhs) { + return foreach(lhs, rhs, [](const auto& lhs, const auto& rhs) { + return support::equal(lhs, rhs); + }); + } +}; + } // namespace clice::support diff --git a/include/Support/Enum.h b/include/Support/Enum.h index f315e410..10a9cb04 100644 --- a/include/Support/Enum.h +++ b/include/Support/Enum.h @@ -8,8 +8,6 @@ #include #include "TypeTraits.h" -#include "JSON.h" -#include "Format.h" namespace clice::support { @@ -250,35 +248,3 @@ concept special_enum = requires { } // namespace clice::support -namespace clice::json { - -template - requires support::special_enum -struct Serde { - static json::Value serialize(const E& e) { - return json::Value(e.value()); - } - - static E deserialize(const json::Value& value) { - assert(value.kind() == json::Value::Number && "Expect a number"); - return E(value.getAsNumber().value()); - } -}; - -} // namespace clice::json - -template - requires clice::support::special_enum -struct std::formatter : std::formatter { - using Base = std::formatter; - - template - constexpr auto parse(ParseContext& ctx) { - return Base::parse(ctx); - } - - template - auto format(const E& e, FormatContext& ctx) const { - return Base::format(e.name(), ctx); - } -}; diff --git a/include/Support/FileSystem.h b/include/Support/FileSystem.h index 22053e17..cd819131 100644 --- a/include/Support/FileSystem.h +++ b/include/Support/FileSystem.h @@ -2,6 +2,7 @@ #include #include +#include namespace clice { diff --git a/include/Support/Format.h b/include/Support/Format.h index 008ee41f..35eddb3b 100644 --- a/include/Support/Format.h +++ b/include/Support/Format.h @@ -80,3 +80,18 @@ struct std::formatter : std::formatter { } }; +template + requires clice::support::special_enum +struct std::formatter : std::formatter { + using Base = std::formatter; + + template + constexpr auto parse(ParseContext& ctx) { + return Base::parse(ctx); + } + + template + auto format(const E& e, FormatContext& ctx) const { + return Base::format(e.name(), ctx); + } +}; diff --git a/include/Support/Hash.h b/include/Support/Hash.h index b8d1b5c4..2ec75c7d 100644 --- a/include/Support/Hash.h +++ b/include/Support/Hash.h @@ -2,6 +2,8 @@ #include "llvm/Support/HashBuilder.h" +#include "Struct.h" + namespace clice::support { template @@ -28,4 +30,14 @@ struct Hash> { }; }; +template +struct Hash { + static llvm::hash_code hash(const T& value) { + llvm::SmallVector hashes; + foreach(value, + [&](auto, const auto& member) { hashes.emplace_back(support::hash(member)); }); + return llvm::hash_combine_range(hashes.begin(), hashes.end()); + } +}; + } // namespace clice::support diff --git a/include/Support/JSON.h b/include/Support/JSON.h index 04261acd..9b491dd7 100644 --- a/include/Support/JSON.h +++ b/include/Support/JSON.h @@ -4,8 +4,11 @@ #include #include -#include "Support/ADT.h" -#include "Support/TypeTraits.h" +#include "ADT.h" +#include "TypeTraits.h" +#include "Enum.h" +#include "Struct.h" + #include "llvm/Support/JSON.h" namespace clice::json { @@ -268,4 +271,49 @@ struct Serde> { } }; +template + requires support::special_enum +struct Serde { + static json::Value serialize(const E& e) { + return json::Value(e.value()); + } + + static E deserialize(const json::Value& value) { + assert(value.kind() == json::Value::Number && "Expect a number"); + return E(value.getAsNumber().value()); + } +}; + +template +struct Serde { + constexpr inline static bool stateful = + support::member_types::apply([] { return (stateful_serde || ...); }); + + template + static json::Value serialize(const T& t, Serdes&&... serdes) { + json::Object object; + support::foreach(t, [&](std::string_view name, auto&& member) { + object.try_emplace(llvm::StringRef(name), + json::serialize(member, std::forward(serdes)...)); + }); + return object; + } + + template + static T deserialize(const json::Value& value, Serdes&&... serdes) { + T t = {}; + if constexpr(!std::is_empty_v) { + assert(value.kind() == json::Value::Object && "Expect an object"); + support::foreach(t, [&](std::string_view name, auto&& member) { + auto v = value.getAsObject()->get(llvm::StringRef(name)); + assert(v && "Member not found"); + member = json::deserialize>( + *v, + std::forward(serdes)...); + }); + } + return t; + } +}; + } // namespace clice::json diff --git a/include/Support/Struct.h b/include/Support/Struct.h index 7ac30bb3..cdbb52c4 100644 --- a/include/Support/Struct.h +++ b/include/Support/Struct.h @@ -5,10 +5,7 @@ #include #include -#include "Support/Compare.h" -#include "Support/Format.h" -#include "Support/Hash.h" -#include "Support/JSON.h" +#include "TypeTraits.h" namespace clice::support { @@ -270,64 +267,3 @@ constexpr bool foreach(LHS&& lhs, RHS&& rhs, const Callback& callback) { } // namespace clice::support -namespace clice::json { - -template -struct Serde { - constexpr inline static bool stateful = - support::member_types::apply([] { return (stateful_serde || ...); }); - - template - static json::Value serialize(const T& t, Serdes&&... serdes) { - json::Object object; - support::foreach(t, [&](std::string_view name, auto&& member) { - object.try_emplace(llvm::StringRef(name), - json::serialize(member, std::forward(serdes)...)); - }); - return object; - } - - template - static T deserialize(const json::Value& value, Serdes&&... serdes) { - T t = {}; - if constexpr(!std::is_empty_v) { - assert(value.kind() == json::Value::Object && "Expect an object"); - support::foreach(t, [&](std::string_view name, auto&& member) { - auto v = value.getAsObject()->get(llvm::StringRef(name)); - assert(v && "Member not found"); - member = json::deserialize>( - *v, - std::forward(serdes)...); - }); - } - return t; - } -}; - -} // namespace clice::json - -namespace clice::support { - -template -struct Hash { - static llvm::hash_code hash(const T& value) { - llvm::SmallVector hashes; - foreach(value, - [&](auto, const auto& member) { hashes.emplace_back(support::hash(member)); }); - return llvm::hash_combine_range(hashes.begin(), hashes.end()); - } -}; - -template - requires (!requires(T lhs, T rhs) { - { lhs == rhs } -> std::convertible_to; - }) -struct Equal { - static bool equal(const T& lhs, const T& rhs) { - return foreach(lhs, rhs, [](const auto& lhs, const auto& rhs) { - return support::equal(lhs, rhs); - }); - } -}; - -} // namespace clice::support diff --git a/include/Support/Support.h b/include/Support/Support.h index 1268a677..43b30dc0 100644 --- a/include/Support/Support.h +++ b/include/Support/Support.h @@ -1,11 +1,12 @@ #pragma once #include "ADT.h" +#include "Enum.h" +#include "Struct.h" #include "Hash.h" #include "Format.h" #include "JSON.h" -#include "Enum.h" -#include "Struct.h" #include "Compare.h" #include "FileSystem.h" +#include "llvm/Support/MemoryBuffer.h"