CVB++ 15.1
Loading...
Searching...
No Matches
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::RINGBUFFER_NUMBUFFER_CMD_GET, num,
37 dumy);
38 return static_cast<int>(num);
39 }
40
42 {
43 CExports::RINGBUFFER_LOCKMODE lockMode = CExports::RINGBUFFER_LOCKMODE::RINGBUFFER_LOCKMODE_AUTO;
44 CExports::RBLockMode(parent_->Handle(), CExports::RINGBUFFER_LOCKMODE_CMD::RINGBUFFER_LOCKMODE_CMD_GET, lockMode);
45
46 return static_cast<RingBufferLockMode>(lockMode);
47 }
48
49 inline void RingBuffer::SetLockMode(RingBufferLockMode ringBufferLockMode)
50 {
51
52 if (parent_->StreamCount() > 0 && parent_->Stream()->IsRunning())
53 throw std::logic_error("can't change lock mode while grab is active");
54
55 auto lockMode = static_cast<CExports::RINGBUFFER_LOCKMODE>(ringBufferLockMode);
56 auto result =
57 CExports::RBLockMode(parent_->Handle(), CExports::RINGBUFFER_LOCKMODE_CMD::RINGBUFFER_LOCKMODE_CMD_SET, lockMode);
58 if (result < 0)
59 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to set ring buffer lock mode"));
60 }
61
62 inline void RingBuffer::ChangeCount(int numBuffers, DeviceUpdateMode mode)
63 {
64 if (parent_->StreamCount() > 0 && parent_->Stream()->IsRunning())
65 throw std::logic_error("can't change number of buffers while grab is active");
66
67 std::vector<Internal::AsyncRef<RingBufferImage>> newBufferImages(static_cast<std::size_t>(numBuffers));
68
69 CExports::cvbval_t num = static_cast<CExports::cvbval_t>(numBuffers);
70 CExports::IMG img = nullptr;
71 auto result = CExports::RBNumBuffer(parent_->Handle(),
72 CExports::RINGBUFFER_NUMBUFFER_CMD::RINGBUFFER_NUMBUFFER_CMD_SET, num, img);
73 if (result < 0)
74 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to set number of buffers"));
75
76 bufferImages_.swap(newBufferImages);
77
78 HandleGuard<Device> guard(img);
79 parent_->ChangeHandle(std::move(guard), mode);
80 }
81
82 } // namespace Driver
83
84 CVB_END_INLINE_NS
85
86} // namespace Cvb
void ChangeCount(int numBuffers, DeviceUpdateMode mode)
Changes the number of buffers in this ring buffer.
Definition detail_ring_buffer.hpp:62
RingBufferLockMode LockMode() const noexcept
Gets the current lock mode.
Definition detail_ring_buffer.hpp:41
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:49
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:27
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 version.hpp:11
DeviceUpdateMode
Defines how to treat the optional device image, when the device itself is updated.
Definition global.hpp:252
T rethrow_exception(T... args)