OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
registry.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// R E G I S T R Y C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include <string>
12#include <functional>
13#include <memory>
14#include <stdexcept>
15#include <unordered_map>
16#include <vector>
17
18namespace opennn
19{
20
21using std::string;
22using std::unique_ptr;
23using std::vector;
24using std::function;
25using std::unordered_map;
26using std::runtime_error;
27
28template<typename T>
30{
31public:
32
33 using Creator = function<unique_ptr<T>()>;
34
36 {
37 static Registry registry;
38 return registry;
39 }
40
41 void register_component(const string& name, Creator creator)
42 {
43 creators[name] = move(creator);
44 }
45
46 unique_ptr<T> create(const string& name) const
47 {
48 auto it = creators.find(name);
49
50 if (it == creators.end())
51 throw runtime_error("Component not found: " + name);
52
53 return it->second();
54
55 }
56
57 vector<string> registered_names() const
58 {
59 vector<string> names;
60
61 for (const auto& pair : creators)
62 names.push_back(pair.first);
63
64 return names;
65 }
66
67private:
68 unordered_map<string, Creator> creators;
69};
70
71#define REGISTER(BASE, CLASS, NAME) \
72namespace { \
73 const bool CLASS##_registered = []() { \
74 Registry<BASE>::instance().register_component(NAME, []() { \
75 return make_unique<CLASS>(); \
76 }); \
77 return true; \
78 }(); \
79}
80
82
83}
84
85// OpenNN: Open Neural Networks Library.
86// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
87// Licensed under the GNU Lesser General Public License v2.1 or later.
Definition registry.h:30
function< unique_ptr< T >()> Creator
Definition registry.h:33
void register_component(const string &name, Creator creator)
Definition registry.h:41
unique_ptr< T > create(const string &name) const
Definition registry.h:46
vector< string > registered_names() const
Definition registry.h:57
static Registry & instance()
Definition registry.h:35
Definition adaptive_moment_estimation.h:19
void register_classes()