flow_set_pool.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <algorithm>
5 
6 #include "../global.hpp"
7 #include "driver.hpp"
8 
9 namespace Cvb
10 {
11 
12  CVB_BEGIN_INLINE_NS
13 
14  namespace Driver
15  {
17 
18  struct FlowInfo
19  {
21  size_t Size = 0;
22 
24  size_t Alignment = 0;
25 
26  FlowInfo() = default;
27 
29 
33  FlowInfo(size_t size, size_t alignment)
34  : Size{ size }
35  , Alignment{ alignment }
36  {
37  }
38  };
39 
40 
42 
44  {
45  friend class CompositeStreamBase;
46 
47 
48  std::vector<FlowInfo> flowInfos_;
50 
51  std::function<void()> releaseThis_;
52  std::function<void()> userCallback_;
53 
54  protected:
55  struct ProtectedTag {};
56 
57  public:
59 
61  FlowSetPool(const std::vector<FlowInfo>& flowInfos, ProtectedTag) noexcept
62  : flowInfos_{ flowInfos }
63  {
64  }
65 
66  virtual ~FlowSetPool() = default;
67 
69 
73  static FlowSetPoolPtr Create(const std::vector<FlowInfo>& flowInfos)
74  {
75  return std::make_shared<FlowSetPool>(flowInfos, ProtectedTag{});
76  }
77 
79 
83  virtual void PushBack(const std::vector<void*>& flowBuffers)
84  {
85  if (flowBuffers.size() != flowInfos_.size())
86  throw std::length_error("index out of range");
87 
88  // Create Flows from size and buffer
89  auto flows = std::vector<Cvb::Flow>(flowBuffers.size());
90  std::transform(flowBuffers.begin(), flowBuffers.end(), flowInfos_.begin(), flows.begin(), [](void* pBuffer, FlowInfo info) mutable
91  {
92  return Cvb::Flow { info.Size, pBuffer };
93  });
94  flowSets_.push_back(flows);
95  }
96 
98 
103  void push_back(const std::vector<void*>& flows)
104  {
105  PushBack(flows);
106  }
107 
109 
112  size_t PoolSize() const noexcept
113  {
114  return flowSets_.size();
115  }
116 
118 
122  void Reserve(int flowSets) noexcept
123  {
124  flowSets_.reserve(flowSets);
125  }
126 
128 
132  auto CBegin() const noexcept
133  {
134  return flowSets_.cbegin();
135  }
136 
138 
142  auto Begin() noexcept
143  {
144  return flowSets_.begin();
145  }
146 
148 
152  auto cbegin() const noexcept
153  {
154  return CBegin();
155  }
156 
158 
162  auto begin() noexcept
163  {
164  return Begin();
165  }
166 
168 
172  auto CEnd() const noexcept
173  {
174  return flowSets_.cend();
175  }
176 
178 
182  auto End() noexcept
183  {
184  return flowSets_.end();
185  }
186 
188 
192  auto cend() const noexcept
193  {
194  return CEnd();
195  }
196 
198 
202  auto end() noexcept
203  {
204  return End();
205  }
206 
208 
212  auto CRBegin() const noexcept
213  {
214  return flowSets_.crbegin();
215  }
216 
218 
222  auto RBegin() noexcept
223  {
224  return flowSets_.rbegin();
225  }
226 
228 
232  auto crbegin() const noexcept
233  {
234  return CRBegin();
235  }
236 
238 
242  auto rbegin() noexcept
243  {
244  return RBegin();
245  }
246 
248 
252  auto CREnd() const noexcept
253  {
254  return flowSets_.crend();
255  }
256 
258 
262  auto REnd() const noexcept
263  {
264  return flowSets_.rend();
265  }
266 
268 
272  auto crend() const noexcept
273  {
274  return CREnd();
275  }
276 
278 
282  auto rend() noexcept
283  {
284  return REnd();
285  }
286 
287  private:
288  void SetReleaseThis(std::shared_ptr<FlowSetPool>& flowSetPoolPtr) noexcept
289  {
290  releaseThis_ = [flowSetPool = flowSetPoolPtr]() mutable
291  {
292  flowSetPool.reset();
293  };
294  }
295 
296  void SetUserCallback(std::function<void()> userCallback)
297  {
298  userCallback_ = userCallback;
299  }
300 
301  std::vector<CExports::CVDFlowSet> ToNativeStruct()
302  {
303  std::vector<CExports::CVDFlowSet> flowSetPool(flowSets_.size());
304  std::transform(flowSets_.begin(), flowSets_.end(), flowSetPool.begin(), [this](std::vector<Cvb::Flow>& flows)
305  {
306  return CExports::CVDFlowSet{ 0, flowInfos_.size(), reinterpret_cast<CExports::CVDFlow*>(flows.data()) };
307  });
308  return flowSetPool;
309  }
310 
311  static CExports::cvbres_t __stdcall OnReleaseCallback(void* pPrivate)
312  {
313  auto flowSetPool = reinterpret_cast<FlowSetPool*>(pPrivate);
314  try
315  {
316  flowSetPool->userCallback_();
317  }
318  catch (...)
319  {
320  flowSetPool->releaseThis_();
321  return CExports::CVC_E_ERROR;
322  }
323 
324  flowSetPool->releaseThis_();
325  return CExports::CVC_E_OK;
326  }
327  };
328 
329  }
330 
331  using Driver::FlowInfo;
332 
333  CVB_END_INLINE_NS
334 
335 }
auto Begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:142
STL class.
FlowInfo(size_t size, size_t alignment)
Construct a flow.
Definition: flow_set_pool.hpp:33
size_t Size
Flow size in bytes.
Definition: driver.hpp:151
auto rbegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:242
auto cbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:152
auto begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:162
Struct handling the size, alignment and number of flows per set.
Definition: flow_set_pool.hpp:18
FlowSetPool class to set external buffers as set of flows.
Definition: flow_set_pool.hpp:43
auto crbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:232
auto CRBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:212
Base class of all composite based streams.
Definition: decl_composite_stream_base.hpp:30
auto RBegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:222
Root namespace for the Image Manager interface.
Definition: version.hpp:11
auto end() noexcept
Iterator access.
Definition: flow_set_pool.hpp:202
auto rend() noexcept
Iterator access.
Definition: flow_set_pool.hpp:282
auto End() noexcept
Iterator access.
Definition: flow_set_pool.hpp:182
void push_back(const std::vector< void * > &flows)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:103
void Reserve(int flowSets) noexcept
Reserve flow sets in the flow set pool.
Definition: flow_set_pool.hpp:122
size_t Size
Flow size in bytes.
Definition: flow_set_pool.hpp:21
auto CEnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:172
STL class.
auto CBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:132
auto CREnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:252
virtual void PushBack(const std::vector< void * > &flowBuffers)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:83
size_t PoolSize() const noexcept
Gets the size of the flow set pool.
Definition: flow_set_pool.hpp:112
size_t Alignment
Flow alignment in bytes.
Definition: flow_set_pool.hpp:24
auto crend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:272
Struct handling the size and buffer of a flow.
Definition: driver.hpp:148
static FlowSetPoolPtr Create(const std::vector< FlowInfo > &flowInfos)
Creates a new flow set pool.
Definition: flow_set_pool.hpp:73
FlowSetPool(const std::vector< FlowInfo > &flowInfos, ProtectedTag) noexcept
Flow set pool constructor.
Definition: flow_set_pool.hpp:61
auto REnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:262
auto cend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:192