From 60c1bcbe9550bfef7c1362de2e075d8d2cbccc27 Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 26 Jul 2023 21:34:12 +0800 Subject: [PATCH] refactor: automatic to_json and from_json function by macros. --- WeaselIPC/ContextUpdater.cpp | 23 +-------- WeaselIPC/Styler.cpp | 97 ++---------------------------------- include/WeaselCommon.h | 53 ++++++++++---------- 3 files changed, 34 insertions(+), 139 deletions(-) diff --git a/WeaselIPC/ContextUpdater.cpp b/WeaselIPC/ContextUpdater.cpp index 049ebe022..704205761 100644 --- a/WeaselIPC/ContextUpdater.cpp +++ b/WeaselIPC/ContextUpdater.cpp @@ -13,27 +13,8 @@ namespace weasel { void from_json(const json& j, std::wstring& w) { w = string_to_wstring(j.get(), CP_UTF8); } - void to_json(json& j, const TextAttributeType& tat) { - switch (tat) { - case TextAttributeType::NONE: - j = "none"; - break; - case TextAttributeType::HIGHLIGHTED: - j = "highlighted"; - break; - case TextAttributeType::LAST_TYPE: - j = "last_type"; - break; - } - } - void from_json(const json& j, TextAttributeType& tat) { - if (j == "none") - tat = TextAttributeType::NONE; - else if (j == "highlighted") - tat = TextAttributeType::HIGHLIGHTED; - else if(j == "last_type") - tat = TextAttributeType::LAST_TYPE; - } + + DEFINE_ENUM_JSON_SERIALIZATION(TextAttributeType) // auto serialize and deserialize TextRange NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TextRange, start, end, cursor) // auto serialize and deserialize TextAttribute diff --git a/WeaselIPC/Styler.cpp b/WeaselIPC/Styler.cpp index 4bf558492..63d1d9aa4 100644 --- a/WeaselIPC/Styler.cpp +++ b/WeaselIPC/Styler.cpp @@ -6,98 +6,11 @@ using namespace weasel; namespace weasel { - void to_json(json& j, const UIStyle::AntiAliasMode& aam) { - switch(aam) { - case UIStyle::DEFAULT: - j = "default"; - break; - case UIStyle::CLEARTYPE: - j = "cleartype"; - break; - case UIStyle::GRAYSCALE: - j = "grayscale"; - break; - case UIStyle::ALIASED: - j = "aliased"; - break; - case UIStyle::FORCE_DWORD: - j = "force_dword"; - break; - } - } - void from_json(const json& j, UIStyle::AntiAliasMode& aam) { - if(j == "default") aam = UIStyle::DEFAULT; - else if(j == "cleartype") aam = UIStyle::CLEARTYPE; - else if(j == "grayscale") aam = UIStyle::GRAYSCALE; - else if(j == "aliased") aam = UIStyle::ALIASED; - else if(j == "force_dword") aam = UIStyle::FORCE_DWORD; - } - void to_json(json& j, const UIStyle::LayoutAlignType& lat) { - switch(lat){ - case UIStyle::ALIGN_TOP: - j = "top"; - break; - case UIStyle::ALIGN_CENTER: - j = "center"; - break; - case UIStyle::ALIGN_BOTTOM: - j = "bottom"; - break; - } - } - void from_json(const json& j, UIStyle::LayoutAlignType& lat) { - if(j == "top") lat = UIStyle::ALIGN_TOP; - else if(j == "center") lat = UIStyle::ALIGN_CENTER; - else if(j == "bottom") lat = UIStyle::ALIGN_BOTTOM; - } - void to_json(json& j, const UIStyle::LayoutType& lt) { - switch(lt){ - case UIStyle::LAYOUT_VERTICAL: - j = "vertical"; - break; - case UIStyle::LAYOUT_HORIZONTAL: - j = "horizontal"; - break; - case UIStyle::LAYOUT_VERTICAL_TEXT: - j = "vertical_text"; - break; - case UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN: - j = "horizontal+fullscreen"; - break; - case UIStyle::LAYOUT_VERTICAL_FULLSCREEN: - j = "vertical+fullscreen"; - break; - case UIStyle::LAYOUT_TYPE_LAST: - j = "last_type"; - break; - } - } - void from_json(const json& j, UIStyle::LayoutType& lt) { - if(j == "vertical") lt = UIStyle::LAYOUT_VERTICAL; - else if(j == "horizontal") lt = UIStyle::LAYOUT_HORIZONTAL; - else if(j == "vertical_text") lt = UIStyle::LAYOUT_VERTICAL_TEXT; - else if(j == "horizontal+fullscreen") lt = UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN; - else if(j == "vertical+fullscreen") lt = UIStyle::LAYOUT_VERTICAL_FULLSCREEN; - else if(j == "last_type") lt = UIStyle::LAYOUT_TYPE_LAST; - } - void to_json(json& j, const UIStyle::PreeditType& pt) { - switch(pt){ - case UIStyle::COMPOSITION: - j = "composition"; - break; - case UIStyle::PREVIEW: - j = "preview"; - break; - case UIStyle::PREVIEW_ALL: - j = "preview_all"; - break; - } - } - void from_json(const json& j, UIStyle::PreeditType& pt) { - if(j == "composition") pt = UIStyle::COMPOSITION; - else if(j == "preview") pt = UIStyle::PREVIEW; - else if(j == "preview_all") pt = UIStyle::PREVIEW_ALL; - } + DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::AntiAliasMode) + DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::PreeditType) + DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::LayoutType) + DEFINE_ENUM_JSON_SERIALIZATION(UIStyle::LayoutAlignType) + // auto serialize and deserialize ColorScheme NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ColorScheme, \ text_color, candidate_text_color, candidate_back_color,\ diff --git a/include/WeaselCommon.h b/include/WeaselCommon.h index 11a3c4dd6..e5f64129d 100644 --- a/include/WeaselCommon.h +++ b/include/WeaselCommon.h @@ -13,6 +13,20 @@ using json = nlohmann::json; namespace weasel { +#define DEFINE_ENUM_JSON_SERIALIZATION(EnumType) \ + void to_json(json& j, const EnumType& value) \ + { \ + j = static_cast(value); \ + } \ + void from_json(const json& j, EnumType& value) \ + { \ + value = static_cast(j.get()); \ + } +// macro for easily define extern prototype +#define EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TypeName) \ + extern void to_json(json& j, const TypeName& var);\ + extern void from_json(const json& j, TypeName& var); + enum TextAttributeType { NONE = 0, @@ -498,31 +512,18 @@ namespace weasel }; // UIStyle vs json - extern void to_json(json& j, const UIStyle::AntiAliasMode& aam); - extern void from_json(const json& j, UIStyle::AntiAliasMode& aam); - extern void to_json(json& j, const UIStyle::LayoutAlignType& lat); - extern void from_json(const json& j, UIStyle::LayoutAlignType& lat); - extern void to_json(json& j, const UIStyle::LayoutType& lt); - extern void from_json(const json& j, UIStyle::LayoutType& lt); - extern void to_json(json& j, const UIStyle::PreeditType& pt); - extern void from_json(const json& j, UIStyle::PreeditType& pt); - extern void to_json(json& j, const ColorScheme& color); - extern void from_json(const json& j, ColorScheme& color); - extern void to_json(json& j, const UIStyle& style); - extern void from_json(const json& j, UIStyle& style); + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::AntiAliasMode) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::LayoutAlignType) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::LayoutType) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle::PreeditType) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(ColorScheme) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(UIStyle) // Context vs json - extern void to_json(json& j, const std::wstring& w); - extern void from_json(const json& j, std::wstring& w); - extern void to_json(json& j, const TextAttributeType& tat); - extern void from_json(const json& j, TextAttributeType& tat); - extern void to_json(json& j, const TextAttribute& ta); - extern void from_json(const json& j, TextAttribute& ta); - extern void to_json(json& j, const TextRange& tr); - extern void from_json(const json& j, TextRange& tr); - extern void to_json(json& j, const Text& t); - extern void from_json(const json& j, Text& t); - extern void to_json(json& j, const CandidateInfo& ci); - extern void from_json(const json& j, CandidateInfo& ci); - extern void to_json(json& j, const Context& ctx); - extern void from_json(const json& j, Context& ctx); + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(std::wstring) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextAttributeType) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextAttribute) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(TextRange) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(Text) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(CandidateInfo) + EXTERN_PROTOTYPE_SERIALIZE_DESERIALIZE(Context) }