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

struct_memory_block.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_STRUCT_MEMORY_BLOCK_HPP
10 #define EAGINE_STRUCT_MEMORY_BLOCK_HPP
11 
12 #include "assert.hpp"
13 #include "memory/block.hpp"
14 #include <type_traits>
15 
16 namespace eagine {
17 
18 template <typename T>
19 class structured_memory_block {
20 private:
21  memory::basic_block<std::is_const_v<T>> _blk;
22 
23  template <typename X = T, typename = std::enable_if_t<!std::is_const_v<X>>>
24  auto _ptr() noexcept {
25  EAGINE_ASSERT(is_valid_block(_blk));
26  return static_cast<X*>(_blk.addr());
27  }
28 
29  auto _cptr() const noexcept {
30  EAGINE_ASSERT(is_valid_block(_blk));
31  return static_cast<const T*>(_blk.addr());
32  }
33 
34 public:
35  static auto is_valid_block(memory::const_block blk) noexcept -> bool {
36  return !blk.empty() && (blk.is_aligned_as<T>()) &&
37  (can_accomodate(blk, type_identity<T>()));
38  }
39 
40  structured_memory_block(memory::basic_block<std::is_const_v<T>> blk) noexcept
41  : _blk(blk) {
42  EAGINE_ASSERT(is_valid_block(_blk));
43  }
44 
45  template <
46  typename X = T,
47  typename = std::enable_if_t<!std::is_const_v<X> && std::is_same_v<X, T>>>
48  auto get() noexcept -> X& {
49  return *_ptr();
50  }
51 
52  template <
53  typename X = T,
54  typename = std::enable_if_t<!std::is_const_v<X> && std::is_same_v<X, T>>>
55  auto operator->() noexcept -> X* {
56  return _ptr();
57  }
58 
59  auto get() const noexcept -> const T& {
60  return *_cptr();
61  }
62 
63  auto operator->() const noexcept -> const T* {
64  return _cptr();
65  }
66 };
67 
68 } // namespace eagine
69 
70 #endif // EAGINE_STRUCT_MEMORY_BLOCK_HPP
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto can_accomodate(basic_span< B, P, S > blk, span_size_t count, type_identity< T > tid={}) noexcept
Indicates if the specified memory block can accomodate count elements of T.
Definition: span.hpp:539
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
basic_span< std::conditional_t< IsConst, const byte, byte > > basic_block
Alias for byte spans.
Definition: block.hpp:22

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