CVB++ 15.0
decl_stream_handler_base.hpp
1#pragma once
2
3#include "../../point_cloud.hpp"
4#include "../../driver/image_stream.hpp"
5#include "../async.hpp"
6
7namespace Cvb
8{
9
10 CVB_BEGIN_INLINE_NS
11
12 namespace Async
13 {
14 namespace Internal
15 {
16 template <class STREAMTYPE>
17 struct IsValidStreamImpl
18 {
19 static auto check(STREAMTYPE *) -> decltype(std::is_base_of<CompositeStreamBase, STREAMTYPE>::value
20 || std::is_same<Stream, STREAMTYPE>::value,
21 std::true_type());
22 static auto check(...) -> std::false_type;
23 };
24
25 template <class STREAMTYPE>
26 struct IsValidStream : public decltype(IsValidStreamImpl<STREAMTYPE>::check(nullptr)){};
27
28 // \brief Abstracts information about the main deliverable of the
29 // specified stream.
30 // \tparam STREAMTYPE Type of stream.
31 template <class STREAMTYPE, typename std::enable_if_t<IsValidStream<STREAMTYPE>::value> * = nullptr>
32 struct DeliverableTraits;
33
34 template <>
35 struct DeliverableTraits<ImageStream>
36 {
37 using type = MultiPartImage;
38 };
39
40 template <>
41 struct DeliverableTraits<CompositeStream>
42 {
43 using type = Composite;
44 };
45
46 template <>
47 struct DeliverableTraits<PointCloudStream>
48 {
49 using type = PointCloud;
50 };
51
52 template <>
53 struct DeliverableTraits<Stream>
54 {
55 using type = StreamImage;
56 };
57
58 // \brief Abstracts information about the corresponding device of the
59 // specified stream.
60 // \tparam STREAMTYPE Type of stream.
61 template <class STREAMTYPE, typename std::enable_if_t<IsValidStream<STREAMTYPE>::value> * = nullptr>
62 struct DeviceTraits
63 {
64 // Represents the corresponding device type of the specified stream.
65 using type = Driver::GenICamDevice;
66 };
67
68 template <>
69 struct DeviceTraits<Stream>
70 {
71 using type = Device;
72 };
73
74 // \brief multi-stream handler based on the specified stream.
75 // \tparam STREAMTYPE Type of stream.
76 template <class STREAMTYPE, typename std::enable_if_t<IsValidStream<STREAMTYPE>::value> * = nullptr>
77 struct TpedStreamHandlerTraits
78 {
79 // Represents the corresponding multi-stream handler type of the specified stream.
80 using type = void;
81 };
82
83 } // namespace Internal
84
86
89 template <class STREAMTYPE>
90 class StreamHandlerBase
91 {
92
93 public:
95 using StreamType = STREAMTYPE;
96
99
102
104 using DeliverableType = typename Internal::DeliverableTraits<STREAMTYPE>::type;
105
108
111
114
117
119
129 {
130 return std::unique_ptr<StreamHandlerBase>(new StreamHandlerBase(streamVector));
131 }
132
134
147 {
148 return Create(StreamVectorType{stream});
149 }
150
151 StreamHandlerBase(const StreamHandlerBase &other) = delete;
152 StreamHandlerBase &operator=(const StreamHandlerBase &other) = delete;
153 StreamHandlerBase(StreamHandlerBase &&other) = delete;
154 StreamHandlerBase &operator=(StreamHandlerBase &&other) = delete;
155 virtual ~StreamHandlerBase();
156
158
166 void Run();
167
169
182 bool TryFinish() noexcept;
183
185
198 void Finish();
199
201
205 bool IsActive() const noexcept
206 {
207 return isActive_;
208 }
209
210 void operator()();
211
212 protected:
214
226 virtual void HandleAsyncStream(const StreamVectorType &streamVector);
227
229
241 const std::vector<WaitResult<typename Internal::DeliverableTraits<STREAMTYPE>::type>> &waitResultVector);
242
244
256 const std::vector<WaitResultTuple<typename Internal::DeliverableTraits<STREAMTYPE>::type>> &waitResultVector);
257
259
268 virtual void Setup(const StreamVectorType streamVector);
269
271
279 virtual void TearDown(const StreamVectorType streamVector) noexcept;
280
282
299 virtual void HandleError(const std::exception &error) noexcept;
300
303
312 virtual void Begin() noexcept {}
313
316
323 virtual void End() noexcept {}
324
325 protected:
327
334 explicit StreamHandlerBase(const StreamVectorType &streamVector);
335
337
344 explicit StreamHandlerBase(const StreamPtrType &stream);
345
347
352 {
353 return streamVector_;
354 }
355
356 private:
357 void BeginFinish();
358 void EndFinish();
359
360 StreamVectorType streamVector_;
361
363 std::mutex mutex_;
364
365 std::atomic<bool> isActive_;
366
367 std::exception_ptr lastError_;
368 };
369
371
378 template <class STREAMTYPE>
380 {
381 public:
384
386
392 explicit StreamHandlerGuard(StreamHandlerBasePtr streamHandler, AutoRun autoRun = AutoRun::No) noexcept
393 : streamHandler_(streamHandler)
394 {
395 assert(("streamHandler must not be NULL", streamHandler));
396 if (!streamHandler->IsActive() && autoRun == AutoRun::Yes)
397 streamHandler->Run();
398 }
399
400 StreamHandlerGuard(const StreamHandlerGuard &other) = delete;
401 StreamHandlerGuard &operator=(const StreamHandlerGuard &other) = delete;
402 StreamHandlerGuard(StreamHandlerGuard &&other) = delete;
403 StreamHandlerGuard &operator=(StreamHandlerGuard &&other) = delete;
404
406
412 {
413 streamHandler_->TryFinish();
414 }
415
416 private:
417 StreamHandlerBasePtr streamHandler_;
418 };
419
420 } // namespace Async
421
422 using Async::StreamHandlerBase;
424
425 using Async::StreamHandlerGuard;
426
427 CVB_END_INLINE_NS
428
429} // namespace Cvb
Stream handler for synchronous streams.
Definition decl_stream_handler_base.hpp:91
std::shared_ptr< StreamType > StreamPtrType
Shorthand notation of the shared pointer of the specified stream.
Definition decl_stream_handler_base.hpp:98
bool TryFinish() noexcept
Stop the handler.
Definition detail_stream_handler_base.hpp:61
virtual void TearDown(const StreamVectorType streamVector) noexcept
Tear down the streams after acquisition.
Definition detail_stream_handler_base.hpp:172
std::vector< DeliverablePtrType > DeliverablePtrVectorType
Shorthand notation of the container of the stream deliverable shared pointers.
Definition decl_stream_handler_base.hpp:110
std::vector< StreamPtrType > StreamVector() const noexcept
Get the streams associated with this handler.
Definition decl_stream_handler_base.hpp:351
std::shared_ptr< typename Internal::DeliverableTraits< STREAMTYPE >::type > DeliverablePtrType
Shorthand notation of the shared pointer of the stream deliverable.
Definition decl_stream_handler_base.hpp:107
virtual void Begin() noexcept
Performs custom operations at the beginning of the acquisition thread.
Definition decl_stream_handler_base.hpp:312
virtual void HandleAsyncWaitResult(const std::vector< WaitResult< typename Internal::DeliverableTraits< STREAMTYPE >::type > > &waitResultVector)
Asynchronously called for all acquired images.
Definition detail_stream_handler_base.hpp:151
virtual void HandleAsyncWaitResult(const std::vector< WaitResultTuple< typename Internal::DeliverableTraits< STREAMTYPE >::type > > &waitResultVector)
Asynchronously called for all acquired images.
Definition detail_stream_handler_base.hpp:158
virtual void Setup(const StreamVectorType streamVector)
Setup the streams for acquisition.
Definition detail_stream_handler_base.hpp:165
std::vector< StreamPtrType > StreamVectorType
Shorthand notation of the container of the stream shared pointer.
Definition decl_stream_handler_base.hpp:101
StreamHandlerBase(const StreamPtrType &stream)
Creates a stream handler object.
Definition detail_stream_handler_base.hpp:185
static std::unique_ptr< StreamHandlerBase > Create(const StreamVectorType &streamVector)
Create a stream handler object.
Definition decl_stream_handler_base.hpp:128
void Run()
Start the handler.
Definition detail_stream_handler_base.hpp:48
virtual void End() noexcept
Performs custom operations just before the end of the acquisition thread.
Definition decl_stream_handler_base.hpp:323
std::vector< EventHandlerType > EventHandlerVectorType
Shorthand notation of the container of the event handler.
Definition decl_stream_handler_base.hpp:116
std::function< void(DeliverablePtrType, Cvb::WaitStatus)> EventHandlerType
Shorthand notation of the event handler, a callable, type.
Definition decl_stream_handler_base.hpp:113
StreamHandlerBase(const StreamVectorType &streamVector)
Creates a stream handler object.
Definition detail_stream_handler_base.hpp:194
bool IsActive() const noexcept
Definition decl_stream_handler_base.hpp:205
typename Internal::DeliverableTraits< STREAMTYPE >::type DeliverableType
Shorthand notation of the stream deliverable type of the specified stream.
Definition decl_stream_handler_base.hpp:104
virtual void HandleError(const std::exception &error) noexcept
Handles standard exceptions in the acquisition thread.
Definition detail_stream_handler_base.hpp:179
static std::unique_ptr< StreamHandlerBase > Create(const StreamPtrType &stream)
Create a stream handler object.
Definition decl_stream_handler_base.hpp:146
STREAMTYPE StreamType
Shorthand notation of the stream type.
Definition decl_stream_handler_base.hpp:95
virtual void HandleAsyncStream(const StreamVectorType &streamVector)
Asynchronously called for all registered streams.
Definition detail_stream_handler_base.hpp:131
Handler guard object to safely run and finish stream handlers.
Definition decl_stream_handler_base.hpp:380
StreamHandlerGuard(StreamHandlerBasePtr streamHandler, AutoRun autoRun=AutoRun::No) noexcept
Creates a stream handler guard object.
Definition decl_stream_handler_base.hpp:392
std::shared_ptr< StreamHandlerBase< STREAMTYPE > > StreamHandlerBasePtr
Shared pointer of the stream handler.
Definition decl_stream_handler_base.hpp:383
~StreamHandlerGuard()
Finishes the shared stream handler .
Definition decl_stream_handler_base.hpp:411
std::shared_ptr< StreamType > StreamPtrType
Definition decl_stream_handler_base.hpp:98
std::vector< StreamPtrType > StreamVectorType
Definition decl_stream_handler_base.hpp:101
Convenience classes for asynchronous image acquisition.
Definition decl_multi_stream_handler.hpp:11
AutoRun
Defines the auto run mode for the handler guard.
Definition async.hpp:63
@ Yes
Definition async.hpp:67
@ No
Definition async.hpp:71
std::shared_ptr< StreamHandlerBase< STREAMTYPE > > StreamHandlerBasePtr
Convenience shared pointer for GenericMultiStreamHandler.
Definition async.hpp:33
@ Stream
Definition driver.hpp:430
std::tuple< std::shared_ptr< T >, WaitStatus, NodeMapEnumerator > WaitResultTuple
Tuple holding multiple return values after waiting for a specific payload data.
Definition driver.hpp:577
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
WaitStatus
Status after waiting for an image to be returned.
Definition global.hpp:396
A combined result returned after waiting for a image.
Definition driver.hpp:391