le2d v0.4.8
2D game engine framework
 
Loading...
Searching...
No Matches
asset_loader.hpp
Go to the documentation of this file.
1#pragma once
3#include <klib/assert.hpp>
4#include <gsl/pointers>
5#include <memory>
6#include <string_view>
7#include <typeindex>
8#include <unordered_map>
9
10namespace le {
13 public:
16 void add_loader(std::unique_ptr<detail::IAssetTypeLoaderBase> loader);
17
20 template <std::derived_from<IAsset> AssetTypeT>
21 [[nodiscard]] auto has_loader() const -> bool {
22 return m_map.contains(typeid(AssetTypeT));
23 }
24
29 template <std::derived_from<IAsset> AssetTypeT>
30 [[nodiscard]] auto load(std::string_view const uri) const -> std::unique_ptr<AssetTypeT> {
31 auto ret = load_impl(typeid(AssetTypeT), uri);
32 if (!ret) { return {}; }
33 KLIB_ASSERT(dynamic_cast<AssetTypeT*>(ret.get()));
34 return std::unique_ptr<AssetTypeT>{dynamic_cast<AssetTypeT*>(ret.release())};
35 }
36
37 private:
38 [[nodiscard]] auto load_impl(std::type_info const& type, std::string_view uri) const -> std::unique_ptr<IAsset>;
39
40 std::unordered_map<std::type_index, std::unique_ptr<detail::IAssetTypeLoaderBase>> m_map{};
41};
42} // namespace le
Set of AssetTypeLoaders mapped to their corrseponding AssetTypes.
Definition asset_loader.hpp:12
void add_loader(std::unique_ptr< detail::IAssetTypeLoaderBase > loader)
Associate a loader with its type.
auto has_loader() const -> bool
Definition asset_loader.hpp:21
auto load(std::string_view const uri) const -> std::unique_ptr< AssetTypeT >
Load an asset given its URI.
Definition asset_loader.hpp:30
Definition animation.hpp:8