CVB++ 15.0
decl_device_factory.hpp
1#pragma once
2
3#include <exception>
4#include <memory>
5#include <mutex>
6#include <stdexcept>
7#include <chrono>
8#include <vector>
9
10#include "../global.hpp"
11#include "../exception.hpp"
12#include "../string.hpp"
13
14#include "../utilities/system_info.hpp"
15
16#include "../driver/discovery_information.hpp"
17
18namespace Cvb
19{
20
21 CVB_BEGIN_INLINE_NS
22
24
37
38 class DeviceFactory final
39 {
40 public:
42
55 template <class T>
56 static std::shared_ptr<T> Open(const String &provider,
57 AcquisitionStack acquisitionStack = AcquisitionStack::PreferVin)
58 {
59 static_assert(std::is_base_of<Device, T>::value, "CVB: Device must be a base of T");
60 auto device = std::dynamic_pointer_cast<T>(Open(provider, acquisitionStack));
61 if (!device)
62 throw std::runtime_error("open failed to cast to device type");
63 return device;
64 }
65
67
88 static DevicePtr Open(const String &provider, AcquisitionStack acquisitionStack = AcquisitionStack::PreferVin);
89
91
104 template <class T>
105 static std::shared_ptr<T> Open(const String &provider, int port, int board)
106 {
107 static_assert(std::is_base_of<Device, T>::value, "CVB: Device must be a base of T");
108 auto device = std::dynamic_pointer_cast<T>(Open(provider, port, board));
109 if (!device)
110 throw std::runtime_error("open failed to cast to device type");
111 return device;
112 }
113
115
128 static DevicePtr Open(const String &provider, int port, int board)
129 {
130 if (Internal::IsAccessToken(provider))
131 throw std::logic_error("access tokens do not support port or board switch");
132 auto device = Open(provider, AcquisitionStack::PreferVin);
133 ChangeBoard(device, board);
134 ChangePort(device, port);
135 return device;
136 }
137
139
145 template <class T>
146 static std::shared_ptr<T> OpenBoard(const String &provider, int board)
147 {
148 static_assert(std::is_base_of<Device, T>::value, "CVB: Device must be a base of T");
149 auto device = std::dynamic_pointer_cast<T>(OpenBoard(provider, board));
150 if (!device)
151 throw std::runtime_error("open board failed to cast to device type");
152 return device;
153 }
154
156
162 static DevicePtr OpenBoard(const String &provider, int board)
163 {
164 if (Internal::IsAccessToken(provider))
165 throw std::logic_error("access tokens do not support port or board switch");
166 auto device = Open(provider, AcquisitionStack::PreferVin);
167 ChangeBoard(device, board);
168 return device;
169 }
170
172
178 template <class T>
179 static std::shared_ptr<T> OpenPort(const String &provider, int port)
180 {
181 static_assert(std::is_base_of<Device, T>::value, "CVB: Device must be a base of T");
182 auto device = std::dynamic_pointer_cast<T>(OpenPort(provider, port));
183 if (!device)
184 throw std::runtime_error("open port failed to cast to device type");
185 return device;
186 }
187
189
195 static DevicePtr OpenPort(const String &provider, int port)
196 {
197 if (Internal::IsAccessToken(provider))
198 throw std::logic_error("access tokens do not support port or board switch");
199 auto device = Open(provider, AcquisitionStack::PreferVin);
200 ChangePort(device, port);
201 return device;
202 }
203
205
218 {
219#if defined _WIN32
220 return Discover(DiscoverFlags::IgnoreVins | DiscoverFlags::IgnoreGevSD);
221#else
222 return Discover(DiscoverFlags::IgnoreVins);
223#endif
224 }
225
227
239
241
250 template <class Rep, class Period>
253 {
254 return Discover(String(), flags, timeSpan);
255 }
256
258
268 template <class Rep, class Period>
270 const std::chrono::duration<Rep, Period> &timeSpan);
271
273
283 {
284 return Discover(accessToken, flags, std::chrono::milliseconds(300));
285 }
286
288
297 {
298 return Discover(accessToken, DiscoverFlags::FindAll);
299 }
300
303
312 {
313 return Discover(info.AccessToken());
314 }
315
317
330
332
342 template <class Rep, class Period>
345 {
346 return Discover(info.AccessToken(), flags, timeSpan);
347 }
348
349 static CExports::cvbguid_t AviMpgGuid()
350 {
351 static CExports::cvbguid_t guid = {0x7DBE4A03, 0xAD3D, 0x4533, 0x85, 0x1F, 0x61, 0xB4, 0xAE, 0x62, 0xC2, 0x0E};
352 return guid;
353 }
354
355 static CExports::cvbguid_t EmuGuid()
356 {
357 static CExports::cvbguid_t guid = {0x10cb8e8f, 0x00b6, 0x4bb0, 0xbb, 0x82, 0xcd, 0x77, 0x2c, 0xba, 0xb7, 0x57};
358 return guid;
359 }
360
361 static std::mutex &LockAnchor()
362 {
363 static std::mutex mutex;
364 return mutex;
365 }
366
367 private:
368 DeviceFactory() noexcept = delete;
369
370 static void ChangeBoard(DevicePtr &device, int board);
371
372 static void ChangeBoard1(DevicePtr &device, int board);
373
374 static void ChangeBoard2(DevicePtr &device, int board);
375
376 static void ChangePort(DevicePtr &device, int port);
377
378 static void ChangePort1(DevicePtr &device, int port);
379
380 static void ChangePort2(DevicePtr &device, int port);
381
382 static bool AllowFallback(AcquisitionStack acquisitionStack) noexcept
383 {
384 return acquisitionStack == Cvb::AcquisitionStack::PreferVin
385 || acquisitionStack == Cvb::AcquisitionStack::PreferGenTL;
386 }
387
388 static HandleGuard<Device> LoadAsGenTL(const String &provider, AcquisitionStack acquisitionStack);
389
390 static HandleGuard<Device> LoadAsVin(const String &provider, AcquisitionStack acquisitionStack);
391
392 static HandleGuard<Device> LoadAsNonStreaming(const String &provider);
393
394 static DevicePtr CreateDeviceByDriverGUID(const String &provider, HandleGuard<Device> &&guard) noexcept;
395 };
396
397 CVB_END_INLINE_NS
398
399} // namespace Cvb
static DevicePtr OpenBoard(const String &provider, int board)
Open a device with the given provider and board.
Definition decl_device_factory.hpp:162
static std::vector< DiscoveryInformation > Discover(const String &accessToken)
Discovers available nodes starting on level of the given access token with a default time span of 300...
Definition decl_device_factory.hpp:296
static std::vector< DiscoveryInformation > Discover(DiscoverFlags flags, const std::chrono::duration< Rep, Period > &timeSpan)
Discovers available devices/nodes depending on the given flags.
Definition decl_device_factory.hpp:251
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition decl_device_factory.hpp:217
static std::vector< DiscoveryInformation > Discover(const String &accessToken, DiscoverFlags flags, const std::chrono::duration< Rep, Period > &timeSpan)
Discovers available nodes starting on level of the given access token.
static std::shared_ptr< T > OpenPort(const String &provider, int port)
Open a device with the given provider and port.
Definition decl_device_factory.hpp:179
static std::vector< DiscoveryInformation > Discover(const DiscoveryInformation &info, DiscoverFlags flags, const std::chrono::duration< Rep, Period > &timeSpan)
Discovers available nodes starting on level of the given info.
Definition decl_device_factory.hpp:343
static DevicePtr OpenPort(const String &provider, int port)
Open a device with the given provider and port.
Definition decl_device_factory.hpp:195
static std::vector< DiscoveryInformation > Discover(DiscoverFlags flags)
Discovers available devices/nodes depending on the given flags, with a default time span of 300ms.
Definition decl_device_factory.hpp:235
static DevicePtr Open(const String &provider, int port, int board)
Opens a device with the given provider.
Definition decl_device_factory.hpp:128
static std::vector< DiscoveryInformation > Discover(const DiscoveryInformation &info, DiscoverFlags flags)
Discovers available nodes starting on level of the given info with a default time span of 300ms.
Definition decl_device_factory.hpp:326
static std::vector< DiscoveryInformation > Discover(const DiscoveryInformation &info)
Discovers available nodes starting on level of the given discovery info with a default time span of 3...
Definition decl_device_factory.hpp:311
static std::shared_ptr< T > Open(const String &provider, int port, int board)
Opens a device with the given provider.
Definition decl_device_factory.hpp:105
static std::shared_ptr< T > OpenBoard(const String &provider, int board)
Open a device with the given provider and board.
Definition decl_device_factory.hpp:146
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition decl_device_factory.hpp:56
static std::vector< DiscoveryInformation > Discover(const String &accessToken, DiscoverFlags flags)
Discovers available nodes starting on level of the given access token with a default time span of 300...
Definition decl_device_factory.hpp:282
Stores information on a discovered device/node.
Definition discovery_information.hpp:26
String AccessToken() const
The access token for opening the device/node.
Definition discovery_information.hpp:105
AcquisitionStack
Defines the acquisition stack when opening the device.
Definition driver.hpp:282
DiscoverFlags
Flags controlling the discovery process.
Definition driver.hpp:302
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98
T dynamic_pointer_cast(T... args)