Loading...
Searching...
No Matches
named_factory.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * NamedFactory — a registry entry that only builds one implementation.
5 *
6 * Some plug-in points need nothing from an implementation beyond a name and a
7 * way to construct it: a volumetric file format, a PAW one-centre convention.
8 * This entry carries exactly that, so each implementation does not need an
9 * entry class of its own. See utils/registry.hpp for how entries are found.
10 */
11
12#include <functional>
13#include <memory>
14#include <string>
15#include <utility>
16#include <vector>
17
18namespace q3 {
19
20/// A registry entry that builds one implementation of `Product`.
21template <typename Product> class NamedFactory {
22 public:
23 using Builder = std::function<std::unique_ptr<Product>()>;
24
25 /// An entry answering to `names` (the first is canonical) that builds with `builder`.
26 NamedFactory(std::vector<std::string> names, Builder builder)
27 : names_(std::move(names)), builder_(std::move(builder)) {}
28
29 /// The names this entry answers to; the first is canonical.
30 [[nodiscard]] const std::vector<std::string>& names() const {
31 return names_;
32 }
33
34 /// A newly built implementation.
35 [[nodiscard]] std::unique_ptr<Product> create() const {
36 return builder_();
37 }
38
39 private:
40 std::vector<std::string> names_;
41 Builder builder_;
42};
43
44} // namespace q3
A registry entry that builds one implementation of Product.
std::function< std::unique_ptr< Product >()> Builder
std::unique_ptr< Product > create() const
A newly built implementation.
NamedFactory(std::vector< std::string > names, Builder builder)
An entry answering to names (the first is canonical) that builds with builder.
const std::vector< std::string > & names() const
The names this entry answers to; the first is canonical.