-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.hpp
More file actions
41 lines (35 loc) · 1.04 KB
/
Factory.hpp
File metadata and controls
41 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include <functional>
#include <memory>
#include <utility>
#include <vector>
template<class T>
class InstanceBuilder {
const std::function<T*()> construct;
public:
InstanceBuilder(std::string name, std::string description, std::function<T*()> c)
: name(std::move(name)), description(std::move(description)), construct(c)
{}
const std::string name;
const std::string description;
template<class P>
constexpr auto get() const -> P {return P(construct());}
constexpr auto describe() const -> std::string
{
if(description.empty()) return name;
return name + " - " + description;
}
};
template<class T>
struct Factory {
std::vector<InstanceBuilder<T>> done;
template<class C>
constexpr auto add(std::string name, std::string description = {}) -> Factory<T>&
{
done.emplace_back(name, description, []{return new C();});
return *this;
}
};
template<class T>
concept has_factory =
std::same_as<decltype(T::all), const std::vector<InstanceBuilder<T>>>;