SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Factory.h
1#pragma once
2
3#include "spark/patterns/details/Creators.h"
4
5#include <memory>
6#include <unordered_map>
7#include <vector>
8
9namespace spark::patterns
10{
17 template <typename Key, typename BaseType, typename... Args>
18 class Factory
19 {
20 public:
21 using BasePtr = std::unique_ptr<BaseType>;
22 using CreatorPtr = std::unique_ptr<details::BaseCreator<BaseType, Args...>>;
23
24 public:
25 Factory() = default;
26 virtual ~Factory() = default;
27
28 Factory(const Factory& other) = delete;
29 Factory(Factory&& other) noexcept = default;
30 Factory& operator=(const Factory& other) = delete;
31 Factory& operator=(Factory&& other) noexcept = default;
32
40 template <typename TypeToRegister>
41 void registerType(const Key& key);
42
51 [[nodiscard]] BasePtr create(const Key& key, Args&&... args) const;
52
59 [[nodiscard]] BasePtr createOrFail(const Key& key, Args&&... args) const noexcept;
60
65 [[nodiscard]] std::vector<Key> registeredTypes() const noexcept;
66
67 private:
68 std::unordered_map<Key, CreatorPtr> m_creators;
69 };
70}
71
72#include "spark/patterns/impl/Factory.h"
A factory class that creates objects of type BaseType.
Definition Factory.h:19
BasePtr createOrFail(const Key &key, Args &&... args) const noexcept
Creates an object of type BaseType.
Definition Factory.h:29
void registerType(const Key &key)
Registers a type the factory can create.
Definition Factory.h:13
std::vector< Key > registeredTypes() const noexcept
Gets a vector of all registered types in the factory.
Definition Factory.h:37
BasePtr create(const Key &key, Args &&... args) const
Creates an object of type BaseType. Throws an exception if the type is not registered.
Definition Factory.h:21
A base class for all creators.
Definition Creators.h:14