FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_baseManager.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_BASEMANAGER_HPP_INCLUDED
18#define _FGE_C_BASEMANAGER_HPP_INCLUDED
19
20#include "FastEngine/C_accessLock.hpp"
21#include "FastEngine/fge_except.hpp"
22#include "FastEngine/network/C_packet.hpp"
23#include "FastEngine/string_hash.hpp"
24#include "json.hpp"
25#include <cstddef>
26#include <filesystem>
27#include <memory>
28#include <mutex>
29#include <string>
30#include <string_view>
31#include <type_traits>
32#include <unordered_map>
33#include <variant>
34
35#define FGE_MANAGER_BAD std::string_view()
36
37namespace fge::manager
38{
39
40template<class TData>
42{
43 virtual ~BaseDataBlock() = default;
44 using DataPointer = std::shared_ptr<TData>;
45
46 inline virtual void unload() {}
47
48 DataPointer _ptr;
49 bool _valid{false};
50 std::filesystem::path _path;
51};
52
61template<class TData, class TDataBlock = BaseDataBlock<TData>>
63{
64public:
65 using DataType = TData;
66 using DataBlockType = TDataBlock;
67 using DataBlockPointer = std::shared_ptr<TDataBlock>;
68 using Map = std::unordered_map<std::string, DataBlockPointer, StringHash, std::equal_to<>>;
69
70 BaseManager() = default;
71 BaseManager(BaseManager const& r) = delete;
72 BaseManager(BaseManager&& r) noexcept = delete;
73 virtual ~BaseManager() = default;
74
75 BaseManager& operator=(BaseManager const& r) = delete;
76 BaseManager& operator=(BaseManager&& r) noexcept = delete;
77
88 virtual bool initialize() = 0;
89 [[nodiscard]] virtual bool isInitialized() = 0;
90 virtual void uninitialize() = 0;
91
97 [[nodiscard]] std::size_t size() const;
98
108 [[nodiscard]] AccessLock<std::mutex> acquireLock() const;
120 [[nodiscard]] typename Map::const_iterator begin(AccessLock<std::mutex> const& lock) const;
129 [[nodiscard]] typename Map::const_iterator end(AccessLock<std::mutex> const& lock) const;
130
137 [[nodiscard]] DataBlockPointer const& getBadElement() const;
138
145 [[nodiscard]] DataBlockPointer getElement(std::string_view name) const;
146 [[nodiscard]] bool contains(std::string_view name) const;
147 bool unload(std::string_view name);
148 void unloadAll();
149
157 bool push(std::string_view name, DataBlockPointer block);
158
159private:
160 Map g_data;
161 mutable std::mutex g_mutex;
162
163protected:
164 DataBlockPointer _g_badElement;
165};
166
167enum class DataAccessorOptions
168{
169 BLOCKPOINTER_ONLY,
170 ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER
171};
172
173template<class TManager, TManager* TGlobalManager>
175{
176 using Manager = TManager;
177 [[nodiscard]] inline constexpr TManager& operator()() const { return *TGlobalManager; }
178};
179
180template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
182{
183public:
184 using SharedDataType = typename TDataAccessorManagerInfo::Manager::DataBlockPointer;
185 using SharedType = typename TDataAccessorManagerInfo::Manager::DataBlockType::DataPointer;
186
193 BaseDataAccessor(std::string_view name);
194 BaseDataAccessor(char const name[]);
195 BaseDataAccessor(std::string name);
201 BaseDataAccessor(SharedDataType data);
207 BaseDataAccessor(SharedType data)
208 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
209 virtual ~BaseDataAccessor() = default;
210
216 virtual void clear();
217
221 void reload();
222
228 [[nodiscard]] bool valid() const;
229
237 [[nodiscard]] SharedDataType const& getSharedBlock() const;
243 [[nodiscard]] SharedType const& getSharedData() const
244 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
250 [[nodiscard]] std::string const& getName() const;
251
252 auto& operator=(std::string_view name);
253 auto& operator=(char const name[]);
254 auto& operator=(std::string name);
255 auto& operator=(SharedDataType data);
256 auto& operator=(SharedType data)
257 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
258
266 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType* retrieve();
267 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType const* retrieve() const;
268
269private:
270 using VariantType = std::variant<SharedDataType, SharedType>;
271 using Type = std::conditional_t<TOption == DataAccessorOptions::BLOCKPOINTER_ONLY, SharedDataType, VariantType>;
272
273 Type g_data;
274 std::string g_name;
275};
276
277template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
278static net::Packet const& operator>>(net::Packet const& pck, BaseDataAccessor<TDataAccessorManagerInfo, TOption>& data);
279template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
281
282template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
283static void to_json(nlohmann::json& j, BaseDataAccessor<TDataAccessorManagerInfo, TOption> const& p);
284template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
285static void from_json(nlohmann::json const& j, BaseDataAccessor<TDataAccessorManagerInfo, TOption>& p);
286
287} // namespace fge::manager
288
289#include "C_baseManager.inl"
290
291#endif // _FGE_C_BASEMANAGER_HPP_INCLUDED
Class that lock a mutex and unlock it only when the object is destroyed.
Definition C_accessLock.hpp:38
Definition C_baseManager.hpp:182
SharedType const & getSharedData() const
Get the shared resource data.
Definition C_baseManager.inl:218
bool valid() const
Check if the font is valid (not unloaded)
Definition C_baseManager.inl:183
TDataAccessorManagerInfo::Manager::DataType * retrieve()
Retrieve the raw shared pointer from the current resource.
Definition C_baseManager.inl:291
SharedDataType const & getSharedBlock() const
Get the resource block data.
Definition C_baseManager.inl:201
virtual void clear()
Clear the resource.
Definition C_baseManager.inl:170
std::string const & getName() const
Get the name of the resource.
Definition C_baseManager.inl:233
void reload()
Reload the cached resource from the same name.
Definition C_baseManager.inl:177
Base class for all managers.
Definition C_baseManager.hpp:63
Map::const_iterator begin(AccessLock< std::mutex > const &lock) const
Get the "begin" iterator of the manager.
Definition C_baseManager.inl:35
DataBlockPointer getElement(std::string_view name) const
Get the resource with the given name.
Definition C_baseManager.inl:55
AccessLock< std::mutex > acquireLock() const
Acquire a AccessLock, with the manager mutex.
Definition C_baseManager.inl:29
std::size_t size() const
Get the number of elements in the manager.
Definition C_baseManager.inl:23
bool push(std::string_view name, DataBlockPointer block)
Add a user handled resource.
Definition C_baseManager.inl:117
DataBlockPointer const & getBadElement() const
Get the "bad" element.
Definition C_baseManager.inl:49
Map::const_iterator end(AccessLock< std::mutex > const &lock) const
Get the "end" iterator of the manager.
Definition C_baseManager.inl:42
virtual bool initialize()=0
Initialize the manager.
Definition C_packet.hpp:70
Definition C_baseManager.hpp:42
Definition C_baseManager.hpp:175