17#include <peak/dll_interface/peak_dll_interface_util.hpp>
18#include <peak/generic/peak_t_callback_manager.hpp>
22#include <unordered_map>
39struct InterfaceFoundCallbackContainer;
50 ,
public std::enable_shared_from_this<System>
70 std::string
Key()
const;
83 std::string
ID()
const;
98 std::string
TLType()
const;
140 std::vector<std::shared_ptr<InterfaceDescriptor>>
Interfaces()
const;
204 struct InterfaceFoundCallbackContainer
206 std::weak_ptr<System> System_;
212 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context);
218 void InitializeUpdateMechanismIfNecessary();
220 friend ClassCreator<System>;
224 std::weak_ptr<ProducerLibrary> m_parentLibrary;
226 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>
227 m_interfaceFoundCallbackManager;
228 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>
229 m_interfaceLostCallbackManager;
231 std::vector<std::shared_ptr<InterfaceDescriptor>> m_interfaces;
232 std::unordered_map<std::string, std::shared_ptr<InterfaceDescriptor>> m_interfacesByKey;
233 std::unordered_map<std::string, std::string> m_interfacesKeyById;
234 mutable std::mutex m_interfacesMutex;
235 std::once_flag m_updateMechanismInitializedFlag;
250 : m_backendHandle(systemHandle)
251 , m_parentLibrary(parentLibrary)
252 , m_interfaceFoundCallbackManager()
253 , m_interfaceLostCallbackManager()
254 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
258 m_interfaceFoundCallbackManager =
259 std::make_unique<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>(
260 [&](
void* callbackContext) {
261 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE>(
264 InterfaceFoundCallbackCWrapper, callbackContext, interfaceFoundCallbackHandle);
268 CallAndCheckCInterfaceFunction([&] {
270 m_backendHandle, callbackHandle);
274 m_interfaceLostCallbackManager =
275 std::make_unique<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>(
276 [&](
void* callbackContext) {
277 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_LOST_CALLBACK_HANDLE>(
280 InterfaceLostCallbackCWrapper, callbackContext, interfaceLostCallbackHandle);
284 CallAndCheckCInterfaceFunction([&] {
286 m_backendHandle, callbackHandle);
295 m_interfaceFoundCallbackManager->UnregisterAllCallbacks();
296 m_interfaceLostCallbackManager->UnregisterAllCallbacks();
311 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
318 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
325 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
332 return QueryStringFromCInterfaceFunction([&](
char* vendorName,
size_t* vendorNameSize) {
339 return QueryStringFromCInterfaceFunction([&](
char* modelName,
size_t* modelNameSize) {
346 return QueryStringFromCInterfaceFunction([&](
char* version,
size_t* versionSize) {
353 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
360 return QueryStringFromCInterfaceFunction([&](
char* ctiFileName,
size_t* ctiFileNameSize) {
367 return QueryStringFromCInterfaceFunction([&](
char* ctiFullPath,
size_t* ctiFullPathSize) {
374 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMajor) {
381 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMinor) {
389 QueryNumericFromCInterfaceFunction<PEAK_CHARACTER_ENCODING>(
397 InitializeUpdateMechanismIfNecessary();
399 CallAndCheckCInterfaceFunction(
405 std::lock_guard<std::mutex> lock(m_interfacesMutex);
411 return LockOrThrow(m_parentLibrary);
418 m_interfaceFoundCallbackManager->RegisterCallback(InterfaceFoundCallbackContainer{ shared_from_this(),
419 [callback](
const std::shared_ptr<InterfaceDescriptor>& foundInterface) { callback(foundInterface); } }));
424 m_interfaceFoundCallbackManager->UnregisterCallback(
436 m_interfaceLostCallbackManager->UnregisterCallback(
443 auto callbackContainer =
static_cast<InterfaceFoundCallbackContainer*
>(context);
445 auto interfaceDescriptor = callbackContainer->System_.lock()->GetOrAddFoundInterface(foundInterface);
447 callbackContainer->Callback(interfaceDescriptor);
451 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context)
455 callback->operator()(std::string(lostInterfaceId, lostInterfaceIdSize - 1));
458inline std::shared_ptr<InterfaceDescriptor> System::GetOrAddFoundInterface(
461 std::lock_guard<std::mutex> lock(m_interfacesMutex);
463 const auto interfaceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
466 auto keyInterfacePairIterator = m_interfacesByKey.find(interfaceKey);
467 if (keyInterfacePairIterator == m_interfacesByKey.end())
469 auto interfaceDescriptor = std::static_pointer_cast<InterfaceDescriptor>(
470 std::make_shared<ClassCreator<InterfaceDescriptor>>(
471 foundInterface, std::weak_ptr<System>{ shared_from_this() }));
472 m_interfaces.emplace_back(interfaceDescriptor);
473 m_interfacesByKey.emplace(interfaceKey, interfaceDescriptor);
474 m_interfacesKeyById.emplace(interfaceDescriptor->ID(), interfaceKey);
476 return interfaceDescriptor;
479 return keyInterfacePairIterator->second;
484 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
494 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
497 m_backendHandle, _eventSupportingModuleHandle);
500 return eventSupportingModuleHandle;
503inline void System::InitializeUpdateMechanismIfNecessary()
505 std::call_once(m_updateMechanismInitializedFlag, [&] {
510 std::lock_guard<std::mutex> lock(m_interfacesMutex);
511 const auto lostInterfaceKey = m_interfacesKeyById.at(lostInterfaceId);
512 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
513 [lostInterfaceKey](
const std::shared_ptr<InterfaceDescriptor>& interface_) {
514 return interface_->Key() == lostInterfaceKey;
516 std::end(m_interfaces));
517 m_interfacesByKey.erase(lostInterfaceKey);
518 m_interfacesKeyById.erase(lostInterfaceId);
EventSupportingModule()=default
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:26
Represents a GenTL producer library (CTI).
Definition peak_producer_library.hpp:39
uint32_t GenTLVersionMajor() const
Returns the GenTL major version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:372
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters a interface lost callback.
Definition peak_system.hpp:434
std::string TLType() const
Returns the TL (transport layer) type.
Definition peak_system.hpp:351
void UpdateInterfaces(Timeout timeout_ms)
Searches for interfaces.
Definition peak_system.hpp:395
std::function< void(const std::shared_ptr< InterfaceDescriptor > &foundInterface)> InterfaceFoundCallback
Definition peak_system.hpp:54
std::string CTIFileName() const
Returns the file name of the GenTL producer library this system belongs to.
Definition peak_system.hpp:358
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_system.hpp:422
std::string CTIFullPath() const
Returns the full path of the GenTL producer library this system belongs to.
Definition peak_system.hpp:365
System(const System &other)=delete
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_system.hpp:60
std::string DisplayName() const
Returns the display name.
Definition peak_system.hpp:323
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_system.hpp:414
uint32_t GenTLVersionMinor() const
Returns the GenTL minor version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:379
std::string Version() const
Returns the version.
Definition peak_system.hpp:344
System(System &&other)=delete
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_system.hpp:428
~System() override
Definition peak_system.hpp:291
std::string ID() const
Returns the ID.
Definition peak_system.hpp:316
peak::core::CharacterEncoding CharacterEncoding() const
Returns the character encoding of the GenTL producer library this system belongs to.
Definition peak_system.hpp:386
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:56
std::shared_ptr< ProducerLibrary > ParentLibrary() const
Returns the parent library.
Definition peak_system.hpp:409
System & operator=(const System &other)=delete
std::vector< std::shared_ptr< InterfaceDescriptor > > Interfaces() const
Returns the interface list.
Definition peak_system.hpp:403
std::string ModelName() const
Returns the model name.
Definition peak_system.hpp:337
std::function< void(const std::string &lostInterfaceId)> InterfaceLostCallback
Definition peak_system.hpp:58
System & operator=(System &&other)=delete
std::string VendorName() const
Returns the vendor name.
Definition peak_system.hpp:330
std::string Key() const
Returns the unique key.
Definition peak_system.hpp:304
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition peak_system.hpp:309
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:31
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:31
CharacterEncoding
Possible character encodings.
Definition peak_common_system_enums.hpp:25
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
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:515
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:421
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:377
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:477
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:373
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:107
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:513
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_C_ABI_PREFIX
Definition peak_dll_defines.h:16
#define PEAK_CALL_CONV
Definition peak_dynamic_loader.h:30