CVB++ 15.0
detail_ring_buffer.hpp
1#pragma once
2
3#include "../../global.hpp"
4#include "../../device.hpp"
5
6#include "../_decl/decl_stream.hpp"
7
8#include "../_decl/decl_ring_buffer.hpp"
9#include "../_decl/decl_ring_buffer_image.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 namespace Driver
17 {
18
19 inline RingBufferImagePtr RingBuffer::At(int bufferIndex) const
20 {
21 return bufferImages_[static_cast<std::size_t>(bufferIndex)].AtomicGet([&] {
22 void *handle = nullptr;
23 auto result = CExports::RBGetBufferImage(parent_->Handle(), bufferIndex, handle);
24 if (result < 0)
25 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to get ring buffer image"));
26
27 return Image::FromHandle<RingBufferImage>(HandleGuard<Image>(handle), bufferIndex, shared_from_this());
28 });
29 }
30
31 inline int RingBuffer::Count() const noexcept
32 {
33
34 void *dumy = nullptr;
35 CExports::cvbval_t num = 0;
36 CExports::RBNumBuffer(parent_->Handle(), CExports::RINGBUFFER_NUMBUFFER_CMD_GET, num, dumy);
37 return static_cast<int>(num);
38 }
39
41 {
42 CExports::RINGBUFFER_LOCKMODE lockMode = CExports::RINGBUFFER_LOCKMODE_AUTO;
43 CExports::RBLockMode(parent_->Handle(), CExports::RINGBUFFER_LOCKMODE_CMD_GET, lockMode);
44
45 return static_cast<RingBufferLockMode>(lockMode);
46 }
47
48 inline void RingBuffer::SetLockMode(RingBufferLockMode ringBufferLockMode)
49 {
50
51 if (parent_->StreamCount() > 0 && parent_->Stream()->IsRunning())
52 throw std::logic_error("can't change lock mode while grab is active");
53
54 auto lockMode = static_cast<CExports::RINGBUFFER_LOCKMODE>(ringBufferLockMode);
55 auto result = CExports::RBLockMode(parent_->Handle(), CExports::RINGBUFFER_LOCKMODE_CMD_SET, lockMode);
56 if (result < 0)
57 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to set ring buffer lock mode"));
58 }
59
60 inline void RingBuffer::ChangeCount(int numBuffers, DeviceUpdateMode mode)
61 {
62 if (parent_->StreamCount() > 0 && parent_->Stream()->IsRunning())
63 throw std::logic_error("can't change number of buffers while grab is active");
64
65 std::vector<Internal::AsyncRef<RingBufferImage>> newBufferImages(static_cast<std::size_t>(numBuffers));
66
67 CExports::cvbval_t num = static_cast<CExports::cvbval_t>(numBuffers);
68 CExports::IMG img = nullptr;
69 auto result = CExports::RBNumBuffer(parent_->Handle(), CExports::RINGBUFFER_NUMBUFFER_CMD_SET, num, img);
70 if (result < 0)
71 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to set number of buffers"));
72
73 bufferImages_.swap(newBufferImages);
74
75 HandleGuard<Device> guard(img);
76 parent_->ChangeHandle(std::move(guard), mode);
77 }
78
79 } // namespace Driver
80
81 CVB_END_INLINE_NS
82
83} // namespace Cvb
void ChangeCount(int numBuffers, DeviceUpdateMode mode)
Changes the number of buffers in this ring buffer.
Definition detail_ring_buffer.hpp:60
RingBufferLockMode LockMode() const noexcept
Gets the current lock mode.
Definition detail_ring_buffer.hpp:40
int Count() const noexcept
Number of buffers in the ring buffer.
Definition detail_ring_buffer.hpp:31
void SetLockMode(RingBufferLockMode ringBufferLockMode)
Sets the current lock mode.
Definition detail_ring_buffer.hpp:48
RingBufferImagePtr At(int bufferIndex) const
Access to a single ring buffer image specified by its buffer index.
Definition detail_ring_buffer.hpp:19
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:160
T move(T... args)
Namespace for driver or device related operations.
Definition decl_composite.hpp:28
RingBufferLockMode
Lock mode options for the ring buffer.
Definition driver.hpp:400
std::shared_ptr< RingBufferImage > RingBufferImagePtr
Convenience shared pointer for RingBufferImage.
Definition driver.hpp:89
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
DeviceUpdateMode
Defines how to treat the optional device image, when the device itself is updated.
Definition global.hpp:252
T rethrow_exception(T... args)