|
10 | 10 | #include "cgimap/backend.hpp"
|
11 | 11 |
|
12 | 12 | #include <fmt/core.h>
|
13 |
| -#include <stdexcept> |
14 |
| -#include <mutex> |
| 13 | +#include <memory> |
15 | 14 |
|
16 | 15 | namespace po = boost::program_options;
|
17 | 16 |
|
@@ -43,166 +42,74 @@ po::variables_map first_pass_argments(int argc, char *argv[],
|
43 | 42 | struct registry {
|
44 | 43 | registry() = default;
|
45 | 44 |
|
46 |
| - bool add(std::unique_ptr<backend> ptr); |
| 45 | + bool set_backend(std::unique_ptr<backend> ptr); |
47 | 46 | void setup_options(int argc, char *argv[], po::options_description &desc);
|
48 | 47 | void output_options(std::ostream &out);
|
49 | 48 | std::unique_ptr<data_selection::factory> create(const po::variables_map &options);
|
50 | 49 | std::unique_ptr<data_update::factory> create_data_update(const po::variables_map &options);
|
51 | 50 |
|
52 | 51 | private:
|
53 |
| - using backend_map_t = std::map<std::string, std::unique_ptr<backend> >; |
54 |
| - backend_map_t backends; |
55 |
| - std::optional<std::string> default_backend; |
| 52 | + std::unique_ptr<backend> backend_ptr; |
56 | 53 | };
|
57 | 54 |
|
58 |
| -bool registry::add(std::unique_ptr<backend> ptr) { |
59 |
| - if (default_backend) { |
60 |
| - if (ptr->name() <= *default_backend) { |
61 |
| - default_backend = ptr->name(); |
62 |
| - } |
63 |
| - } else { |
64 |
| - default_backend = ptr->name(); |
65 |
| - } |
66 |
| - |
67 |
| - std::string name = ptr->name(); |
68 |
| - backends.emplace(name, std::move(ptr)); |
69 |
| - |
| 55 | +bool registry::set_backend(std::unique_ptr<backend> ptr) { |
| 56 | + backend_ptr = std::move(ptr); |
70 | 57 | return true;
|
71 | 58 | }
|
72 | 59 |
|
73 | 60 | void registry::setup_options(int argc, char *argv[],
|
74 | 61 | po::options_description &desc) {
|
75 |
| - if (backends.empty() || !default_backend) { |
76 |
| - throw std::runtime_error("No backends available - this is most likely a " |
77 |
| - "compile-time configuration error."); |
78 |
| - } |
79 |
| - |
80 |
| - std::string all_backends; |
81 |
| - |
82 |
| - for (const backend_map_t::value_type &val : backends) { |
83 |
| - if (!all_backends.empty()) { |
84 |
| - all_backends += ", "; |
85 |
| - } |
86 |
| - all_backends += val.second->name(); |
87 |
| - } |
88 |
| - |
89 |
| - std::string description = fmt::format("backend to use, available options are: {}", all_backends); |
90 |
| - |
91 |
| - desc.add_options()("backend", po::value<std::string>()->default_value(*default_backend), |
92 |
| - description.c_str()); |
93 | 62 |
|
94 | 63 | po::variables_map vm = first_pass_argments(argc, argv, desc);
|
95 | 64 |
|
96 |
| - std::string bcknd = *default_backend; |
97 |
| - |
98 |
| - // little hack - we want to print *all* the backends when --help is passed, so |
99 |
| - // we don't add one here when it's present. it's a nasty way to do it, but i |
100 |
| - // can't think of a better one right now... |
101 | 65 | if (!vm.count("help")) {
|
102 |
| - |
103 |
| - if (vm.count("backend")) { |
104 |
| - auto itr = |
105 |
| - backends.find(vm["backend"].as<std::string>()); |
106 |
| - if (itr != backends.end()) { |
107 |
| - bcknd = itr->first; |
108 |
| - } |
109 |
| - else { |
110 |
| - throw std::runtime_error(fmt::format("unknown backend provided, available options are: {}", all_backends)); |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - desc.add(backends[bcknd]->options()); |
| 66 | + desc.add(backend_ptr->options()); |
115 | 67 | }
|
116 | 68 | }
|
117 | 69 |
|
118 | 70 | void registry::output_options(std::ostream &out) {
|
119 |
| - for (const backend_map_t::value_type &val : backends) { |
120 |
| - out << val.second->options() << std::endl; |
121 |
| - } |
| 71 | + out << backend_ptr->options() << std::endl; |
122 | 72 | }
|
123 | 73 |
|
124 | 74 | std::unique_ptr<data_selection::factory>
|
125 | 75 | registry::create(const po::variables_map &options) {
|
126 |
| - std::string bcknd = *default_backend; |
127 |
| - |
128 |
| - if (options.count("backend")) { |
129 |
| - auto itr = |
130 |
| - backends.find(options["backend"].as<std::string>()); |
131 |
| - if (itr != backends.end()) { |
132 |
| - bcknd = itr->first; |
133 |
| - } |
134 |
| - } |
135 |
| - |
136 |
| - return backends[bcknd]->create(options); |
| 76 | + return backend_ptr->create(options); |
137 | 77 | }
|
138 | 78 |
|
139 | 79 | std::unique_ptr<data_update::factory>
|
140 | 80 | registry::create_data_update(const po::variables_map &options) {
|
141 |
| - std::string bcknd = *default_backend; |
142 |
| - |
143 |
| - if (options.count("backend")) { |
144 |
| - auto itr = |
145 |
| - backends.find(options["backend"].as<std::string>()); |
146 |
| - if (itr != backends.end()) { |
147 |
| - bcknd = itr->first; |
148 |
| - } |
149 |
| - } |
150 |
| - |
151 |
| - return backends[bcknd]->create_data_update(options); |
| 81 | + return backend_ptr->create_data_update(options); |
152 | 82 | }
|
153 | 83 |
|
154 |
| -registry *registry_ptr = NULL; |
155 |
| -std::mutex registry_mut; |
| 84 | +std::unique_ptr<registry> registry_ptr = std::make_unique<registry>(); |
| 85 | + |
156 | 86 |
|
157 | 87 | } // anonymous namespace
|
158 | 88 |
|
159 | 89 | backend::~backend() = default;
|
160 | 90 |
|
| 91 | +// Registers a single backend, replacing an existing backend |
161 | 92 | bool register_backend(std::unique_ptr<backend> ptr) {
|
162 |
| - std::unique_lock<std::mutex> lock(registry_mut); |
163 |
| - if (registry_ptr == NULL) { |
164 |
| - registry_ptr = new registry; |
165 |
| - } |
166 |
| - |
167 |
| - return registry_ptr->add(std::move(ptr)); |
| 93 | + return registry_ptr->set_backend(std::move(ptr)); |
168 | 94 | }
|
169 | 95 |
|
170 | 96 | void setup_backend_options(int argc, char *argv[],
|
171 | 97 | po::options_description &desc) {
|
172 |
| - std::unique_lock<std::mutex> lock(registry_mut); |
173 |
| - if (registry_ptr == NULL) { |
174 |
| - registry_ptr = new registry; |
175 |
| - } |
176 | 98 |
|
177 | 99 | registry_ptr->setup_options(argc, argv, desc);
|
178 | 100 | }
|
179 | 101 |
|
180 | 102 | void output_backend_options(std::ostream &out) {
|
181 |
| - std::unique_lock<std::mutex> lock(registry_mut); |
182 |
| - if (registry_ptr == NULL) { |
183 |
| - registry_ptr = new registry; |
184 |
| - } |
185 |
| - |
186 | 103 | registry_ptr->output_options(out);
|
187 | 104 | }
|
188 | 105 |
|
189 | 106 | std::unique_ptr<data_selection::factory>
|
190 | 107 | create_backend(const po::variables_map &options) {
|
191 |
| - std::unique_lock<std::mutex> lock(registry_mut); |
192 |
| - if (registry_ptr == NULL) { |
193 |
| - registry_ptr = new registry; |
194 |
| - } |
195 |
| - |
196 | 108 | return registry_ptr->create(options);
|
197 | 109 | }
|
198 | 110 |
|
199 | 111 | std::unique_ptr<data_update::factory>
|
200 | 112 | create_update_backend(const po::variables_map &options) {
|
201 |
| - std::unique_lock<std::mutex> lock(registry_mut); |
202 |
| - if (registry_ptr == NULL) { |
203 |
| - registry_ptr = new registry; |
204 |
| - } |
205 |
| - |
206 | 113 | return registry_ptr->create_data_update(options);
|
207 | 114 | }
|
208 | 115 |
|
0 commit comments