CVB++ 15.0
flow_set_pool.hpp
1#pragma once
2
3#include <functional>
4#include <algorithm>
5
6#include "../global.hpp"
7#include "driver.hpp"
8
9namespace 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
40
45 bool operator==(const FlowInfo& other) const noexcept
46 {
47 return (Size == other.Size && Alignment == other.Alignment);
48 }
49
50
52
57 bool operator!=(const FlowInfo& other) const noexcept
58 {
59 return !(*this == other);
60 }
61 };
62
63
65
67 {
68 friend class CompositeStreamBase;
69
70
71 std::vector<FlowInfo> flowInfos_;
73
74 std::function<void()> releaseThis_;
75 std::function<void()> userCallback_;
76
77 protected:
78 struct ProtectedTag {};
79
80 public:
82
84 FlowSetPool(const std::vector<FlowInfo>& flowInfos, ProtectedTag) noexcept
85 : flowInfos_{ flowInfos }
86 {
87 }
88
89 FlowSetPool(const FlowSetPool& other) = delete;
90 FlowSetPool& operator=(const FlowSetPool& other) = delete;
91 FlowSetPool(FlowSetPool&& other) = delete;
92 FlowSetPool& operator=(FlowSetPool&& other) = delete;
93 virtual ~FlowSetPool() = default;
94
96
101 {
102 return std::make_shared<FlowSetPool>(flowInfos, ProtectedTag{});
103 }
104
106
110 virtual void PushBack(const std::vector<void*>& flowBuffers)
111 {
112 if (flowBuffers.size() != flowInfos_.size())
113 throw std::length_error("index out of range");
114
115 // Create Flows from size and buffer
116 auto flows = std::vector<Cvb::Flow>(flowBuffers.size());
117 std::transform(flowBuffers.begin(), flowBuffers.end(), flowInfos_.begin(), flows.begin(), [](void* pBuffer, FlowInfo info) mutable
118 {
119 return Cvb::Flow { info.Size, pBuffer };
120 });
121 flowSets_.push_back(flows);
122 }
123
125
130 void push_back(const std::vector<void*>& flows)
131 {
132 PushBack(flows);
133 }
134
136
139 size_t PoolSize() const noexcept
140 {
141 return flowSets_.size();
142 }
143
145
149 void Reserve(int flowSets) noexcept
150 {
151 flowSets_.reserve(flowSets);
152 }
153
155
159 auto CBegin() const noexcept
160 {
161 return flowSets_.cbegin();
162 }
163
165
169 auto Begin() noexcept
170 {
171 return flowSets_.begin();
172 }
173
175
179 auto cbegin() const noexcept
180 {
181 return CBegin();
182 }
183
185
189 auto begin() noexcept
190 {
191 return Begin();
192 }
193
195
199 auto CEnd() const noexcept
200 {
201 return flowSets_.cend();
202 }
203
205
209 auto End() noexcept
210 {
211 return flowSets_.end();
212 }
213
215
219 auto cend() const noexcept
220 {
221 return CEnd();
222 }
223
225
229 auto end() noexcept
230 {
231 return End();
232 }
233
235
239 auto CRBegin() const noexcept
240 {
241 return flowSets_.crbegin();
242 }
243
245
249 auto RBegin() noexcept
250 {
251 return flowSets_.rbegin();
252 }
253
255
259 auto crbegin() const noexcept
260 {
261 return CRBegin();
262 }
263
265
269 auto rbegin() noexcept
270 {
271 return RBegin();
272 }
273
275
279 auto CREnd() const noexcept
280 {
281 return flowSets_.crend();
282 }
283
285
289 auto REnd() const noexcept
290 {
291 return flowSets_.rend();
292 }
293
295
299 auto crend() const noexcept
300 {
301 return CREnd();
302 }
303
305
309 auto rend() noexcept
310 {
311 return REnd();
312 }
313
314 private:
315 void SetReleaseThis(std::shared_ptr<FlowSetPool>& flowSetPoolPtr) noexcept
316 {
317 releaseThis_ = [flowSetPool = flowSetPoolPtr]() mutable
318 {
319 flowSetPool.reset();
320 };
321 }
322
323 void SetUserCallback(std::function<void()> userCallback)
324 {
325 userCallback_ = userCallback;
326 }
327
328 std::vector<CExports::CVDFlowSet> ToNativeStruct()
329 {
330 std::vector<CExports::CVDFlowSet> flowSetPool(flowSets_.size());
331 std::transform(flowSets_.begin(), flowSets_.end(), flowSetPool.begin(), [this](std::vector<Cvb::Flow>& flows)
332 {
333 return CExports::CVDFlowSet{ 0, flowInfos_.size(), reinterpret_cast<CExports::CVDFlow*>(flows.data()) };
334 });
335 return flowSetPool;
336 }
337
338 static CExports::cvbres_t __stdcall OnReleaseCallback(void* pPrivate)
339 {
340 auto flowSetPool = reinterpret_cast<FlowSetPool*>(pPrivate);
341 try
342 {
343 flowSetPool->userCallback_();
344 }
345 catch (...)
346 {
347 flowSetPool->releaseThis_();
349 }
350
351 flowSetPool->releaseThis_();
352 return CExports::CVC_E_OK;
353 }
354 };
355
356 }
357
358 using Driver::FlowInfo;
359
360 CVB_END_INLINE_NS
361
362}
Base class of all composite based streams.
Definition: decl_composite_stream_base.hpp:32
FlowSetPool class to set external buffers as set of flows.
Definition: flow_set_pool.hpp:67
auto CREnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:279
auto REnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:289
virtual void PushBack(const std::vector< void * > &flowBuffers)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:110
void push_back(const std::vector< void * > &flows)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:130
auto cend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:219
auto begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:189
auto CRBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:239
auto crend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:299
static FlowSetPoolPtr Create(const std::vector< FlowInfo > &flowInfos)
Creates a new flow set pool.
Definition: flow_set_pool.hpp:100
auto crbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:259
auto rend() noexcept
Iterator access.
Definition: flow_set_pool.hpp:309
size_t PoolSize() const noexcept
Gets the size of the flow set pool.
Definition: flow_set_pool.hpp:139
auto End() noexcept
Iterator access.
Definition: flow_set_pool.hpp:209
auto end() noexcept
Iterator access.
Definition: flow_set_pool.hpp:229
auto RBegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:249
auto CBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:159
auto rbegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:269
FlowSetPool(const std::vector< FlowInfo > &flowInfos, ProtectedTag) noexcept
Flow set pool constructor.
Definition: flow_set_pool.hpp:84
void Reserve(int flowSets) noexcept
Reserve flow sets in the flow set pool.
Definition: flow_set_pool.hpp:149
auto Begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:169
auto CEnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:199
auto cbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:179
@ CVC_E_OK
No Error occurred.
Definition: CVCError.h:58
@ CVC_E_ERROR
Definition: CVCError.h:61
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Struct handling the size, alignment and number of flows per set.
Definition: flow_set_pool.hpp:19
size_t Size
Flow size in bytes.
Definition: flow_set_pool.hpp:21
size_t Alignment
Flow alignment in bytes.
Definition: flow_set_pool.hpp:24
bool operator!=(const FlowInfo &other) const noexcept
Compares to an other flow set info.
Definition: flow_set_pool.hpp:57
bool operator==(const FlowInfo &other) const noexcept
Compares to an other flow set info.
Definition: flow_set_pool.hpp:45
FlowInfo(size_t size, size_t alignment)
Construct a flow.
Definition: flow_set_pool.hpp:33