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
51
56 bool operator!=(const FlowInfo &other) const noexcept
57 {
58 return !(*this == other);
59 }
60 };
61
63
65 {
66 friend class CompositeStreamBase;
67
68 std::vector<FlowInfo> flowInfos_;
70
71 std::function<void()> releaseThis_;
72 std::function<void()> userCallback_;
73
74 protected:
75 struct ProtectedTag
76 {
77 };
78
79 public:
80 using value_type = std::vector<void *>;
81
83 FlowSetPool(const std::vector<FlowInfo> &flowInfos, ProtectedTag) noexcept
84 : flowInfos_{flowInfos}
85 {
86 }
87
88 FlowSetPool(const FlowSetPool &other) = delete;
89 FlowSetPool &operator=(const FlowSetPool &other) = delete;
90 FlowSetPool(FlowSetPool &&other) = delete;
91 FlowSetPool &operator=(FlowSetPool &&other) = delete;
92 virtual ~FlowSetPool() = default;
93
95
100 {
101 return std::make_shared<FlowSetPool>(flowInfos, ProtectedTag{});
102 }
103
105
109 virtual void PushBack(const std::vector<void *> &flowBuffers)
110 {
111 if (flowBuffers.size() != flowInfos_.size())
112 throw std::length_error("index out of range");
113
114 // Create Flows from size and buffer
115 auto flows = std::vector<Cvb::Flow>(flowBuffers.size());
116 std::transform(flowBuffers.begin(), flowBuffers.end(), flowInfos_.begin(), flows.begin(),
117 [](void *pBuffer, FlowInfo info) mutable { return Cvb::Flow{info.Size, pBuffer}; });
118 flowSets_.push_back(flows);
119 }
120
122
128 {
129 PushBack(flows);
130 }
131
133
136 size_t PoolSize() const noexcept
137 {
138 return flowSets_.size();
139 }
140
142
146 void Reserve(int flowSets) noexcept
147 {
148 flowSets_.reserve(flowSets);
149 }
150
152
156 auto CBegin() const noexcept
157 {
158 return flowSets_.cbegin();
159 }
160
162
166 auto Begin() noexcept
167 {
168 return flowSets_.begin();
169 }
170
172
176 auto cbegin() const noexcept
177 {
178 return CBegin();
179 }
180
182
186 auto begin() noexcept
187 {
188 return Begin();
189 }
190
192
196 auto CEnd() const noexcept
197 {
198 return flowSets_.cend();
199 }
200
202
206 auto End() noexcept
207 {
208 return flowSets_.end();
209 }
210
212
216 auto cend() const noexcept
217 {
218 return CEnd();
219 }
220
222
226 auto end() noexcept
227 {
228 return End();
229 }
230
232
236 auto CRBegin() const noexcept
237 {
238 return flowSets_.crbegin();
239 }
240
242
246 auto RBegin() noexcept
247 {
248 return flowSets_.rbegin();
249 }
250
252
256 auto crbegin() const noexcept
257 {
258 return CRBegin();
259 }
260
262
266 auto rbegin() noexcept
267 {
268 return RBegin();
269 }
270
272
276 auto CREnd() const noexcept
277 {
278 return flowSets_.crend();
279 }
280
282
286 auto REnd() const noexcept
287 {
288 return flowSets_.rend();
289 }
290
292
296 auto crend() const noexcept
297 {
298 return CREnd();
299 }
300
302
306 auto rend() noexcept
307 {
308 return REnd();
309 }
310
311 private:
312 void SetReleaseThis(std::shared_ptr<FlowSetPool> &flowSetPoolPtr) noexcept
313 {
314 releaseThis_ = [flowSetPool = flowSetPoolPtr]() mutable { flowSetPool.reset(); };
315 }
316
317 void SetUserCallback(std::function<void()> userCallback)
318 {
319 userCallback_ = userCallback;
320 }
321
322 std::vector<CExports::CVDFlowSet> ToNativeStruct()
323 {
324 std::vector<CExports::CVDFlowSet> flowSetPool(flowSets_.size());
325 std::transform(flowSets_.begin(), flowSets_.end(), flowSetPool.begin(), [this](std::vector<Cvb::Flow> &flows) {
326 return CExports::CVDFlowSet{0, flowInfos_.size(), reinterpret_cast<CExports::CVDFlow *>(flows.data())};
327 });
328 return flowSetPool;
329 }
330
331 static CExports::cvbres_t __stdcall OnReleaseCallback(void *pPrivate)
332 {
333 auto flowSetPool = reinterpret_cast<FlowSetPool *>(pPrivate);
334 try
335 {
336 flowSetPool->userCallback_();
337 }
338 catch (...)
339 {
340 flowSetPool->releaseThis_();
341 return CExports::CVC_E_ERROR;
342 }
343
344 flowSetPool->releaseThis_();
345 return CExports::CVC_E_OK;
346 }
347 };
348
349 } // namespace Driver
350
351 using Driver::FlowInfo;
352
353 CVB_END_INLINE_NS
354
355} // namespace Cvb
FlowSetPool class to set external buffers as set of flows.
Definition flow_set_pool.hpp:65
auto CREnd() const noexcept
Iterator access.
Definition flow_set_pool.hpp:276
auto REnd() const noexcept
Iterator access.
Definition flow_set_pool.hpp:286
virtual void PushBack(const std::vector< void * > &flowBuffers)
Sets a flow to the flow set pool.
Definition flow_set_pool.hpp:109
void push_back(const std::vector< void * > &flows)
Sets a flow to the flow set pool.
Definition flow_set_pool.hpp:127
auto cend() const noexcept
Iterator access.
Definition flow_set_pool.hpp:216
auto begin() noexcept
Iterator access.
Definition flow_set_pool.hpp:186
auto CRBegin() const noexcept
Iterator access.
Definition flow_set_pool.hpp:236
auto crend() const noexcept
Iterator access.
Definition flow_set_pool.hpp:296
static FlowSetPoolPtr Create(const std::vector< FlowInfo > &flowInfos)
Creates a new flow set pool.
Definition flow_set_pool.hpp:99
auto crbegin() const noexcept
Iterator access.
Definition flow_set_pool.hpp:256
auto rend() noexcept
Iterator access.
Definition flow_set_pool.hpp:306
size_t PoolSize() const noexcept
Gets the size of the flow set pool.
Definition flow_set_pool.hpp:136
auto End() noexcept
Iterator access.
Definition flow_set_pool.hpp:206
auto end() noexcept
Iterator access.
Definition flow_set_pool.hpp:226
auto RBegin() noexcept
Iterator access.
Definition flow_set_pool.hpp:246
auto CBegin() const noexcept
Iterator access.
Definition flow_set_pool.hpp:156
auto rbegin() noexcept
Iterator access.
Definition flow_set_pool.hpp:266
FlowSetPool(const std::vector< FlowInfo > &flowInfos, ProtectedTag) noexcept
Flow set pool constructor.
Definition flow_set_pool.hpp:83
void Reserve(int flowSets) noexcept
Reserve flow sets in the flow set pool.
Definition flow_set_pool.hpp:146
auto Begin() noexcept
Iterator access.
Definition flow_set_pool.hpp:166
auto CEnd() const noexcept
Iterator access.
Definition flow_set_pool.hpp:196
auto cbegin() const noexcept
Iterator access.
Definition flow_set_pool.hpp:176
T make_shared(T... args)
Namespace for driver or device related operations.
Definition decl_composite.hpp:27
std::shared_ptr< FlowSetPool > FlowSetPoolPtr
Convenience shared pointer for FlowSetPool.
Definition driver.hpp:28
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
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:56
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
T transform(T... args)