OGLplus  (0.59.0) a C++ wrapper for rendering APIs

embed.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_EMBED_HPP
10 #define EAGINE_EMBED_HPP
11 
12 #include "assert.hpp"
13 #include "compression.hpp"
14 #include "identifier.hpp"
15 #include "int_constant.hpp"
16 #include "main_ctx.hpp"
17 #include "memory/copy.hpp"
18 #include "string_span.hpp"
19 
20 namespace eagine {
21 
26 public:
28  constexpr embedded_resource() noexcept = default;
29 
30  constexpr embedded_resource(
32  string_view src_path,
33  bool packed = false) noexcept
34  : _res_blk{blk}
35  , _src_path{src_path}
36  , _packed{packed} {}
37 
39  constexpr auto source_path() const noexcept -> string_view {
40  return _src_path;
41  }
42 
45  constexpr auto is_packed() const noexcept -> bool {
46  return _packed;
47  }
48 
50  constexpr operator memory::const_block() const noexcept {
51  EAGINE_ASSERT(!is_packed());
52  return _res_blk;
53  }
54 
57  auto unpack(data_compressor& comp, memory::buffer& buf) const
59  if(is_packed()) {
60  return {comp.decompress(_res_blk, buf)};
61  }
62  return copy_into(_res_blk, buf);
63  }
64 
67  auto unpack(main_ctx& ctx) const -> memory::const_block {
68  return unpack(ctx.compressor(), ctx.scratch_space());
69  }
70 
74  return unpack(mco.main_context());
75  }
76 
77 private:
78  memory::const_block _res_blk{};
79  string_view _src_path{};
80  bool _packed{false};
81 };
82 
86 static inline auto as_chars(const embedded_resource& res) noexcept {
87  return as_chars(memory::const_block{res});
88 }
89 
90 extern auto
91 get_embedded_resource(identifier res_id, string_view src_path) noexcept
93 
105 static inline auto embed(identifier res_id, string_view src_path) noexcept
106  -> embedded_resource {
107  return get_embedded_resource(res_id, src_path);
108 }
109 
110 } // namespace eagine
111 
112 #endif // EAGINE_EMBED_HPP
Class for a single-instance object providing useful information ans services.
Definition: main_ctx.hpp:45
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
auto unpack(main_ctx_object &mco) const -> memory::const_block
Unpacks this resource using compressor from a main context object.
Definition: embed.hpp:73
auto unpack(main_ctx &ctx) const -> memory::const_block
Unpacks this resource into a buffer using compressor from main context.
Definition: embed.hpp:67
static auto copy_into(const_block source, buffer &dest) -> block
Copies the content of source block to destination buffer.
Definition: copy.hpp:33
Base class for main context objects.
Definition: main_ctx_object.hpp:71
Common code is placed in this namespace.
Definition: eagine.hpp:21
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
Class providing access to a const resource block embedded into the executable.
Definition: embed.hpp:25
constexpr embedded_resource() noexcept=default
Default constructor.
Reallocatable owning byte buffer.
Definition: buffer.hpp:22
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
auto unpack(data_compressor &comp, memory::buffer &buf) const -> memory::const_block
Unpacks this resource into a buffer using the provided compressor.
Definition: embed.hpp:57
static auto as_chars(const embedded_resource &res) noexcept
Converts the embedded resource block to a const span of characters.
Definition: embed.hpp:86
Class implementing data compression and decompresson.
Definition: compression.hpp:37
constexpr auto source_path() const noexcept -> string_view
Returns the path of the file this resource data comes from.
Definition: embed.hpp:39
constexpr auto is_packed() const noexcept -> bool
Indicates if the resource is packed and needs to be decompressed.
Definition: embed.hpp:45
static constexpr auto as_chars(block blk) noexcept
Converts a block into a span of characters.
Definition: block.hpp:48
static auto embed(identifier res_id, string_view src_path) noexcept -> embedded_resource
Triggers the embedding of data from a file on the specified path.
Definition: embed.hpp:105

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).