17#ifndef _FGE_C_BASEMANAGER_HPP_INCLUDED
18#define _FGE_C_BASEMANAGER_HPP_INCLUDED
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"
32#include <unordered_map>
35#define FGE_MANAGER_BAD std::string_view()
44 using DataPointer = std::shared_ptr<TData>;
46 inline virtual void unload() {}
48 [[nodiscard]]
inline virtual bool duplicate(
BaseDataBlock& block)
const
50 if constexpr (std::is_copy_constructible_v<TData>)
52 block._ptr = std::make_shared<TData>(*this->_ptr);
53 block._valid = this->_valid;
54 block._path = this->_path;
65 std::filesystem::path _path;
76template<
class TData,
class TDataBlock = BaseDataBlock<TData>>
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<>>;
85 BaseManager() =
default;
86 BaseManager(BaseManager
const& r) =
delete;
87 BaseManager(BaseManager&& r)
noexcept =
delete;
88 virtual ~BaseManager() =
default;
90 BaseManager& operator=(BaseManager
const& r) =
delete;
91 BaseManager& operator=(BaseManager&& r)
noexcept =
delete;
104 [[nodiscard]]
virtual bool isInitialized();
105 virtual void uninitialize();
112 [[nodiscard]] std::size_t
size()
const;
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);
165 bool duplicate(std::string_view name, std::string_view newName);
174 bool push(std::string_view name, DataBlockPointer block);
178 mutable std::mutex g_mutex;
181 DataBlockPointer _g_badElement;
184enum class DataAccessorOptions
187 ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER
190template<
class TManager, TManager* TGlobalManager>
193 using Manager = TManager;
194 [[nodiscard]]
inline constexpr TManager& operator()()
const {
return *TGlobalManager; }
197template<
class TDataAccessorManagerInfo, DataAccessorOptions TOption>
198class BaseDataAccessor
201 using SharedDataType =
typename TDataAccessorManagerInfo::Manager::DataBlockPointer;
202 using SharedType =
typename TDataAccessorManagerInfo::Manager::DataBlockType::DataPointer;
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;
233 virtual void clear();
245 [[nodiscard]]
bool valid()
const;
261 requires(TOption == DataAccessorOptions::ALLOW_VARIANT_OF_DATAPOINTER_AND_BLOCKPOINTER);
267 [[nodiscard]] std::string
const&
getName()
const;
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);
283 [[nodiscard]]
typename TDataAccessorManagerInfo::Manager::DataType*
retrieve();
284 [[nodiscard]]
typename TDataAccessorManagerInfo::Manager::DataType
const*
retrieve()
const;
291 [[nodiscard]]
typename TDataAccessorManagerInfo::Manager::DataType*
retrieveValid();
292 [[nodiscard]]
typename TDataAccessorManagerInfo::Manager::DataType
const*
retrieveValid()
const;
295 using VariantType = std::variant<SharedDataType, SharedType>;
296 using Type = std::conditional_t<TOption == DataAccessorOptions::BLOCKPOINTER_ONLY, SharedDataType, VariantType>;
302template<
class TDataAccessorManagerInfo, DataAccessorOptions TOption>
304template<
class TDataAccessorManagerInfo, DataAccessorOptions TOption>
307template<
class TDataAccessorManagerInfo, DataAccessorOptions TOption>
309template<
class TDataAccessorManagerInfo, DataAccessorOptions TOption>
314#include "C_baseManager.inl"
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