From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/ui/mapper/MapperRegistry.h | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 include/cru/ui/mapper/MapperRegistry.h (limited to 'include/cru/ui/mapper/MapperRegistry.h') diff --git a/include/cru/ui/mapper/MapperRegistry.h b/include/cru/ui/mapper/MapperRegistry.h new file mode 100644 index 00000000..29d26156 --- /dev/null +++ b/include/cru/ui/mapper/MapperRegistry.h @@ -0,0 +1,58 @@ +#pragma once + +#include "Mapper.h" + +#include +#include + +namespace cru::ui::mapper { +class CRU_UI_API MapperRegistry { + public: + static MapperRegistry* GetInstance(); + + MapperRegistry(); + + CRU_DELETE_COPY(MapperRegistry) + CRU_DELETE_MOVE(MapperRegistry) + + ~MapperRegistry(); + + const std::vector& GetAllMappers() const { return mapper_list_; } + + template + BasicMapper* GetMapper() const { + for (auto mapper : mapper_list_) { + if (mapper->GetTypeIndex() == typeid(T)) { + return static_cast*>(mapper); + } + } + return nullptr; + } + + template + BasicRefMapper* GetRefMapper() const { + return GetMapper>(); + } + + template + BasicPtrMapper* GetPtrMapper() const { + 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); + + private: + std::vector mapper_list_; +}; +} // namespace cru::ui::mapper -- cgit v1.2.3