CVB++ 15.0
decl_composite.hpp
1#pragma once
2
3#include <vector>
4#include <memory>
5#include <mutex>
6#include <unordered_map>
7
8#include "../_cexports/c_core.h"
9#include "../_cexports/c_img.h"
10
11#include "../_decl/decl_plane_enumerator.hpp"
12
13#include "../driver/gendc_descriptor.hpp"
14#include "../exception.hpp"
15#include "../global.hpp"
16#include "../image.hpp"
17#include "../buffer.hpp"
18#include "../pfnc_buffer.hpp"
19#include "../compressed_pfnc_buffer.hpp"
20#include "../handle_only.hpp"
21#include "../shims/stdvariant.hpp"
22
23namespace Cvb
24{
25 CVB_BEGIN_INLINE_NS
26
27 namespace Driver
28 {
29 class MultiPartImage;
30 }
31
32 template <>
33 inline HandleGuard<Composite>::HandleGuard(void *handle) noexcept
34 : HandleGuard<Composite>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
35 {
36 }
37
41
43
49 class Composite final
50 {
51 friend class Driver::MultiPartImage;
52
53 public:
54 using GuardType = HandleGuard<Composite>;
55
57
62 static std::unique_ptr<Composite> Create(CompositePurpose purpose = CompositePurpose::Custom)
63 {
64 return Internal::DoHandleCallObjectOut<class Composite>(
65 CVB_CALL_CAPI(CVCCreateComposite(static_cast<CExports::CVCCompositePurpose>(purpose))));
66 }
67
68 private:
69 template <class OTHER>
70 explicit Composite(const OTHER &object)
71 : Composite(MakeShared(object.Handle()))
72 {
73 }
74
75 explicit Composite(HandleGuard<Composite> &&guard)
76 : handle_(std::move(guard))
77 {
78 if (!CVB_CALL_CAPI(CVCIsComposite(Handle())))
79 throw std::runtime_error("composite handle must be an IComposite object");
80 }
81
82 static HandleGuard<Composite> MakeShared(void *handle) noexcept
83 {
84 CVB_CALL_CAPI(ShareObject(handle));
85 return HandleGuard<Composite>{handle};
86 }
87
88 public:
90
96 void *Handle() const noexcept
97 {
98 return handle_.Handle();
99 }
100
102
109 static std::unique_ptr<Composite> FromHandle(HandleGuard<Composite> &&guard)
110 {
111 if (!guard.Handle())
112 throw std::runtime_error("handle must not be null");
113
114 return std::unique_ptr<Composite>(new Composite(std::move(guard)));
115 }
116
117 template <class T>
118 static std::unique_ptr<T> FromHandle(HandleGuard<Composite> &&guard)
119 {
120 static_assert(std::is_base_of<Composite, T>::value, "CVB: Type must be derived from Composite");
121 return FromHandle(std::move(guard));
122 }
123
125
129 template <class T>
131
133
136 CompositePurpose Purpose() const noexcept
137 {
138 CExports::CVCCompositePurpose purpose = CExports::CVCCompositePurpose::CVCCP_Custom;
139 CVB_CALL_CAPI(CVCCompositeGetPurpose(Handle(), purpose));
140 return static_cast<CompositePurpose>(purpose);
141 }
142
144
149 {
150 if (!CExports::CVDCanGenDCDescriptorProvider(Handle()))
151 throw std::runtime_error("Composite does not represent a GenDC container");
152
153 return Internal::DoResCallObjectOut<Driver::GenDcDescriptor>([this](CExports::CVDGNDCDESCRIPTOR &handle) {
154 return CVB_CALL_CAPI(CVDGNDCPGetDescriptor(Handle(), handle));
155 });
156 }
157
159
162 bool ContainsItem(const CompositeVariant &item) const
163 {
164 const auto itemHandle = HandleOf(item);
165
166 for (auto i = ItemCount() - 1; i >= 0; --i)
167 {
168 if (itemHandle == HandleOf(ItemAt(i)))
169 return true;
170 }
171 return false;
172 }
173
175
179 int ItemCount() const noexcept
180 {
181 CExports::cvbdim_t numElements = 0;
182 CVB_CALL_CAPI(CVCCompositeGetCount(Handle(), numElements));
183 return static_cast<int>(numElements);
184 }
185
187
192 void InsertItemAt(int index, const CompositeVariant &item)
193 {
194 const auto handle = HandleOf(item);
195 Internal::DoResCall(
196 [this, index, handle]() { return CVB_CALL_CAPI(CVCCompositeInsertAt(Handle(), index, handle)); });
197
198 std::lock_guard<std::mutex> guard{cacheMtx_};
199 const auto pos = itemCache_.find(handle);
200 if (pos == itemCache_.end())
201 itemCache_.emplace_hint(pos, handle, item);
202 }
203
205
210 {
211 InsertItemAt(ItemCount(), item);
212 }
213
215
220 CompositeVariant ItemAt(int index) const
221 {
222 auto item = MakeWrapperFrom(index);
223 const auto handle = HandleOf(item);
224
225 std::lock_guard<std::mutex> guard{cacheMtx_};
226 const auto pos = itemCache_.find(handle);
227 if (pos != itemCache_.end())
228 return pos->second;
229
230 itemCache_.emplace_hint(pos, handle, item);
231 return item;
232 }
233
235
239 void RemoveItemAt(int index)
240 {
241 const auto item = MakeWrapperFrom(index);
242
243 Internal::DoResCall([&]() { return CVB_CALL_CAPI(CVCCompositeRemoveAt(Handle(), index)); });
244
245 if (!ContainsItem(item))
246 {
247 std::lock_guard<std::mutex> guard{cacheMtx_};
248 itemCache_.erase(HandleOf(item));
249 }
250 }
251
252 private:
253 static void *HandleOf(const CompositeVariant &item)
254 {
255 return Cvb::visit([](const auto &obj) { return obj->Handle(); }, item);
256 }
257
258 CompositeVariant MakeWrapperFrom(int index) const;
259
260 private:
261 HandleGuard<Composite> handle_;
262 mutable std::mutex cacheMtx_;
263 mutable std::unordered_map<void *, CompositeVariant> itemCache_;
264 };
265
266 CVB_END_INLINE_NS
267} // namespace Cvb
Component class is a container for CVB objects.
Definition decl_composite.hpp:50
static CompositePtr FromObject(std::shared_ptr< T > object)
Returns a composite object from the given composite compatible object.
bool ContainsItem(const CompositeVariant &item) const
Checks whether item is in this Composite.
Definition decl_composite.hpp:162
void PushBackItem(const CompositeVariant &item)
Appends a composite item.
Definition decl_composite.hpp:209
void RemoveItemAt(int index)
Remove a composite item specified by its index.
Definition decl_composite.hpp:239
static std::unique_ptr< Composite > Create(CompositePurpose purpose=CompositePurpose::Custom)
Creates a composite with the given optional parameter.
Definition decl_composite.hpp:62
CompositeVariant ItemAt(int index) const
Access to a composite item specified by its index.
Definition decl_composite.hpp:220
int ItemCount() const noexcept
Number of variants in the composite.
Definition decl_composite.hpp:179
static std::unique_ptr< Composite > FromHandle(HandleGuard< Composite > &&guard)
Creates a composite from a classic API handle.
Definition decl_composite.hpp:109
void * Handle() const noexcept
Classic API node handle.
Definition decl_composite.hpp:96
std::unique_ptr< Driver::GenDcDescriptor > GenDCFinalDescriptor() const
If this Composite represents a GenDC container, returns the GenDC final descriptor.
Definition decl_composite.hpp:148
CompositePurpose Purpose() const noexcept
Gets the purpose of this Composite.
Definition decl_composite.hpp:136
void InsertItemAt(int index, const CompositeVariant &item)
Insert a composite item specified at given index.
Definition decl_composite.hpp:192
MultiPart image class.
Definition multi_part_image.hpp:23
This class is a replacement for C++17 std::variant.
Definition variant.hpp:49
cvbbool_t ShareObject(OBJ Object)
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Namespace for driver or device related operations.
Definition decl_composite.hpp:28
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::shared_ptr< HandleOnly > HandleOnlyPtr
Convenience shared pointer for HandleOnly.
Definition global.hpp:106
std::shared_ptr< PFNCBuffer > PFNCBufferPtr
Convenience shared pointer for PFNCBuffer.
Definition global.hpp:70
std::shared_ptr< Buffer > BufferPtr
Convenience shared pointer for Buffer.
Definition global.hpp:62
auto visit(VISITOR &&visitor, VARIANT &&var) -> decltype(visitor(get< 0 >(var)))
Visits the given Shims::variant var. Cvb::Shims::visit can only visit one variant (not multiple like ...
Definition variant.hpp:622
std::shared_ptr< Image > ImagePtr
Convenience shared pointer for Image.
Definition global.hpp:86
std::shared_ptr< PlaneEnumerator > PlaneEnumeratorPtr
Convenience shared pointer for PlaneEnumerator.
Definition global.hpp:82
Cvb::variant< ImagePtr, PlanePtr, PlaneEnumeratorPtr, BufferPtr, PFNCBufferPtr, CompressedPFNCBufferPtr, HandleOnlyPtr > CompositeVariant
Variant that can contain the different types of composite items.
Definition decl_composite.hpp:39
std::shared_ptr< CompressedPFNCBuffer > CompressedPFNCBufferPtr
Convenience shared pointer for a compressed PFNCBuffer.
Definition global.hpp:74
std::shared_ptr< Composite > CompositePtr
Convenience shared pointer for Composite.
Definition global.hpp:102
std::shared_ptr< Plane > PlanePtr
Convenience shared pointer for Plane.
Definition global.hpp:78