opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
resource_loader.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <concepts>
10#include <functional>
11#include <memory>
12#include <optional>
13#include <string>
14#include <utility>
15
16namespace osc
17{
18 // An accessor that loads resources within a prefix (i.e. a directory)
19 // relative to the root of some `VirtualFilesystem`.
20 //
21 // Example: An implementation might combine `VirtualFilesystem`s with
22 // a `LayeredFilesystem` and prefix it with `textures/`, as part of a
23 // "texture loader" subsystem.
25 public:
27 std::shared_ptr<VirtualFilesystem> impl,
28 ResourcePath prefix = ResourcePath{}) :
29
30 impl_{std::move(impl)},
31 prefix_{std::move(prefix)}
32 {}
33
35 {
36 return impl_->resource_exists(resource_path);
37 }
38
40 {
41 return impl_->open(prefix_ / resource_path);
42 }
43
44 std::string slurp(const ResourcePath& resource_path)
45 {
46 return impl_->slurp(prefix_ / resource_path);
47 }
48
50 {
51 return ResourceLoader{impl_, prefix_ / prefix};
52 }
53
54 template<typename StringLike>
56 requires std::constructible_from<ResourceLoader, StringLike&&>
57 {
58 return with_prefix(ResourcePath{std::forward<StringLike>(str)});
59 }
60
62 {
63 return impl_->iterate_directory(prefix_ / resource_path);
64 }
65 private:
66 template<std::derived_from<VirtualFilesystem> TResourceLoader, typename... Args>
67 requires std::constructible_from<TResourceLoader, Args&&...>
69
70 std::shared_ptr<VirtualFilesystem> impl_;
71 ResourcePath prefix_;
72 };
73
74 template<std::derived_from<VirtualFilesystem> TResourceLoader, typename... Args>
75 requires std::constructible_from<TResourceLoader, Args&&...>
77 {
78 return ResourceLoader{std::make_shared<TResourceLoader>(std::forward<Args>(args)...)};
79 }
80}
Definition resource_loader.h:24
ResourceLoader(std::shared_ptr< VirtualFilesystem > impl, ResourcePath prefix=ResourcePath{})
Definition resource_loader.h:26
friend ResourceLoader make_resource_loader(Args &&...)
ResourceLoader with_prefix(const ResourcePath &prefix) const
Definition resource_loader.h:49
bool resource_exists(const ResourcePath &resource_path)
Definition resource_loader.h:34
cpp23::generator< ResourceDirectoryEntry > iterate_directory(const ResourcePath &resource_path)
Definition resource_loader.h:61
ResourceLoader with_prefix(StringLike &&str) const
Definition resource_loader.h:55
std::string slurp(const ResourcePath &resource_path)
Definition resource_loader.h:44
ResourceStream open(const ResourcePath &resource_path)
Definition resource_loader.h:39
Definition resource_path.h:12
Definition resource_stream.h:11
Definition generator.h:32
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
ResourceLoader make_resource_loader(Args &&... args)
Definition resource_loader.h:76