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

block.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MEMORY_BLOCK_HPP
10 #define EAGINE_MEMORY_BLOCK_HPP
11 
12 #include "../assert.hpp"
13 #include "span.hpp"
14 #include <cstddef>
15 #include <utility>
16 
17 namespace eagine::memory {
18 //------------------------------------------------------------------------------
21 template <bool IsConst>
23 //------------------------------------------------------------------------------
28 
33 //------------------------------------------------------------------------------
38 template <typename T, typename P, typename S>
39 static constexpr auto as_bytes(basic_span<T, P, S> spn) noexcept
41  return {spn.begin_addr(), spn.end_addr()};
42 }
43 //------------------------------------------------------------------------------
48 static constexpr auto as_chars(block blk) noexcept {
49  return accomodate<char>(blk);
50 }
51 //------------------------------------------------------------------------------
56 static constexpr auto as_chars(const_block blk) noexcept {
57  return accomodate<const char>(blk);
58 }
59 //------------------------------------------------------------------------------
60 class block_owner;
61 //------------------------------------------------------------------------------
66 class owned_block : public block {
67 public:
70  constexpr owned_block() noexcept = default;
71 
73  owned_block(owned_block&& temp) noexcept
74  : block(static_cast<const block&>(temp)) {
75  temp.reset();
76  }
77 
79  auto operator=(owned_block&& temp) noexcept -> owned_block& {
80  if(this != std::addressof(temp)) {
81  static_cast<block&>(*this) = static_cast<const block&>(temp);
82  temp.reset();
83  }
84  return *this;
85  }
86 
88  owned_block(const owned_block&) = delete;
89 
91  auto operator=(const owned_block&) = delete;
92 
95  ~owned_block() noexcept {
96  EAGINE_ASSERT(empty());
97  }
98 
99 private:
100  friend class block_owner;
101 
102  constexpr owned_block(block b) noexcept
103  : block{b} {}
104 };
105 //------------------------------------------------------------------------------
109 class block_owner {
110 protected:
112  [[nodiscard]] static auto acquire_block(block b) noexcept -> owned_block {
113  return {b};
114  }
115 
117  static void release_block(owned_block&& b) noexcept {
118  b.reset();
119  }
120 };
121 //------------------------------------------------------------------------------
122 } // namespace eagine::memory
123 
124 #endif // EAGINE_MEMORY_BLOCK_HPP
constexpr auto empty() const noexcept -> bool
Indicates that the span is empty.
Definition: span.hpp:235
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
auto operator=(owned_block &&temp) noexcept -> owned_block &
Move assignment operator.
Definition: block.hpp:79
constexpr owned_block() noexcept=default
Default constructor.
Specialization of block indicating byte span ownership.
Definition: block.hpp:66
basic_block< false > block
Alias for non-const byte memory span.
Definition: block.hpp:27
Base class for classes that act as memory block owners.
Definition: block.hpp:109
auto reset() noexcept -> auto &
Resets this span.
Definition: span.hpp:200
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static void release_block(owned_block &&b) noexcept
Should be called to release the ownership of a memory block.
Definition: block.hpp:117
constexpr auto begin_addr() const noexcept -> address_type
Returns the memory address of the start of the span.
Definition: span.hpp:284
~owned_block() noexcept
Destructor.
Definition: block.hpp:95
static constexpr auto as_chars(const_block blk) noexcept
Converts a block into a span of characters.
Definition: block.hpp:56
static auto acquire_block(block b) noexcept -> owned_block
Should be called to take the ownership of a memory block.
Definition: block.hpp:112
owned_block(owned_block &&temp) noexcept
Move constructor.
Definition: block.hpp:73

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