From 1cc6e51838fcd5fc986dbb3e13bbb85f11a348be Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 24 Jan 2022 17:52:47 +0800 Subject: ... --- include/cru/ui/mapper/Mapper.hpp | 21 ++++++++++--------- include/cru/ui/mapper/MapperRegistry.hpp | 10 +++++++++ include/cru/ui/mapper/style/AndConditionMapper.hpp | 24 ++++++++++++++++++++++ .../cru/ui/mapper/style/FocusConditionMapper.hpp | 9 +++++++- include/cru/ui/mapper/style/IConditionMapper.hpp | 17 +++++++++++++++ include/cru/ui/mapper/style/NoConditionMapper.hpp | 9 +++++++- include/cru/ui/mapper/style/OrConditionMapper.hpp | 24 ++++++++++++++++++++++ include/cru/ui/style/Condition.hpp | 10 +++++++++ 8 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 include/cru/ui/mapper/style/AndConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/IConditionMapper.hpp create mode 100644 include/cru/ui/mapper/style/OrConditionMapper.hpp (limited to 'include/cru/ui') diff --git a/include/cru/ui/mapper/Mapper.hpp b/include/cru/ui/mapper/Mapper.hpp index 5febb26e..ceda8e0f 100644 --- a/include/cru/ui/mapper/Mapper.hpp +++ b/include/cru/ui/mapper/Mapper.hpp @@ -12,7 +12,7 @@ namespace cru::ui::mapper { template -class Mapper; +class BasicMapper; class CRU_UI_API MapperBase : public Object { public: @@ -27,13 +27,19 @@ class CRU_UI_API MapperBase : public Object { std::type_index GetTypeIndex() const { return type_index_; } template - Mapper* StaticCast() { - return static_cast*>(this); + BasicMapper* StaticCast() { + return static_cast*>(this); } template - Mapper* DynamicCast() { - return dynamic_cast*>(this); + BasicMapper* DynamicCast() { + return dynamic_cast*>(this); + } + + virtual bool SupportMapFromString() { return false; } + virtual bool SupportMapFromXml() { return false; } + virtual bool XmlElementIsOfThisType(xml::XmlElementNode* node) { + return false; } private: @@ -53,7 +59,6 @@ class CRU_UI_API BasicMapper : public MapperBase { ~BasicMapper() override = default; - virtual bool SupportMapFromString() { return false; } virtual T MapFromString(String str) { if (!SupportMapFromString()) { throw Exception(u"This mapper does not support map from string."); @@ -62,10 +67,6 @@ class CRU_UI_API BasicMapper : public MapperBase { return DoMapFromString(str); } - virtual bool SupportMapFromXml() { return false; } - virtual bool XmlElementIsOfThisType(xml::XmlElementNode* node) { - return false; - } T MapFromXml(xml::XmlElementNode* node) { if (!SupportMapFromXml()) { throw new Exception(u"This mapper does not support map from xml."); diff --git a/include/cru/ui/mapper/MapperRegistry.hpp b/include/cru/ui/mapper/MapperRegistry.hpp index c3e5b4c4..1c02be49 100644 --- a/include/cru/ui/mapper/MapperRegistry.hpp +++ b/include/cru/ui/mapper/MapperRegistry.hpp @@ -39,6 +39,16 @@ class CRU_UI_API MapperRegistry { return GetMapper>(); } + template + std::vector GetMappersByInterface() const { + std::vector result; + for (auto mapper : mapper_list_) { + auto m = dynamic_cast(mapper); + if (m) result.push_back(m); + } + return result; + } + void RegisterMapper(MapperBase* mapper); void UnregisterMapper(MapperBase* mapper); diff --git a/include/cru/ui/mapper/style/AndConditionMapper.hpp b/include/cru/ui/mapper/style/AndConditionMapper.hpp new file mode 100644 index 00000000..ef6ecdd8 --- /dev/null +++ b/include/cru/ui/mapper/style/AndConditionMapper.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../Mapper.hpp" +#include "IConditionMapper.hpp" + +namespace cru::ui::mapper::style { +class AndConditionMapper : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(AndConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/FocusConditionMapper.hpp b/include/cru/ui/mapper/style/FocusConditionMapper.hpp index bf84304d..974a6739 100644 --- a/include/cru/ui/mapper/style/FocusConditionMapper.hpp +++ b/include/cru/ui/mapper/style/FocusConditionMapper.hpp @@ -1,9 +1,11 @@ #pragma once #include "../Mapper.hpp" +#include "IConditionMapper.hpp" #include "cru/ui/style/Condition.hpp" namespace cru::ui::mapper::style { -class FocusConditionMapper : public BasicPtrMapper { +class FocusConditionMapper : public BasicPtrMapper, + public virtual IConditionMapper { public: CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(FocusConditionMapper) @@ -11,6 +13,11 @@ class FocusConditionMapper : public BasicPtrMapper { bool SupportMapFromXml() override { return true; } bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + protected: ClonablePtr DoMapFromXml( xml::XmlElementNode* node) override; diff --git a/include/cru/ui/mapper/style/IConditionMapper.hpp b/include/cru/ui/mapper/style/IConditionMapper.hpp new file mode 100644 index 00000000..f5d7ad1e --- /dev/null +++ b/include/cru/ui/mapper/style/IConditionMapper.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "../../Base.hpp" +#include "cru/common/ClonablePtr.hpp" +#include "cru/ui/mapper/Mapper.hpp" +#include "cru/ui/style/Condition.hpp" +#include "cru/xml/XmlNode.hpp" + +namespace cru::ui::mapper::style { +struct IConditionMapper : virtual Interface { + bool XmlElementIsOfThisType(xml::XmlElementNode* node) { + return dynamic_cast(this)->XmlElementIsOfThisType(node); + } + + virtual ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) = 0; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/mapper/style/NoConditionMapper.hpp b/include/cru/ui/mapper/style/NoConditionMapper.hpp index 21d186b5..79985e89 100644 --- a/include/cru/ui/mapper/style/NoConditionMapper.hpp +++ b/include/cru/ui/mapper/style/NoConditionMapper.hpp @@ -1,12 +1,14 @@ #pragma once #include "../Mapper.hpp" +#include "IConditionMapper.hpp" #include "cru/common/Base.hpp" #include "cru/common/ClonablePtr.hpp" #include "cru/ui/style/Condition.hpp" #include "cru/xml/XmlNode.hpp" namespace cru::ui::mapper::style { -class NoConditionMapper : public BasicPtrMapper { +class NoConditionMapper : public BasicPtrMapper, + public virtual IConditionMapper { public: CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(NoConditionMapper) @@ -14,6 +16,11 @@ class NoConditionMapper : public BasicPtrMapper { bool SupportMapFromXml() override { return true; } bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + protected: ClonablePtr DoMapFromXml( xml::XmlElementNode* node) override; diff --git a/include/cru/ui/mapper/style/OrConditionMapper.hpp b/include/cru/ui/mapper/style/OrConditionMapper.hpp new file mode 100644 index 00000000..252d8340 --- /dev/null +++ b/include/cru/ui/mapper/style/OrConditionMapper.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../Mapper.hpp" +#include "IConditionMapper.hpp" + +namespace cru::ui::mapper::style { +class OrConditionMapper : public BasicPtrMapper, + public virtual IConditionMapper { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(OrConditionMapper) + + public: + bool SupportMapFromXml() override { return true; } + bool XmlElementIsOfThisType(xml::XmlElementNode* node) override; + + ClonablePtr MapConditionFromXml( + xml::XmlElementNode* node) override { + return MapFromXml(node); + } + + protected: + ClonablePtr DoMapFromXml( + xml::XmlElementNode* node) override; +}; +} // namespace cru::ui::mapper::style diff --git a/include/cru/ui/style/Condition.hpp b/include/cru/ui/style/Condition.hpp index d5cf16f2..89da81f6 100644 --- a/include/cru/ui/style/Condition.hpp +++ b/include/cru/ui/style/Condition.hpp @@ -48,6 +48,11 @@ class CompoundCondition : public Condition { class AndCondition : public CompoundCondition { public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new AndCondition(std::move(conditions))); + } + using CompoundCondition::CompoundCondition; bool Judge(controls::Control* control) const override; @@ -57,6 +62,11 @@ class AndCondition : public CompoundCondition { class OrCondition : public CompoundCondition { public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new OrCondition(std::move(conditions))); + } + using CompoundCondition::CompoundCondition; bool Judge(controls::Control* control) const override; -- cgit v1.2.3