Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
ApiDatabase.h
Go to the documentation of this file.
1#pragma once
2
4#include "Core/Engine.h"
5#include "Frame/TFrame.h"
7
8#include <Common/logging.h>
9
10#include <memory>
11#include <mutex>
12#include <type_traits>
13#include <utility>
14#include <string>
15
16namespace ph
17{
18
20
21class ApiDatabase final
22{
23public:
24 template<typename Resource>
25 static std::size_t addResource(std::unique_ptr<Resource> resource);
26
27 template<typename Resource>
28 static Resource* getResource(std::size_t id);
29
30 template<typename Resource>
31 static bool removeResource(std::size_t id);
32
33 template<typename Resource>
34 static std::weak_ptr<Resource> useResource(std::size_t id);
35
36 static void clear();
37
38private:
39 template<typename Resource>
41
42 static std::mutex& MUTEX();
43};
44
45// In-header Implementations:
46
47template<typename Resource>
48inline std::size_t ApiDatabase::addResource(std::unique_ptr<Resource> resource)
49{
50 std::lock_guard<std::mutex> lock(MUTEX());
51
52 const std::size_t id = RESOURCES<Resource>().add(std::move(resource));
53
54 PH_LOG(ApiDatabase, Note, "added resource<{}>", id);
55
56 return id;
57}
58
59template<typename Resource>
60inline Resource* ApiDatabase::getResource(const std::size_t id)
61{
62 std::lock_guard<std::mutex> lock(MUTEX());
63
64 auto* const resource = RESOURCES<Resource>().get(id);
65
66 if(!resource)
67 {
68 PH_LOG(ApiDatabase, Warning, "resource<{}> does not exist", id);
69 }
70
71 return resource ? resource->get() : nullptr;
72}
73
74template<typename Resource>
75inline bool ApiDatabase::removeResource(const std::size_t id)
76{
77 std::lock_guard<std::mutex> lock(MUTEX());
78
79 const bool isRemoved = RESOURCES<Resource>().remove(id);
80
81 if(isRemoved)
82 {
83 PH_LOG(ApiDatabase, Note, "removed resource<{}>", id);
84 }
85 else
86 {
87 PH_LOG(ApiDatabase, Warning, "failed removing resource<{}>", id);
88 }
89
90 return isRemoved;
91}
92
93template<typename Resource>
94inline std::weak_ptr<Resource> ApiDatabase::useResource(const std::size_t id)
95{
96 std::lock_guard<std::mutex> lock(MUTEX());
97
98 return RESOURCES<Resource>().isStableIndexValid(id) ? RESOURCES<Resource>()[id] : nullptr;
99}
100
101template<typename Resource>
102inline TStableIndexDenseVector<std::shared_ptr<Resource>>& ApiDatabase::RESOURCES()
103{
104 static_assert(
105 std::is_same_v<Resource, Engine> ||
106 std::is_same_v<Resource, HdrRgbFrame> ||
107 std::is_same_v<Resource, ByteBuffer>,
108 "error: in ApiDatabase::getResource(), "
109 "type of the specified resource is unsupported");
110
112 return resources;
113}
114
115inline std::mutex& ApiDatabase::MUTEX()
116{
117 static std::mutex lock;
118 return lock;
119}
120
121}// end namespace ph
Definition ApiDatabase.h:22
static bool removeResource(std::size_t id)
Definition ApiDatabase.h:75
static std::size_t addResource(std::unique_ptr< Resource > resource)
Definition ApiDatabase.h:48
static void clear()
Definition ApiDatabase.cpp:6
static std::weak_ptr< Resource > useResource(std::size_t id)
Definition ApiDatabase.h:94
static Resource * getResource(std::size_t id)
Definition ApiDatabase.h:60
A container offering stable index and efficient iteration.
Definition TStableIndexDenseVector.h:35
The root for all renderer implementations.
Definition EEngineProject.h:6
PH_DEFINE_EXTERNAL_LOG_GROUP(ApiDatabase, Core)