FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_baseManager.hpp
1/*
2 * Copyright 2025 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 [[nodiscard]] inline virtual bool duplicate(BaseDataBlock& block) const
49 {
50 if constexpr (std::is_copy_constructible_v<TData>)
51 {
52 block._ptr = std::make_shared<TData>(*this->_ptr);
53 block._valid = this->_valid;
54 block._path = this->_path;
55 return true;
56 }
57 else
58 {
59 return false;
60 }
61 }
62
63 DataPointer _ptr;
64 bool _valid{false};
65 std::filesystem::path _path;
66};
67
76template<class TData, class TDataBlock = BaseDataBlock<TData>>
77class BaseManager
78{
79public:
80 using DataType = TData;
81 using DataBlockType = TDataBlock;
82 using DataBlockPointer = std::shared_ptr<TDataBlock>;
83 using Map = std::unordered_map<std::string, DataBlockPointer, StringHash, std::equal_to<>>;
84
85 BaseManager() = default;
86 BaseManager(BaseManager const& r) = delete;
87 BaseManager(BaseManager&& r) noexcept = delete;
88 virtual ~BaseManager() = default;
89
90 BaseManager& operator=(BaseManager const& r) = delete;
91 BaseManager& operator=(BaseManager&& r) noexcept = delete;
92
103 virtual bool initialize() = 0;
104 [[nodiscard]] virtual bool isInitialized();
105 virtual void uninitialize();
106
112 [[nodiscard]] std::size_t size() const;
113
123 [[nodiscard]] AccessLock<std::mutex> acquireLock() const;
135 [[nodiscard]] typename Map::const_iterator begin(AccessLock<std::mutex> const& lock) const;
144 [[nodiscard]] typename Map::const_iterator end(AccessLock<std::mutex> const& lock) const;
145
152 [[nodiscard]] DataBlockPointer const& getBadElement() const;
153
160 [[nodiscard]] DataBlockPointer getElement(std::string_view name) const;
161 [[nodiscard]] bool contains(std::string_view name) const;
162 bool unload(std::string_view name);
163 void unloadAll();
164
165 bool duplicate(std::string_view name, std::string_view newName);
166
174 bool push(std::string_view name, DataBlockPointer block);
175
176private:
177 Map g_data;
178 mutable std::mutex g_mutex;
179
180protected:
181 DataBlockPointer _g_badElement;
182};
183
184enum class DataAccessorOptions
185{
186 BLOCKPOINTER_ONLY,
187 ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER
188};
189
190template<class TManager, TManager* TGlobalManager>
192{
193 using Manager = TManager;
194 [[nodiscard]] inline constexpr TManager& operator()() const { return *TGlobalManager; }
195};
196
197template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
198class BaseDataAccessor
199{
200public:
201 using SharedDataType = typename TDataAccessorManagerInfo::Manager::DataBlockPointer;
202 using SharedType = typename TDataAccessorManagerInfo::Manager::DataBlockType::DataPointer;
203
204 BaseDataAccessor();
210 BaseDataAccessor(std::string_view name);
211 BaseDataAccessor(char const name[]);
212 BaseDataAccessor(std::string name);
218 BaseDataAccessor(SharedDataType data);
224 BaseDataAccessor(SharedType data)
225 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
226 virtual ~BaseDataAccessor() = default;
227
233 virtual void clear();
234
238 void reload();
239
245 [[nodiscard]] bool valid() const;
246
254 [[nodiscard]] SharedDataType const& getSharedBlock() const;
260 [[nodiscard]] SharedType const& getSharedData() const
261 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
267 [[nodiscard]] std::string const& getName() const;
268
269 auto& operator=(std::string_view name);
270 auto& operator=(char const name[]);
271 auto& operator=(std::string name);
272 auto& operator=(SharedDataType data);
273 auto& operator=(SharedType data)
274 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
275
283 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType* retrieve();
284 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType const* retrieve() const;
285
291 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType* retrieveValid();
292 [[nodiscard]] typename TDataAccessorManagerInfo::Manager::DataType const* retrieveValid() const;
293
294private:
295 using VariantType = std::variant<SharedDataType, SharedType>;
296 using Type = std::conditional_t<TOption == DataAccessorOptions::BLOCKPOINTER_ONLY, SharedDataType, VariantType>;
297
298 Type g_data;
299 std::string g_name;
300};
301
302template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
303static net::Packet const& operator>>(net::Packet const& pck, BaseDataAccessor<TDataAccessorManagerInfo, TOption>& data);
304template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
306
307template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
308static void to_json(nlohmann::json& j, BaseDataAccessor<TDataAccessorManagerInfo, TOption> const& p);
309template<class TDataAccessorManagerInfo, DataAccessorOptions TOption>
310static void from_json(nlohmann::json const& j, BaseDataAccessor<TDataAccessorManagerInfo, TOption>& p);
311
312} // namespace fge::manager
313
314#include "C_baseManager.inl"
315
316#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:199
SharedType const & getSharedData() const
Get the shared resource data.
Definition C_baseManager.inl:267
bool valid() const
Check if the font is valid (not unloaded)
Definition C_baseManager.inl:232
TDataAccessorManagerInfo::Manager::DataType * retrieve()
Retrieve the raw shared pointer from the current resource.
Definition C_baseManager.inl:340
SharedDataType const & getSharedBlock() const
Get the resource block data.
Definition C_baseManager.inl:250
virtual void clear()
Clear the resource.
Definition C_baseManager.inl:219
std::string const & getName() const
Get the name of the resource.
Definition C_baseManager.inl:282
TDataAccessorManagerInfo::Manager::DataType * retrieveValid()
Retrieve the raw shared pointer from the current resource only if the resource is valid.
Definition C_baseManager.inl:385
void reload()
Reload the cached resource from the same name.
Definition C_baseManager.inl:226
Map::const_iterator begin(AccessLock< std::mutex > const &lock) const
Get the "begin" iterator of the manager.
Definition C_baseManager.inl:52
DataBlockPointer getElement(std::string_view name) const
Get the resource with the given name.
Definition C_baseManager.inl:72
AccessLock< std::mutex > acquireLock() const
Acquire a AccessLock, with the manager mutex.
Definition C_baseManager.inl:46
std::size_t size() const
Get the number of elements in the manager.
Definition C_baseManager.inl:40
bool push(std::string_view name, DataBlockPointer block)
Add a user handled resource.
Definition C_baseManager.inl:166
DataBlockPointer const & getBadElement() const
Get the "bad" element.
Definition C_baseManager.inl:66
Map::const_iterator end(AccessLock< std::mutex > const &lock) const
Get the "end" iterator of the manager.
Definition C_baseManager.inl:59
virtual bool initialize()=0
Initialize the manager.
Definition C_packet.hpp:52
Definition C_baseManager.hpp:42
Definition C_baseManager.hpp:192