| 1 | /*! |
| 2 | * Copyright (c) 2015 by Contributors |
| 3 | * \file registry.h |
| 4 | * \brief Registry utility that helps to build registry singletons. |
| 5 | */ |
| 6 | #ifndef DMLC_REGISTRY_H_ |
| 7 | #define DMLC_REGISTRY_H_ |
| 8 | |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "./base.h" |
| 14 | #include "./logging.h" |
| 15 | #include "./parameter.h" |
| 16 | #include "./type_traits.h" |
| 17 | |
| 18 | namespace dmlc { |
| 19 | /*! |
| 20 | * \brief Registry class. |
| 21 | * Registry can be used to register global singletons. |
| 22 | * The most commonly use case are factory functions. |
| 23 | * |
| 24 | * \tparam EntryType Type of Registry entries, |
| 25 | * EntryType need to name a name field. |
| 26 | */ |
| 27 | template <typename EntryType> |
| 28 | class Registry { |
| 29 | public: |
| 30 | /*! \return list of entries in the registry(excluding alias) */ |
| 31 | inline static const std::vector<const EntryType *> &List() { |
| 32 | return Get()->const_list_; |
| 33 | } |
| 34 | /*! \return list all names registered in the registry, including alias */ |
| 35 | inline static std::vector<std::string> ListAllNames() { |
| 36 | const std::map<std::string, EntryType *> &fmap = Get()->fmap_; |
| 37 | typename std::map<std::string, EntryType *>::const_iterator p; |
| 38 | std::vector<std::string> names; |
| 39 | for (p = fmap.begin(); p != fmap.end(); ++p) { |
| 40 | names.push_back(p->first); |
| 41 | } |
| 42 | return names; |
| 43 | } |
| 44 | /*! |
| 45 | * \brief Find the entry with corresponding name. |
| 46 | * \param name name of the function |
| 47 | * \return the corresponding function, can be NULL |
| 48 | */ |
| 49 | inline static const EntryType *Find(const std::string &name) { |
| 50 | const std::map<std::string, EntryType *> &fmap = Get()->fmap_; |
| 51 | typename std::map<std::string, EntryType *>::const_iterator p = fmap.find(name); |
| 52 | if (p != fmap.end()) { |
| 53 | return p->second; |
| 54 | } else { |
| 55 | return NULL; |
| 56 | } |
| 57 | } |
| 58 | /*! |
| 59 | * \brief Add alias to the key_name |
| 60 | * \param key_name The original entry key |
| 61 | * \param alias The alias key. |
| 62 | */ |
| 63 | inline void AddAlias(const std::string &key_name, const std::string &alias) { |
| 64 | EntryType *e = fmap_.at(key_name); |
| 65 | if (fmap_.count(alias)) { |
| 66 | CHECK_EQ(e, fmap_.at(alias)) << "Trying to register alias " << alias << " for key " |
| 67 | << key_name << " but " << alias << " is already taken"; |
| 68 | } else { |
| 69 | fmap_[alias] = e; |
| 70 | } |
| 71 | } |
| 72 | /*! |
| 73 | * \brief Internal function to register a name function under name. |
| 74 | * \param name name of the function |
| 75 | * \return ref to the registered entry, used to set properties |
| 76 | */ |
| 77 | inline EntryType &__REGISTER__(const std::string &name) { |
| 78 | std::lock_guard<std::mutex> guard(registering_mutex); |
| 79 | if (fmap_.count(name) > 0) { |
| 80 | return *fmap_[name]; |
| 81 | } |
| 82 | EntryType *e = new EntryType(); |
| 83 | e->name = name; |
| 84 | fmap_[name] = e; |
| 85 | const_list_.push_back(e); |
| 86 | entry_list_.push_back(e); |
| 87 | return *e; |
| 88 | } |
| 89 | /*! |
| 90 | * \brief Internal function to either register or get registered entry |
| 91 | * \param name name of the function |
| 92 | * \return ref to the registered entry, used to set properties |
| 93 | */ |
| 94 | inline EntryType &__REGISTER_OR_GET__(const std::string &name) { |
| 95 | if (fmap_.count(name) == 0) { |
| 96 | return __REGISTER__(name); |
| 97 | } else { |
| 98 | return *fmap_.at(name); |
| 99 | } |
| 100 | } |
| 101 | /*! |
| 102 | * \brief get a singleton of the Registry. |
| 103 | * This function can be defined by DMLC_REGISTRY_ENABLE. |
| 104 | * \return get a singleton |
| 105 | */ |
| 106 | static Registry *Get(); |
| 107 | |
| 108 | private: |
| 109 | /*! \brief list of entry types */ |
| 110 | std::vector<EntryType *> entry_list_; |
| 111 | /*! \brief list of entry types */ |
| 112 | std::vector<const EntryType *> const_list_; |
| 113 | /*! \brief map of name->function */ |
| 114 | std::map<std::string, EntryType *> fmap_; |
| 115 | /*! \brief lock guarding the registering*/ |
| 116 | std::mutex registering_mutex; |
| 117 | /*! \brief constructor */ |
| 118 | Registry() {} |
| 119 | /*! \brief destructor */ |
| 120 | ~Registry() { |
| 121 | for (size_t i = 0; i < entry_list_.size(); ++i) { |
| 122 | delete entry_list_[i]; |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | /*! |
| 128 | * \brief Common base class for function registry. |
| 129 | * |
| 130 | * \code |
| 131 | * // This example demonstrates how to use Registry to create a factory of trees. |
| 132 | * struct TreeFactory : |
| 133 | * public FunctionRegEntryBase<TreeFactory, std::function<Tree*()> > { |
| 134 | * }; |
| 135 | * |
| 136 | * // in a independent cc file |
| 137 | * namespace dmlc { |
| 138 | * DMLC_REGISTRY_ENABLE(TreeFactory); |
| 139 | * } |
| 140 | * // register binary tree constructor into the registry. |
| 141 | * DMLC_REGISTRY_REGISTER(TreeFactory, TreeFactory, BinaryTree) |
| 142 | * .describe("Constructor of BinaryTree") |
| 143 | * .set_body([]() { return new BinaryTree(); }); |
| 144 | * \endcode |
| 145 | * |
| 146 | * \tparam EntryType The type of subclass that inheritate the base. |
| 147 | * \tparam FunctionType The function type this registry is registerd. |
| 148 | */ |
| 149 | template <typename EntryType, typename FunctionType> |
| 150 | class FunctionRegEntryBase { |
| 151 | public: |
| 152 | /*! \brief name of the entry */ |
| 153 | std::string name; |
| 154 | /*! \brief description of the entry */ |
| 155 | std::string description; |
| 156 | /*! \brief additional arguments to the factory function */ |
| 157 | std::vector<ParamFieldInfo> arguments; |
| 158 | /*! \brief Function body to create ProductType */ |
| 159 | FunctionType body; |
| 160 | /*! \brief Return type of the function */ |
| 161 | std::string return_type; |
| 162 | |
| 163 | /*! |
| 164 | * \brief Set the function body. |
| 165 | * \param body Function body to set. |
| 166 | * \return reference to self. |
| 167 | */ |
| 168 | inline EntryType &set_body(FunctionType body) { |
| 169 | this->body = body; |
| 170 | return this->self(); |
| 171 | } |
| 172 | /*! |
| 173 | * \brief Describe the function. |
| 174 | * \param description The description of the factory function. |
| 175 | * \return reference to self. |
| 176 | */ |
| 177 | inline EntryType &describe(const std::string &description) { |
| 178 | this->description = description; |
| 179 | return this->self(); |
| 180 | } |
| 181 | /*! |
| 182 | * \brief Add argument information to the function. |
| 183 | * \param name Name of the argument. |
| 184 | * \param type Type of the argument. |
| 185 | * \param description Description of the argument. |
| 186 | * \return reference to self. |
| 187 | */ |
| 188 | inline EntryType &add_argument( |
| 189 | const std::string &name, const std::string &type, const std::string &description) { |
| 190 | ParamFieldInfo info; |
| 191 | info.name = name; |
| 192 | info.type = type; |
| 193 | info.type_info_str = info.type; |
| 194 | info.description = description; |
| 195 | arguments.push_back(info); |
| 196 | return this->self(); |
| 197 | } |
| 198 | /*! |
| 199 | * \brief Append list if arguments to the end. |
| 200 | * \param args Additional list of arguments. |
| 201 | * \return reference to self. |
| 202 | */ |
| 203 | inline EntryType &add_arguments(const std::vector<ParamFieldInfo> &args) { |
| 204 | arguments.insert(arguments.end(), args.begin(), args.end()); |
| 205 | return this->self(); |
| 206 | } |
| 207 | /*! |
| 208 | * \brief Set the return type. |
| 209 | * \param type Return type of the function, could be Symbol or Symbol[] |
| 210 | * \return reference to self. |
| 211 | */ |
| 212 | inline EntryType &set_return_type(const std::string &type) { |
| 213 | return_type = type; |
| 214 | return this->self(); |
| 215 | } |
| 216 | |
| 217 | protected: |
| 218 | /*! |
| 219 | * \return reference of self as derived type |
| 220 | */ |
| 221 | inline EntryType &self() { |
| 222 | return *(static_cast<EntryType *>(this)); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | /*! |
| 227 | * \def DMLC_REGISTRY_ENABLE |
| 228 | * \brief Macro to enable the registry of EntryType. |
| 229 | * This macro must be used under namespace dmlc, and only used once in cc file. |
| 230 | * \param EntryType Type of registry entry |
| 231 | */ |
| 232 | #define DMLC_REGISTRY_ENABLE(EntryType) \ |
| 233 | template <> \ |
| 234 | Registry<EntryType> *Registry<EntryType>::Get() { \ |
| 235 | static Registry<EntryType> inst; \ |
| 236 | return &inst; \ |
| 237 | } |
| 238 | |
| 239 | /*! |
| 240 | * \brief Generic macro to register an EntryType |
| 241 | * There is a complete example in FactoryRegistryEntryBase. |
| 242 | * |
| 243 | * \param EntryType The type of registry entry. |
| 244 | * \param EntryTypeName The typename of EntryType, must do not contain namespace :: . |
| 245 | * \param Name The name to be registered. |
| 246 | * \sa FactoryRegistryEntryBase |
| 247 | */ |
| 248 | #define DMLC_REGISTRY_REGISTER(EntryType, EntryTypeName, Name) \ |
| 249 | static DMLC_ATTRIBUTE_UNUSED EntryType &__make_##EntryTypeName##_##Name##__ \ |
| 250 | = ::dmlc::Registry<EntryType>::Get()->__REGISTER__(#Name) |
| 251 | |
| 252 | /*! |
| 253 | * \brief (Optional) Declare a file tag to current file that contains object registrations. |
| 254 | * |
| 255 | * This will declare a dummy function that will be called by register file to |
| 256 | * incur a link dependency. |
| 257 | * |
| 258 | * \param UniqueTag The unique tag used to represent. |
| 259 | * \sa DMLC_REGISTRY_LINK_TAG |
| 260 | */ |
| 261 | #define DMLC_REGISTRY_FILE_TAG(UniqueTag) \ |
| 262 | int __dmlc_registry_file_tag_##UniqueTag##__() { \ |
| 263 | return 0; \ |
| 264 | } |
| 265 | |
| 266 | /*! |
| 267 | * \brief (Optional) Force link to all the objects registered in file tag. |
| 268 | * |
| 269 | * This macro must be used in the same file as DMLC_REGISTRY_ENABLE and |
| 270 | * in the same namespace as DMLC_REGISTRY_FILE_TAG |
| 271 | * |
| 272 | * DMLC_REGISTRY_FILE_TAG and DMLC_REGISTRY_LINK_TAG are optional macros for registration. |
| 273 | * They are used to encforce link of certain file into during static linking. |
| 274 | * |
| 275 | * This is mainly used to solve problem during statically link a library which contains backward |
| 276 | * registration. Specifically, this avoids the objects in these file tags to be ignored by compiler. |
| 277 | * |
| 278 | * For dynamic linking, this problem won't occur as everything is loaded by default. |
| 279 | * |
| 280 | * Use of this is optional as it will create an error when a file tag do not exist. |
| 281 | * An alternative solution is always ask user to enable --whole-archieve during static link. |
| 282 | * |
| 283 | * \code |
| 284 | * // in file objective_registry.cc |
| 285 | * DMLC_REGISTRY_ENABLE(MyObjective); |
| 286 | * DMLC_REGISTRY_LINK_TAG(regression_op); |
| 287 | * DMLC_REGISTRY_LINK_TAG(rank_op); |
| 288 | * |
| 289 | * // in file regression_op.cc |
| 290 | * // declare tag of this file. |
| 291 | * DMLC_REGISTRY_FILE_TAG(regression_op); |
| 292 | * DMLC_REGISTRY_REGISTER(MyObjective, logistic_reg, logistic_reg); |
| 293 | * // ... |
| 294 | * |
| 295 | * // in file rank_op.cc |
| 296 | * // declare tag of this file. |
| 297 | * DMLC_REGISTRY_FILE_TAG(rank_op); |
| 298 | * DMLC_REGISTRY_REGISTER(MyObjective, pairwiserank, pairwiserank); |
| 299 | * |
| 300 | * \endcode |
| 301 | * |
| 302 | * \param UniqueTag The unique tag used to represent. |
| 303 | * \sa DMLC_REGISTRY_ENABLE, DMLC_REGISTRY_FILE_TAG |
| 304 | */ |
| 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_ |