2021-12-07 10:57:55 +08:00
|
|
|
// Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
|
|
|
|
#ifndef MMDEPLOY_REGISTRY_H
|
|
|
|
#define MMDEPLOY_REGISTRY_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
#include "macro.h"
|
2021-12-07 10:57:55 +08:00
|
|
|
#include "value.h"
|
|
|
|
|
|
|
|
namespace mmdeploy {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename EntryType, typename = void>
|
|
|
|
struct get_return_type {
|
|
|
|
using type = std::unique_ptr<EntryType>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EntryType>
|
|
|
|
struct get_return_type<EntryType, std::void_t<typename EntryType::type>> {
|
|
|
|
using type = typename EntryType::type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EntryType>
|
|
|
|
using get_return_type_t = typename get_return_type<EntryType>::type;
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
template <class EntryType>
|
|
|
|
class Creator;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class Creator<void> {
|
|
|
|
public:
|
|
|
|
virtual ~Creator() = default;
|
|
|
|
virtual const char *GetName() const = 0;
|
|
|
|
virtual int GetVersion() const { return 0; }
|
|
|
|
};
|
|
|
|
|
2021-12-07 10:57:55 +08:00
|
|
|
template <typename EntryType>
|
2022-02-24 20:08:44 +08:00
|
|
|
class Creator : public Creator<void> {
|
2021-12-07 10:57:55 +08:00
|
|
|
public:
|
|
|
|
using ReturnType = detail::get_return_type_t<EntryType>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ReturnType Create(const Value &args) = 0;
|
|
|
|
};
|
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
template <class EntryType>
|
|
|
|
class Registry;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class MMDEPLOY_API Registry<void> {
|
2021-12-07 10:57:55 +08:00
|
|
|
public:
|
2022-02-24 20:08:44 +08:00
|
|
|
Registry();
|
2021-12-07 10:57:55 +08:00
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
~Registry();
|
|
|
|
|
|
|
|
bool AddCreator(Creator<void> &creator);
|
|
|
|
|
|
|
|
Creator<void> *GetCreator(const std::string &type, int version = 0);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::multimap<std::string, Creator<void> *> entries_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class EntryType>
|
|
|
|
class Registry : public Registry<void> {
|
|
|
|
public:
|
|
|
|
bool AddCreator(Creator<EntryType> &creator) { return Registry<void>::AddCreator(creator); }
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
Creator<EntryType> *GetCreator(const std::string &type, int version = 0) {
|
2022-02-24 20:08:44 +08:00
|
|
|
auto creator = Registry<void>::GetCreator(type, version);
|
|
|
|
return static_cast<Creator<EntryType> *>(creator);
|
2021-12-07 10:57:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
static Registry &Get();
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Registry() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EntryType, typename CreatorType>
|
|
|
|
class Registerer {
|
|
|
|
public:
|
|
|
|
Registerer() { Registry<EntryType>::Get().AddCreator(inst_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CreatorType inst_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mmdeploy
|
|
|
|
|
2022-02-24 20:08:44 +08:00
|
|
|
#define MMDEPLOY_DECLARE_REGISTRY(EntryType) \
|
|
|
|
template <> \
|
|
|
|
Registry<EntryType> &Registry<EntryType>::Get();
|
|
|
|
|
|
|
|
#define MMDEPLOY_DEFINE_REGISTRY(EntryType) \
|
|
|
|
template <> \
|
|
|
|
MMDEPLOY_EXPORT Registry<EntryType> &Registry<EntryType>::Get() { \
|
|
|
|
static Registry v; \
|
|
|
|
return v; \
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:57:55 +08:00
|
|
|
#define REGISTER_MODULE(EntryType, CreatorType) \
|
|
|
|
static ::mmdeploy::Registerer<EntryType, CreatorType> g_register_##EntryType##_##CreatorType{};
|
|
|
|
|
|
|
|
#define DECLARE_AND_REGISTER_MODULE(base_type, module_name, version) \
|
|
|
|
class module_name##Creator : public ::mmdeploy::Creator<base_type> { \
|
|
|
|
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<base_type> Create(const Value &value) override { \
|
|
|
|
return std::make_unique<module_name>(value); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
REGISTER_MODULE(base_type, module_name##Creator);
|
|
|
|
|
|
|
|
#endif // MMDEPLOY_REGISTRY_H
|