Raytracer
Loading...
Searching...
No Matches
Factory.hpp
1#include <functional>
2#include <memory>
3#include <string>
4#include "interfaces/IArguments.hpp"
5#include "interfaces/IHittable.hpp"
6#include "interfaces/IMaterial.hpp"
7#include "interfaces/ITexture.hpp"
8#include <type_traits>
9#include <unordered_map>
10
11#ifndef __CFG_FACTORY_HPP__
12 #define __CFG_FACTORY_HPP__
13
14namespace Raytracer::Config
15{
16 enum class ConfigTextures {
17 TEXTURE_SOLID,
18 TEXTURE_NOISE,
19 TEXTURE_IMAGE,
20 TEXTURE_CHECKER,
21 };
22
23 enum class ConfigEffects {
24 EFFECT_ROTATE_X,
25 EFFECT_ROTATE_Y,
26 EFFECT_ROTATE_Z,
27 EFFECT_SMOKE,
28 EFFECT_TRANSLATE,
29 };
30
31 enum class ConfigMaterials {
32 MATERIAL_LAMBERTIAN,
33 MATERIAL_DIELECTRIC,
34 MATERIAL_DIFFUSE_LIGHT,
35 MATERIAL_ISOTROPIC,
36 MATERIAL_METAL,
37 };
38
39 enum class ConfigShapes {
40 SHAPE_CONE,
41 SHAPE_CYLINDER,
42 SHAPE_PLANE,
43 SHAPE_QUAD,
44 SHAPE_SPHERE,
45 SHAPE_MOVING_SPHERE,
46 };
47
48 template <typename I, typename E>
49 requires std::is_enum_v<E>
50 using FactoryFunction = std::function<std::shared_ptr<I>(
51 std::shared_ptr<Interfaces::IArguments>)>;
52
53 template <typename I, typename E>
54 using FactoryMap = std::unordered_map<std::string, FactoryFunction<I, E>>;
55
56 template <typename T, typename E>
57 concept isValidEnum = std::is_enum_v<T> && std::is_same_v<T, E>;
58
59 class Factory {
60 private:
61 Factory() = delete;
62
63 template <typename I, typename E>
64 static std::shared_ptr<I> _get(const FactoryMap<I, E> &map,
65 const std::string &name,
66 std::shared_ptr<Interfaces::IArguments> args)
67 {
68 return map.at(name)(args);
69 }
70
71 public:
72 static FactoryMap<Raytracer::Interfaces::ITexture, ConfigTextures>
74 static FactoryMap<Raytracer::Interfaces::IHittable, ConfigEffects>
75 effects;
76 static FactoryMap<Raytracer::Interfaces::IMaterial, ConfigMaterials>
78 static FactoryMap<Raytracer::Interfaces::IHittable, ConfigShapes>
79 shapes;
80
81 template <typename I, typename E>
83 static std::shared_ptr<I> get(const std::string &name,
84 std::shared_ptr<Interfaces::IArguments> args)
85 {
86 return _get<I, E>(textures, name, args);
87 }
88
89 template <typename I, typename E>
91 static std::shared_ptr<I> get(const std::string &name,
92 std::shared_ptr<Interfaces::IArguments> args)
93 {
94 return _get<I, E>(effects, name, args);
95 }
96
97 template <typename I, typename E>
99 static std::shared_ptr<I> get(const std::string &name,
100 std::shared_ptr<Interfaces::IArguments> args)
101 {
102 return _get<I, E>(materials, name, args);
103 }
105 template <typename I, typename E>
107 static std::shared_ptr<I> get(const std::string &name,
108 std::shared_ptr<Interfaces::IArguments> args)
109 {
110 return _get<I, E>(shapes, name, args);
111 }
112 };
113} // namespace Raytracer::Config
114
115#endif /* __CFG_FACTORY_HPP__ */
Definition Factory.hpp:59
static FactoryMap< Raytracer::Interfaces::IMaterial, ConfigMaterials > materials
Factory map for materials.
Definition Factory.hpp:182
static FactoryMap< Raytracer::Interfaces::ITexture, ConfigTextures > textures
Factory map for textures.
Definition Factory.hpp:37
static FactoryMap< Raytracer::Interfaces::IHittable, ConfigEffects > effects
Factory map for effects.
Definition Factory.hpp:104
static FactoryMap< Raytracer::Interfaces::IHittable, ConfigShapes > shapes
Factory map for shapes.
Definition Factory.hpp:289
Definition Factory.hpp:57