Medial Code Documentation
Loading...
Searching...
No Matches
registry.h
Go to the documentation of this file.
1
6#ifndef DMLC_REGISTRY_H_
7#define DMLC_REGISTRY_H_
8
9#include <map>
10#include <string>
11#include <vector>
12#include "./base.h"
13#include "./logging.h"
14#include "./parameter.h"
15#include "./type_traits.h"
16
17namespace dmlc {
26template<typename EntryType>
27class Registry {
28 public:
30 inline static const std::vector<const EntryType*>& List() {
31 return Get()->const_list_;
32 }
34 inline static std::vector<std::string> ListAllNames() {
35 const std::map<std::string, EntryType*> &fmap = Get()->fmap_;
36 typename std::map<std::string, EntryType*>::const_iterator p;
37 std::vector<std::string> names;
38 for (p = fmap.begin(); p !=fmap.end(); ++p) {
39 names.push_back(p->first);
40 }
41 return names;
42 }
48 inline static const EntryType *Find(const std::string &name) {
49 const std::map<std::string, EntryType*> &fmap = Get()->fmap_;
50 typename std::map<std::string, EntryType*>::const_iterator p = fmap.find(name);
51 if (p != fmap.end()) {
52 return p->second;
53 } else {
54 return NULL;
55 }
56 }
62 inline void AddAlias(const std::string& key_name,
63 const std::string& alias) {
64 EntryType* e = fmap_.at(key_name);
65 if (fmap_.count(alias)) {
66 CHECK_EQ(e, fmap_.at(alias))
67 << "Trying to register alias " << alias << " for key " << key_name
68 << " but " << alias << " is already taken";
69 } else {
70 fmap_[alias] = e;
71 }
72 }
78 inline EntryType &__REGISTER__(const std::string& name) {
79 std::lock_guard<std::mutex> guard(registering_mutex);
80 if (fmap_.count(name) > 0) {
81 return *fmap_[name];
82 }
83 EntryType *e = new EntryType();
84 e->name = name;
85 fmap_[name] = e;
86 const_list_.push_back(e);
87 entry_list_.push_back(e);
88 return *e;
89 }
95 inline EntryType &__REGISTER_OR_GET__(const std::string& name) {
96 if (fmap_.count(name) == 0) {
97 return __REGISTER__(name);
98 } else {
99 return *fmap_.at(name);
100 }
101 }
107 static Registry *Get();
108
109 private:
111 std::vector<EntryType*> entry_list_;
113 std::vector<const EntryType*> const_list_;
115 std::map<std::string, EntryType*> fmap_;
117 std::mutex registering_mutex;
119 Registry() {}
121 ~Registry() {
122 for (size_t i = 0; i < entry_list_.size(); ++i) {
123 delete entry_list_[i];
124 }
125 }
126};
127
150template<typename EntryType, typename FunctionType>
152 public:
154 std::string name;
156 std::string description;
158 std::vector<ParamFieldInfo> arguments;
160 FunctionType body;
162 std::string return_type;
163
169 inline EntryType &set_body(FunctionType body) {
170 this->body = body;
171 return this->self();
172 }
178 inline EntryType &describe(const std::string &description) {
179 this->description = description;
180 return this->self();
181 }
189 inline EntryType &add_argument(const std::string &name,
190 const std::string &type,
191 const std::string &description) {
192 ParamFieldInfo info;
193 info.name = name;
194 info.type = type;
195 info.type_info_str = info.type;
196 info.description = description;
197 arguments.push_back(info);
198 return this->self();
199 }
205 inline EntryType &add_arguments(const std::vector<ParamFieldInfo> &args) {
206 arguments.insert(arguments.end(), args.begin(), args.end());
207 return this->self();
208 }
214 inline EntryType &set_return_type(const std::string &type) {
215 return_type = type;
216 return this->self();
217 }
218
219 protected:
223 inline EntryType &self() {
224 return *(static_cast<EntryType*>(this));
225 }
226};
227
234#define DMLC_REGISTRY_ENABLE(EntryType) \
235 template<> \
236 Registry<EntryType > *Registry<EntryType >::Get() { \
237 static Registry<EntryType > inst; \
238 return &inst; \
239 } \
240
250#define DMLC_REGISTRY_REGISTER(EntryType, EntryTypeName, Name) \
251 static DMLC_ATTRIBUTE_UNUSED EntryType & __make_ ## EntryTypeName ## _ ## Name ## __ = \
252 ::dmlc::Registry<EntryType>::Get()->__REGISTER__(#Name) \
253
263#define DMLC_REGISTRY_FILE_TAG(UniqueTag) \
264 int __dmlc_registry_file_tag_ ## UniqueTag ## __() { return 0; }
265
305#define DMLC_REGISTRY_LINK_TAG(UniqueTag) \
306 int __dmlc_registry_file_tag_ ## UniqueTag ## __(); \
307 static int DMLC_ATTRIBUTE_UNUSED __reg_file_tag_ ## UniqueTag ## __ = \
308 __dmlc_registry_file_tag_ ## UniqueTag ## __();
309} // namespace dmlc
310#endif // DMLC_REGISTRY_H_
Common base class for function registry.
Definition registry.h:151
EntryType & set_body(FunctionType body)
Set the function body.
Definition registry.h:169
EntryType & add_argument(const std::string &name, const std::string &type, const std::string &description)
Add argument information to the function.
Definition registry.h:189
EntryType & describe(const std::string &description)
Describe the function.
Definition registry.h:178
std::vector< ParamFieldInfo > arguments
additional arguments to the factory function
Definition registry.h:158
std::string return_type
Return type of the function.
Definition registry.h:162
EntryType & set_return_type(const std::string &type)
Set the return type.
Definition registry.h:214
std::string description
description of the entry
Definition registry.h:156
FunctionType body
Function body to create ProductType.
Definition registry.h:160
EntryType & self()
Definition registry.h:223
std::string name
name of the entry
Definition registry.h:154
EntryType & add_arguments(const std::vector< ParamFieldInfo > &args)
Append list if arguments to the end.
Definition registry.h:205
Registry class. Registry can be used to register global singletons. The most commonly use case are fa...
Definition registry.h:27
static Registry * Get()
get a singleton of the Registry. This function can be defined by DMLC_REGISTRY_ENABLE.
static const std::vector< const EntryType * > & List()
Definition registry.h:30
void AddAlias(const std::string &key_name, const std::string &alias)
Add alias to the key_name.
Definition registry.h:62
EntryType & __REGISTER__(const std::string &name)
Internal function to register a name function under name.
Definition registry.h:78
static std::vector< std::string > ListAllNames()
Definition registry.h:34
static const EntryType * Find(const std::string &name)
Find the entry with corresponding name.
Definition registry.h:48
EntryType & __REGISTER_OR_GET__(const std::string &name)
Internal function to either register or get registered entry.
Definition registry.h:95
defines console logging options for xgboost. Use to enforce unified print behavior.
macro for using C++11 enum class as DMLC parameter
namespace for dmlc
Definition array_view.h:12
Macros common to all headers.
type traits information header