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

implementation.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_VALUE_TREE_IMPLEMENTATION_HPP
10 #define EAGINE_VALUE_TREE_IMPLEMENTATION_HPP
11 
12 #include "interface.hpp"
13 #include <memory>
14 #include <vector>
15 
16 namespace eagine::valtree {
17 //------------------------------------------------------------------------------
18 template <typename Derived>
19 class compound_implementation : public compound_interface {
20 private:
21  auto derived() noexcept -> Derived& {
22  return *static_cast<Derived*>(this);
23  }
24 
25 public:
26  auto fetch_values(
27  attribute_interface& attrib,
28  span_size_t offset,
29  span<bool> dest) -> span_size_t final {
30  return derived().do_fetch_values(attrib, offset, dest);
31  }
32 
33  auto fetch_values(
34  attribute_interface& attrib,
35  span_size_t offset,
36  span<char> dest) -> span_size_t final {
37  return derived().do_fetch_values(attrib, offset, dest);
38  }
39 
40  auto fetch_values(
41  attribute_interface& attrib,
42  span_size_t offset,
43  span<byte> dest) -> span_size_t final {
44  return derived().do_fetch_values(attrib, offset, dest);
45  }
46 
47  auto fetch_values(
48  attribute_interface& attrib,
49  span_size_t offset,
50  span<std::int16_t> dest) -> span_size_t final {
51  return derived().do_fetch_values(attrib, offset, dest);
52  }
53 
54  auto fetch_values(
55  attribute_interface& attrib,
56  span_size_t offset,
57  span<std::int32_t> dest) -> span_size_t final {
58  return derived().do_fetch_values(attrib, offset, dest);
59  }
60 
61  auto fetch_values(
62  attribute_interface& attrib,
63  span_size_t offset,
64  span<std::int64_t> dest) -> span_size_t final {
65  return derived().do_fetch_values(attrib, offset, dest);
66  }
67 
68  auto fetch_values(
69  attribute_interface& attrib,
70  span_size_t offset,
71  span<std::uint16_t> dest) -> span_size_t final {
72  return derived().do_fetch_values(attrib, offset, dest);
73  }
74 
75  auto fetch_values(
76  attribute_interface& attrib,
77  span_size_t offset,
78  span<std::uint32_t> dest) -> span_size_t final {
79  return derived().do_fetch_values(attrib, offset, dest);
80  }
81 
82  auto fetch_values(
83  attribute_interface& attrib,
84  span_size_t offset,
85  span<std::uint64_t> dest) -> span_size_t final {
86  return derived().do_fetch_values(attrib, offset, dest);
87  }
88 
89  auto fetch_values(
90  attribute_interface& attrib,
91  span_size_t offset,
92  span<float> dest) -> span_size_t final {
93  return derived().do_fetch_values(attrib, offset, dest);
94  }
95 
96  auto fetch_values(
97  attribute_interface& attrib,
98  span_size_t offset,
99  span<std::chrono::duration<float>> dest) -> span_size_t final {
100  return derived().do_fetch_values(attrib, offset, dest);
101  }
102 
103  auto fetch_values(
104  attribute_interface& attrib,
105  span_size_t offset,
106  span<std::string> dest) -> span_size_t final {
107  return derived().do_fetch_values(attrib, offset, dest);
108  }
109 };
110 //------------------------------------------------------------------------------
111 template <typename Derived, typename Node>
112 class compound_with_refcounted_node : public compound_implementation<Derived> {
113 private:
114  std::vector<std::tuple<span_size_t, std::unique_ptr<Node>>> _nodes{};
115 
116  auto _do_make_new(Node&& temp) -> Node* {
117  for(auto& [ref_count, node_ptr] : _nodes) {
118  if(temp == *node_ptr) {
119  ++ref_count;
120  return node_ptr.get();
121  }
122  }
123  _nodes.emplace_back(1, std::make_unique<Node>(std::move(temp)));
124  return std::get<1>(_nodes.back()).get();
125  }
126 
127 protected:
128  inline auto _unwrap(attribute_interface& attrib) const noexcept -> auto& {
129  EAGINE_ASSERT(attrib.type_id() == this->type_id());
130  EAGINE_ASSERT(dynamic_cast<Node*>(&attrib));
131  return static_cast<Node&>(attrib);
132  }
133 
134 public:
135  template <typename... Args>
136  auto make_node(Args&&... args) {
137  return _do_make_new(Node{std::forward<Args>(args)...});
138  }
139 
140  void add_ref(attribute_interface& attrib) noexcept final {
141  auto& that = _unwrap(attrib);
142  for(auto& [ref_count, node_ptr] : _nodes) {
143  if(&that == node_ptr.get()) {
144  ++ref_count;
145  }
146  }
147  }
148 
149  void release(attribute_interface& attrib) noexcept final {
150  auto& that = _unwrap(attrib);
151  for(auto pos = _nodes.begin(); pos != _nodes.end(); ++pos) {
152  auto& [ref_count, node_ptr] = *pos;
153  if(&that == node_ptr.get()) {
154  if(--ref_count <= 0) {
155  _nodes.erase(pos);
156  break;
157  }
158  }
159  }
160  }
161 };
162 //------------------------------------------------------------------------------
163 } // namespace eagine::valtree
164 
165 #endif // EAGINE_VALUE_TREE_IMPLEMENTATION_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
basic_span< T, T *, S > span
Default alias for basic memory spans with native pointer type.
Definition: span.hpp:415
Value-tree code is placed in this namespace.
Definition: eagine.hpp:53

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