// Copyright (c) OpenMMLab. All rights reserved. #ifndef MMDEPLOY_REGISTRY_H #define MMDEPLOY_REGISTRY_H #include #include #include #include #include #include "macro.h" #include "value.h" namespace mmdeploy { namespace detail { template struct get_return_type { using type = std::unique_ptr; }; template struct get_return_type> { using type = typename EntryType::type; }; template using get_return_type_t = typename get_return_type::type; } // namespace detail template class Creator; template <> class Creator { public: virtual ~Creator() = default; virtual const char *GetName() const = 0; virtual int GetVersion() const { return 0; } }; template class Creator : public Creator { public: using ReturnType = detail::get_return_type_t; public: virtual ReturnType Create(const Value &args) = 0; }; template class Registry; template <> class MMDEPLOY_API Registry { public: Registry(); ~Registry(); bool AddCreator(Creator &creator); Creator *GetCreator(const std::string &type, int version = 0); private: std::multimap *> entries_; }; template class Registry : public Registry { public: bool AddCreator(Creator &creator) { return Registry::AddCreator(creator); } Creator *GetCreator(const std::string &type, int version = 0) { auto creator = Registry::GetCreator(type, version); return static_cast *>(creator); } static Registry &Get(); private: Registry() = default; }; template class Registerer { public: Registerer() { Registry::Get().AddCreator(inst_); } private: CreatorType inst_; }; } // namespace mmdeploy #define MMDEPLOY_DECLARE_REGISTRY(EntryType) \ template <> \ Registry &Registry::Get(); #define MMDEPLOY_DEFINE_REGISTRY(EntryType) \ template <> \ MMDEPLOY_EXPORT Registry &Registry::Get() { \ static Registry v; \ return v; \ } #define REGISTER_MODULE(EntryType, CreatorType) \ static ::mmdeploy::Registerer g_register_##EntryType##_##CreatorType{}; #define DECLARE_AND_REGISTER_MODULE(base_type, module_name, version) \ class module_name##Creator : public ::mmdeploy::Creator { \ public: \ module_name##Creator() = default; \ ~module_name##Creator() = default; \ const char *GetName() const override { return #module_name; } \ int GetVersion() const override { return version; } \ \ std::unique_ptr Create(const Value &value) override { \ return std::make_unique(value); \ } \ }; \ REGISTER_MODULE(base_type, module_name##Creator); #endif // MMDEPLOY_REGISTRY_H