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 <utility>
17#include <vector>
18#include <format>
19
20namespace opennn
21{
22
23template<typename T>
25{
26public:
27
28 using Creator = function<unique_ptr<T>()>;
29
30 [[nodiscard]] static Registry& instance()
31 {
32 static Registry registry;
33 return registry;
34 }
35
36 void register_component(const string& name, Creator creator)
37 {
38 creators[name] = move(creator);
39 }
40
41 [[nodiscard]] unique_ptr<T> create(const string& name) const
42 {
43 auto it = creators.find(name);
44
45 if (it == creators.end())
46 throw runtime_error(format("Component not found: {}", name));
47
48 return it->second();
49
50 }
51
52 [[nodiscard]] vector<string> registered_names() const
53 {
54 vector<string> names;
55
56 for (const auto& pair : creators)
57 names.push_back(pair.first);
58
59 return names;
60 }
61
62private:
63 unordered_map<string, Creator> creators;
64};
65
66#define REGISTER(BASE, CLASS, NAME) \
67namespace { \
68 const bool CLASS##_registered = []() { \
69 Registry<BASE>::instance().register_component(NAME, []() { \
70 return make_unique<CLASS>(); \
71 }); \
72 return true; \
73 }(); \
74}
75
77
78}
79
80// OpenNN: Open Neural Networks Library.
81// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
82// Licensed under the GNU Lesser General Public License v2.1 or later.
Definition registry.h:25
function< unique_ptr< T >()> Creator
Definition registry.h:28
void register_component(const string &name, Creator creator)
Definition registry.h:36
unique_ptr< T > create(const string &name) const
Definition registry.h:41
vector< string > registered_names() const
Definition registry.h:52
static Registry & instance()
Definition registry.h:30
Definition adaptive_moment_estimation.h:14
void register_classes()