14#include <peak/dll_interface/peak_dll_interface_util.hpp>
15#include <peak/generic/peak_t_callback_manager.hpp>
19#include <unordered_map>
36struct InterfaceFoundCallbackContainer;
48 ,
public std::enable_shared_from_this<System>
202 struct InterfaceFoundCallbackContainer
204 std::weak_ptr<System> System_;
207 static void PEAK_CALL_CONV InterfaceFoundCallbackCWrapper(
209 static void PEAK_CALL_CONV InterfaceLostCallbackCWrapper(
210 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context);
216 void InitializeUpdateMechanismIfNecessary();
218 friend ClassCreator<System>;
222 std::weak_ptr<ProducerLibrary> m_parentLibrary;
224 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>
225 m_interfaceFoundCallbackManager;
226 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>
227 m_interfaceLostCallbackManager;
229 std::vector<std::shared_ptr<InterfaceDescriptor>> m_interfaces;
230 std::unordered_map<std::string, std::shared_ptr<InterfaceDescriptor>> m_interfacesByKey;
231 std::unordered_map<std::string, std::string> m_interfacesKeyById;
232 mutable std::mutex m_interfacesMutex;
233 std::once_flag m_updateMechanismInitializedFlag;
248 : m_backendHandle(systemHandle)
249 , m_parentLibrary(parentLibrary)
250 , m_interfaceFoundCallbackManager()
251 , m_interfaceLostCallbackManager()
252 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
256 m_interfaceFoundCallbackManager =
257 std::make_unique<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>(
258 [&](
void* callbackContext) {
259 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE>(
262 InterfaceFoundCallbackCWrapper, callbackContext, interfaceFoundCallbackHandle);
266 CallAndCheckCInterfaceFunction([&] {
268 m_backendHandle, callbackHandle);
272 m_interfaceLostCallbackManager =
273 std::make_unique<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>(
274 [&](
void* callbackContext) {
275 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_LOST_CALLBACK_HANDLE>(
278 InterfaceLostCallbackCWrapper, callbackContext, interfaceLostCallbackHandle);
282 CallAndCheckCInterfaceFunction([&] {
284 m_backendHandle, callbackHandle);
293 m_interfaceFoundCallbackManager->UnregisterAllCallbacks();
294 m_interfaceLostCallbackManager->UnregisterAllCallbacks();
309 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
316 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
323 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
330 return QueryStringFromCInterfaceFunction([&](
char* vendorName,
size_t* vendorNameSize) {
337 return QueryStringFromCInterfaceFunction([&](
char* modelName,
size_t* modelNameSize) {
344 return QueryStringFromCInterfaceFunction([&](
char* version,
size_t* versionSize) {
351 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
358 return QueryStringFromCInterfaceFunction([&](
char* ctiFileName,
size_t* ctiFileNameSize) {
365 return QueryStringFromCInterfaceFunction([&](
char* ctiFullPath,
size_t* ctiFullPathSize) {
372 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMajor) {
379 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMinor) {
387 QueryNumericFromCInterfaceFunction<PEAK_CHARACTER_ENCODING>(
395 InitializeUpdateMechanismIfNecessary();
397 CallAndCheckCInterfaceFunction(
403 std::lock_guard<std::mutex> lock(m_interfacesMutex);
409 return LockOrThrow(m_parentLibrary);
416 m_interfaceFoundCallbackManager->RegisterCallback(InterfaceFoundCallbackContainer{ shared_from_this(),
417 [callback](
const std::shared_ptr<InterfaceDescriptor>& foundInterface) { callback(foundInterface); } }));
422 m_interfaceFoundCallbackManager->UnregisterCallback(
434 m_interfaceLostCallbackManager->UnregisterCallback(
438inline void PEAK_CALL_CONV System::InterfaceFoundCallbackCWrapper(
441 auto callbackContainer =
static_cast<InterfaceFoundCallbackContainer*
>(context);
443 auto interfaceDescriptor = callbackContainer->System_.lock()->GetOrAddFoundInterface(foundInterface);
445 callbackContainer->Callback(interfaceDescriptor);
448inline void PEAK_CALL_CONV System::InterfaceLostCallbackCWrapper(
449 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context)
453 callback->operator()(std::string(lostInterfaceId, lostInterfaceIdSize - 1));
456inline std::shared_ptr<InterfaceDescriptor> System::GetOrAddFoundInterface(
459 std::lock_guard<std::mutex> lock(m_interfacesMutex);
461 const auto interfaceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
464 auto keyInterfacePairIterator = m_interfacesByKey.find(interfaceKey);
465 if (keyInterfacePairIterator == m_interfacesByKey.end())
467 auto interfaceDescriptor = std::static_pointer_cast<InterfaceDescriptor>(
468 std::make_shared<ClassCreator<InterfaceDescriptor>>(
469 foundInterface, std::weak_ptr<System>{ shared_from_this() }));
470 m_interfaces.emplace_back(interfaceDescriptor);
471 m_interfacesByKey.emplace(interfaceKey, interfaceDescriptor);
472 m_interfacesKeyById.emplace(interfaceDescriptor->ID(), interfaceKey);
474 return interfaceDescriptor;
477 return keyInterfacePairIterator->second;
482 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
492 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
495 m_backendHandle, _eventSupportingModuleHandle);
498 return eventSupportingModuleHandle;
501inline void System::InitializeUpdateMechanismIfNecessary()
503 std::call_once(m_updateMechanismInitializedFlag, [&] {
508 std::lock_guard<std::mutex> lock(m_interfacesMutex);
509 const auto lostInterfaceKey = m_interfacesKeyById.at(lostInterfaceId);
510 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
511 [lostInterfaceKey](
const std::shared_ptr<InterfaceDescriptor>& interface_) {
512 return interface_->Key() == lostInterfaceKey;
514 std::end(m_interfaces));
515 m_interfacesByKey.erase(lostInterfaceKey);
516 m_interfacesKeyById.erase(lostInterfaceId);
EventSupportingModule()=default
virtual PEAK_EVENT_SUPPORTING_MODULE_HANDLE EventSupportingModuleHandle() const =0
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:24
virtual PEAK_MODULE_HANDLE ModuleHandle() const =0
Represents a GenTL producer library (CTI).
Definition peak_producer_library.hpp:37
PEAK_COMMON_NO_DISCARD uint32_t GenTLVersionMajor() const
Returns the GenTL major version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:370
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters a interface lost callback.
Definition peak_system.hpp:432
PEAK_COMMON_NO_DISCARD std::string TLType() const
Returns the TL (transport layer) type.
Definition peak_system.hpp:349
void UpdateInterfaces(Timeout timeout_ms)
Searches for interfaces.
Definition peak_system.hpp:393
std::function< void(const std::shared_ptr< InterfaceDescriptor > &foundInterface)> InterfaceFoundCallback
Definition peak_system.hpp:52
PEAK_COMMON_NO_DISCARD std::string CTIFileName() const
Returns the file name of the GenTL producer library this system belongs to.
Definition peak_system.hpp:356
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_system.hpp:420
PEAK_COMMON_NO_DISCARD std::string CTIFullPath() const
Returns the full path of the GenTL producer library this system belongs to.
Definition peak_system.hpp:363
System(const System &other)=delete
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_system.hpp:58
PEAK_COMMON_NO_DISCARD std::string DisplayName() const
Returns the display name.
Definition peak_system.hpp:321
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_system.hpp:412
PEAK_COMMON_NO_DISCARD uint32_t GenTLVersionMinor() const
Returns the GenTL minor version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:377
PEAK_COMMON_NO_DISCARD std::string Version() const
Returns the version.
Definition peak_system.hpp:342
System(System &&other)=delete
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_system.hpp:426
~System() override
Definition peak_system.hpp:289
PEAK_COMMON_NO_DISCARD std::string ID() const
Returns the ID.
Definition peak_system.hpp:314
PEAK_COMMON_NO_DISCARD peak::core::CharacterEncoding CharacterEncoding() const
Returns the character encoding of the GenTL producer library this system belongs to.
Definition peak_system.hpp:384
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:54
PEAK_COMMON_NO_DISCARD std::shared_ptr< ProducerLibrary > ParentLibrary() const
Returns the parent library.
Definition peak_system.hpp:407
System & operator=(const System &other)=delete
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< InterfaceDescriptor > > Interfaces() const
Returns the interface list.
Definition peak_system.hpp:401
PEAK_COMMON_NO_DISCARD std::string ModelName() const
Returns the model name.
Definition peak_system.hpp:335
std::function< void(const std::string &lostInterfaceId)> InterfaceLostCallback
Definition peak_system.hpp:56
System & operator=(System &&other)=delete
PEAK_COMMON_NO_DISCARD std::string VendorName() const
Returns the vendor name.
Definition peak_system.hpp:328
PEAK_COMMON_NO_DISCARD std::string Key() const
Returns the unique key.
Definition peak_system.hpp:302
PEAK_COMMON_NO_DISCARD RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition peak_system.hpp:307
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:28
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:32
CharacterEncoding
Possible character encodings.
Definition peak_common_system_enums.hpp:23
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_buffer.hpp:29
PEAK_C_API PEAK_System_GetGenTLVersionMinor(PEAK_SYSTEM_HANDLE systemHandle, uint32_t *gentlVersionMinor)
Asks the given system for the minor component of its GenTL version.
PEAK_INTERFACE_LOST_CALLBACK * PEAK_INTERFACE_LOST_CALLBACK_HANDLE
Definition peak_backend.h:513
PEAK_C_API PEAK_System_GetGenTLVersionMajor(PEAK_SYSTEM_HANDLE systemHandle, uint32_t *gentlVersionMajor)
Asks the given system for the major component of its GenTL version.
PEAK_C_API PEAK_System_ToEventSupportingModule(PEAK_SYSTEM_HANDLE systemHandle, PEAK_EVENT_SUPPORTING_MODULE_HANDLE *eventSupportingModuleHandle)
Casts the given system to an event-supporting module.
PEAK_C_API PEAK_System_GetInfo(PEAK_SYSTEM_HANDLE systemHandle, int32_t infoCommand, int32_t *infoDataType, uint8_t *info, size_t *infoSize)
Asks the given system for an information based on a specific command.
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition peak_backend.h:419
PEAK_C_API PEAK_System_GetKey(PEAK_SYSTEM_HANDLE systemHandle, char *key, size_t *keySize)
Asks the given system for its key.
PEAK_C_API PEAK_System_Destruct(PEAK_SYSTEM_HANDLE systemHandle)
Destroys the given system.
struct PEAK_INTERFACE_DESCRIPTOR * PEAK_INTERFACE_DESCRIPTOR_HANDLE
Definition peak_backend.h:375
PEAK_C_API PEAK_System_GetDisplayName(PEAK_SYSTEM_HANDLE systemHandle, char *displayName, size_t *displayNameSize)
Asks the given system for its display name.
PEAK_C_API PEAK_InterfaceDescriptor_GetKey(PEAK_INTERFACE_DESCRIPTOR_HANDLE interfaceDescriptorHandle, char *key, size_t *keySize)
Asks the given interface descriptor for its key.
PEAK_C_API PEAK_System_UpdateInterfaces(PEAK_SYSTEM_HANDLE systemHandle, uint64_t timeout_ms)
Tells the given system to update its interfaces with the given timeout.
PEAK_C_API PEAK_System_GetID(PEAK_SYSTEM_HANDLE systemHandle, char *id, size_t *idSize)
Asks the given system for its ID.
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition peak_backend.h:475
PEAK_C_API PEAK_System_UnregisterInterfaceLostCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_LOST_CALLBACK_HANDLE callbackHandle)
Unregisters the interface-lost callback with the given handle from the given system.
struct PEAK_SYSTEM * PEAK_SYSTEM_HANDLE
Definition peak_backend.h:371
PEAK_C_API PEAK_System_GetCTIFileName(PEAK_SYSTEM_HANDLE systemHandle, char *ctiFileName, size_t *ctiFileNameSize)
Asks the given system for its parent CTI's file name.
PEAK_C_API PEAK_System_GetModelName(PEAK_SYSTEM_HANDLE systemHandle, char *modelName, size_t *modelNameSize)
Asks the given system for its model name.
PEAK_C_API PEAK_System_GetCharacterEncoding(PEAK_SYSTEM_HANDLE systemHandle, PEAK_CHARACTER_ENCODING *characterEncoding)
Asks the given system for its character encoding.
PEAK_C_API PEAK_System_GetVendorName(PEAK_SYSTEM_HANDLE systemHandle, char *vendorName, size_t *vendorNameSize)
Asks the given system for its vendor name.
PEAK_C_API PEAK_System_GetCTIFullPath(PEAK_SYSTEM_HANDLE systemHandle, char *ctiFullPath, size_t *ctiFullPathSize)
Asks the given system for its parent CTI's full path.
PEAK_C_API PEAK_System_GetTLType(PEAK_SYSTEM_HANDLE systemHandle, char *tlType, size_t *tlTypeSize)
Asks the given system for its TL type.
PEAK_C_API PEAK_System_GetVersion(PEAK_SYSTEM_HANDLE systemHandle, char *version, size_t *versionSize)
Asks the given system for its version.
PEAK_C_API PEAK_System_RegisterInterfaceLostCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_LOST_CALLBACK callback, void *callbackContext, PEAK_INTERFACE_LOST_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a lost interface at the given system.
int32_t PEAK_CHARACTER_ENCODING
Definition peak_backend.h:105
PEAK_C_API PEAK_System_RegisterInterfaceFoundCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_FOUND_CALLBACK callback, void *callbackContext, PEAK_INTERFACE_FOUND_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a found interface at the given system.
PEAK_INTERFACE_FOUND_CALLBACK * PEAK_INTERFACE_FOUND_CALLBACK_HANDLE
Definition peak_backend.h:511
PEAK_C_API PEAK_System_UnregisterInterfaceFoundCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_FOUND_CALLBACK_HANDLE callbackHandle)
Unregisters the interface-found callback with the given handle from the given system.
PEAK_C_API PEAK_System_ToModule(PEAK_SYSTEM_HANDLE systemHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given system to a module.
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:13