CVB++ 14.1
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 };
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 FlowSetPool(const FlowSetPool& other) = delete;
67 FlowSetPool& operator=(const FlowSetPool& other) = delete;
68 FlowSetPool(FlowSetPool&& other) = delete;
69 FlowSetPool& operator=(FlowSetPool&& other) = delete;
70 virtual ~FlowSetPool() = default;
71
73
78 {
79 return std::make_shared<FlowSetPool>(flowInfos, ProtectedTag{});
80 }
81
83
87 virtual void PushBack(const std::vector<void*>& flowBuffers)
88 {
89 if (flowBuffers.size() != flowInfos_.size())
90 throw std::length_error("index out of range");
91
92 // Create Flows from size and buffer
93 auto flows = std::vector<Cvb::Flow>(flowBuffers.size());
94 std::transform(flowBuffers.begin(), flowBuffers.end(), flowInfos_.begin(), flows.begin(), [](void* pBuffer, FlowInfo info) mutable
95 {
96 return Cvb::Flow { info.Size, pBuffer };
97 });
98 flowSets_.push_back(flows);
99 }
100
102
107 void push_back(const std::vector<void*>& flows)
108 {
109 PushBack(flows);
110 }
111
113
116 size_t PoolSize() const noexcept
117 {
118 return flowSets_.size();
119 }
120
122
126 void Reserve(int flowSets) noexcept
127 {
128 flowSets_.reserve(flowSets);
129 }
130
132
136 auto CBegin() const noexcept
137 {
138 return flowSets_.cbegin();
139 }
140
142
146 auto Begin() noexcept
147 {
148 return flowSets_.begin();
149 }
150
152
156 auto cbegin() const noexcept
157 {
158 return CBegin();
159 }
160
162
166 auto begin() noexcept
167 {
168 return Begin();
169 }
170
172
176 auto CEnd() const noexcept
177 {
178 return flowSets_.cend();
179 }
180
182
186 auto End() noexcept
187 {
188 return flowSets_.end();
189 }
190
192
196 auto cend() const noexcept
197 {
198 return CEnd();
199 }
200
202
206 auto end() noexcept
207 {
208 return End();
209 }
210
212
216 auto CRBegin() const noexcept
217 {
218 return flowSets_.crbegin();
219 }
220
222
226 auto RBegin() noexcept
227 {
228 return flowSets_.rbegin();
229 }
230
232
236 auto crbegin() const noexcept
237 {
238 return CRBegin();
239 }
240
242
246 auto rbegin() noexcept
247 {
248 return RBegin();
249 }
250
252
256 auto CREnd() const noexcept
257 {
258 return flowSets_.crend();
259 }
260
262
266 auto REnd() const noexcept
267 {
268 return flowSets_.rend();
269 }
270
272
276 auto crend() const noexcept
277 {
278 return CREnd();
279 }
280
282
286 auto rend() noexcept
287 {
288 return REnd();
289 }
290
291 private:
292 void SetReleaseThis(std::shared_ptr<FlowSetPool>& flowSetPoolPtr) noexcept
293 {
294 releaseThis_ = [flowSetPool = flowSetPoolPtr]() mutable
295 {
296 flowSetPool.reset();
297 };
298 }
299
300 void SetUserCallback(std::function<void()> userCallback)
301 {
302 userCallback_ = userCallback;
303 }
304
305 std::vector<CExports::CVDFlowSet> ToNativeStruct()
306 {
307 std::vector<CExports::CVDFlowSet> flowSetPool(flowSets_.size());
308 std::transform(flowSets_.begin(), flowSets_.end(), flowSetPool.begin(), [this](std::vector<Cvb::Flow>& flows)
309 {
310 return CExports::CVDFlowSet{ 0, flowInfos_.size(), reinterpret_cast<CExports::CVDFlow*>(flows.data()) };
311 });
312 return flowSetPool;
313 }
314
315 static CExports::cvbres_t __stdcall OnReleaseCallback(void* pPrivate)
316 {
317 auto flowSetPool = reinterpret_cast<FlowSetPool*>(pPrivate);
318 try
319 {
320 flowSetPool->userCallback_();
321 }
322 catch (...)
323 {
324 flowSetPool->releaseThis_();
326 }
327
328 flowSetPool->releaseThis_();
329 return CExports::CVC_E_OK;
330 }
331 };
332
333 }
334
335 using Driver::FlowInfo;
336
337 CVB_END_INLINE_NS
338
339}
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:44
auto CREnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:256
auto REnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:266
virtual void PushBack(const std::vector< void * > &flowBuffers)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:87
void push_back(const std::vector< void * > &flows)
Sets a flow to the flow set pool.
Definition: flow_set_pool.hpp:107
auto cend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:196
auto begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:166
auto CRBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:216
auto crend() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:276
static FlowSetPoolPtr Create(const std::vector< FlowInfo > &flowInfos)
Creates a new flow set pool.
Definition: flow_set_pool.hpp:77
auto crbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:236
auto rend() noexcept
Iterator access.
Definition: flow_set_pool.hpp:286
size_t PoolSize() const noexcept
Gets the size of the flow set pool.
Definition: flow_set_pool.hpp:116
auto End() noexcept
Iterator access.
Definition: flow_set_pool.hpp:186
auto end() noexcept
Iterator access.
Definition: flow_set_pool.hpp:206
auto RBegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:226
auto CBegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:136
auto rbegin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:246
FlowSetPool(const std::vector< FlowInfo > &flowInfos, ProtectedTag) noexcept
Flow set pool constructor.
Definition: flow_set_pool.hpp:61
void Reserve(int flowSets) noexcept
Reserve flow sets in the flow set pool.
Definition: flow_set_pool.hpp:126
auto Begin() noexcept
Iterator access.
Definition: flow_set_pool.hpp:146
auto CEnd() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:176
auto cbegin() const noexcept
Iterator access.
Definition: flow_set_pool.hpp:156
@ 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
FlowInfo(size_t size, size_t alignment)
Construct a flow.
Definition: flow_set_pool.hpp:33