5 #ifndef CAFFE2_CORE_REGISTRY_H_ 6 #define CAFFE2_CORE_REGISTRY_H_ 15 #include "caffe2/core/common.h" 16 #include "caffe2/core/typeid.h" 30 template <
class SrcType,
class ObjectType,
class... Args>
33 typedef std::function<std::unique_ptr<ObjectType> (Args ...)> Creator;
37 void Register(
const SrcType& key, Creator creator) {
44 std::lock_guard<std::mutex> lock(register_mutex_);
45 if (registry_.count(key) != 0) {
46 std::cerr <<
"Key " << key <<
" already registered." << std::endl;
49 registry_[key] = creator;
52 void Register(
const SrcType& key, Creator creator,
const string& help_msg) {
53 Register(key, creator);
54 help_message_[key] = help_msg;
57 inline bool Has(
const SrcType& key) {
return (registry_.count(key) != 0); }
59 unique_ptr<ObjectType> Create(
const SrcType& key, Args ... args) {
60 if (registry_.count(key) == 0) {
64 return registry_[key](args...);
72 for (
const auto& it : registry_) {
73 keys.push_back(it.first);
78 const CaffeMap<SrcType, string>& HelpMessage()
const {
82 const char* HelpMessage(
const SrcType& key)
const {
83 auto it = help_message_.find(key);
84 if (it == help_message_.end()) {
87 return it->second.c_str();
91 CaffeMap<SrcType, Creator> registry_;
92 CaffeMap<SrcType, string> help_message_;
93 std::mutex register_mutex_;
98 template <
class SrcType,
class ObjectType,
class... Args>
103 typename Registry<SrcType, ObjectType, Args...>::Creator creator,
104 const string& help_msg=
"") {
105 registry->Register(key, creator, help_msg);
108 template <
class DerivedType>
109 static unique_ptr<ObjectType> DefaultCreator(Args ... args) {
114 return std::unique_ptr<ObjectType>(
new DerivedType(args...));
123 #define CAFFE_CONCATENATE_IMPL(s1, s2) s1##s2 124 #define CAFFE_CONCATENATE(s1, s2) CAFFE_CONCATENATE_IMPL(s1, s2) 126 #define CAFFE_ANONYMOUS_VARIABLE(str) CAFFE_CONCATENATE(str, __COUNTER__) 128 #define CAFFE_ANONYMOUS_VARIABLE(str) CAFFE_CONCATENATE(str, __LINE__) 136 #define CAFFE_DECLARE_TYPED_REGISTRY(RegistryName, SrcType, ObjectType, ...) \ 137 Registry<SrcType, ObjectType, ##__VA_ARGS__>* RegistryName(); \ 138 typedef Registerer<SrcType, ObjectType, ##__VA_ARGS__> \ 139 Registerer##RegistryName; 141 #define CAFFE_DEFINE_TYPED_REGISTRY(RegistryName, SrcType, ObjectType, ...) \ 142 Registry<SrcType, ObjectType, ##__VA_ARGS__>* RegistryName() { \ 143 static Registry<SrcType, ObjectType, ##__VA_ARGS__>* registry = \ 144 new Registry<SrcType, ObjectType, ##__VA_ARGS__>(); \ 150 #define CAFFE_REGISTER_TYPED_CREATOR(RegistryName, key, ...) \ 152 static Registerer##RegistryName CAFFE_ANONYMOUS_VARIABLE(g_##RegistryName)( \ 153 key, RegistryName(), __VA_ARGS__); \ 156 #define CAFFE_REGISTER_TYPED_CLASS(RegistryName, key, ...) \ 158 static Registerer##RegistryName CAFFE_ANONYMOUS_VARIABLE(g_##RegistryName)( \ 161 Registerer##RegistryName::DefaultCreator<__VA_ARGS__>, \ 162 TypeMeta::Name<__VA_ARGS__>()); \ 168 #define CAFFE_DECLARE_REGISTRY(RegistryName, ObjectType, ...) \ 169 CAFFE_DECLARE_TYPED_REGISTRY( \ 170 RegistryName, std::string, ObjectType, ##__VA_ARGS__) 172 #define CAFFE_DEFINE_REGISTRY(RegistryName, ObjectType, ...) \ 173 CAFFE_DEFINE_TYPED_REGISTRY( \ 174 RegistryName, std::string, ObjectType, ##__VA_ARGS__) 179 #define CAFFE_REGISTER_CREATOR(RegistryName, key, ...) \ 180 CAFFE_REGISTER_TYPED_CREATOR(RegistryName, #key, __VA_ARGS__) 182 #define CAFFE_REGISTER_CLASS(RegistryName, key, ...) \ 183 CAFFE_REGISTER_TYPED_CLASS(RegistryName, #key, __VA_ARGS__) 186 #endif // CAFFE2_CORE_REGISTRY_H_ A template class that allows one to register classes by keys.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
vector< SrcType > Keys()
Returns the keys currently registered as a vector.