17 #include <peak/dll_interface/peak_dll_interface_util.hpp> 18 #include <peak/generic/peak_t_callback_manager.hpp> 22 #include <unordered_map> 37 class ProducerLibrary;
39 struct InterfaceFoundCallbackContainer;
50 ,
public std::enable_shared_from_this<System>
70 std::string
Key()
const;
83 std::string
ID()
const;
93 std::string
TLType()
const;
129 std::vector<std::shared_ptr<InterfaceDescriptor>>
Interfaces()
const;
189 struct InterfaceFoundCallbackContainer
191 std::weak_ptr<System> _System;
197 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context);
203 void InitializeUpdateMechanismIfNecessary();
205 friend ClassCreator<System>;
209 std::weak_ptr<ProducerLibrary> m_parentLibrary;
211 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>
212 m_interfaceFoundCallbackManager;
213 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>
214 m_interfaceLostCallbackManager;
216 std::vector<std::shared_ptr<InterfaceDescriptor>> m_interfaces;
217 std::unordered_map<std::string, std::shared_ptr<InterfaceDescriptor>> m_interfacesByKey;
218 std::unordered_map<std::string, std::string> m_interfacesKeyById;
219 mutable std::mutex m_interfacesMutex;
220 std::once_flag m_updateMechanismInitializedFlag;
235 : m_backendHandle(systemHandle)
236 , m_parentLibrary(parentLibrary)
237 , m_interfaceFoundCallbackManager()
238 , m_interfaceLostCallbackManager()
239 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
243 m_interfaceFoundCallbackManager =
244 std::make_unique<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>(
245 [&](
void* callbackContext) {
246 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE>(
249 InterfaceFoundCallbackCWrapper, callbackContext, interfaceFoundCallbackHandle);
253 CallAndCheckCInterfaceFunction([&] {
255 m_backendHandle, callbackHandle);
259 m_interfaceLostCallbackManager =
260 std::make_unique<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>(
261 [&](
void* callbackContext) {
262 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_LOST_CALLBACK_HANDLE>(
265 InterfaceLostCallbackCWrapper, callbackContext, interfaceLostCallbackHandle);
269 CallAndCheckCInterfaceFunction([&] {
271 m_backendHandle, callbackHandle);
280 m_interfaceFoundCallbackManager->UnregisterAllCallbacks();
281 m_interfaceLostCallbackManager->UnregisterAllCallbacks();
296 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
303 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
310 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
317 return QueryStringFromCInterfaceFunction([&](
char* vendorName,
size_t* vendorNameSize) {
324 return QueryStringFromCInterfaceFunction([&](
char* modelName,
size_t* modelNameSize) {
331 return QueryStringFromCInterfaceFunction([&](
char* version,
size_t* versionSize) {
338 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
345 return QueryStringFromCInterfaceFunction([&](
char* ctiFileName,
size_t* ctiFileNameSize) {
352 return QueryStringFromCInterfaceFunction([&](
char* ctiFullPath,
size_t* ctiFullPathSize) {
359 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMajor) {
366 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMinor) {
374 QueryNumericFromCInterfaceFunction<PEAK_CHARACTER_ENCODING>(
382 InitializeUpdateMechanismIfNecessary();
384 CallAndCheckCInterfaceFunction(
390 std::lock_guard<std::mutex> lock(m_interfacesMutex);
396 return LockOrThrow(m_parentLibrary);
403 m_interfaceFoundCallbackManager->RegisterCallback(InterfaceFoundCallbackContainer{ shared_from_this(),
404 [callback](
const std::shared_ptr<InterfaceDescriptor>& foundInterface) { callback(foundInterface); } }));
409 m_interfaceFoundCallbackManager->UnregisterCallback(
410 reinterpret_cast<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE>(callbackHandle));
421 m_interfaceLostCallbackManager->UnregisterCallback(
422 reinterpret_cast<PEAK_INTERFACE_LOST_CALLBACK_HANDLE>(callbackHandle));
425 inline void PEAK_CALL_CONV System::InterfaceFoundCallbackCWrapper(
428 auto callbackContainer =
static_cast<InterfaceFoundCallbackContainer*
>(context);
430 auto interfaceDescriptor = callbackContainer->_System.lock()->GetOrAddFoundInterface(foundInterface);
432 callbackContainer->Callback(interfaceDescriptor);
436 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context)
440 callback->operator()(std::string(lostInterfaceId, lostInterfaceIdSize - 1));
443 inline std::shared_ptr<InterfaceDescriptor> System::GetOrAddFoundInterface(
446 std::lock_guard<std::mutex> lock(m_interfacesMutex);
448 const auto interfaceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
451 auto keyInterfacePairIterator = m_interfacesByKey.find(interfaceKey);
452 if (keyInterfacePairIterator == m_interfacesByKey.end())
455 std::make_shared<ClassCreator<InterfaceDescriptor>>(
456 foundInterface, std::weak_ptr<System>{ shared_from_this() }));
457 m_interfaces.emplace_back(interfaceDescriptor);
458 m_interfacesByKey.emplace(interfaceKey, interfaceDescriptor);
459 m_interfacesKeyById.emplace(interfaceDescriptor->ID(), interfaceKey);
461 return interfaceDescriptor;
464 return keyInterfacePairIterator->second;
469 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
479 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
482 m_backendHandle, _eventSupportingModuleHandle);
485 return eventSupportingModuleHandle;
488 inline void System::InitializeUpdateMechanismIfNecessary()
490 std::call_once(m_updateMechanismInitializedFlag, [&] {
495 std::lock_guard<std::mutex> lock(m_interfacesMutex);
496 const auto lostInterfaceKey = m_interfacesKeyById.at(lostInterfaceId);
497 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
498 [lostInterfaceKey](
const std::shared_ptr<InterfaceDescriptor>& interface_) {
499 return interface_->Key() == lostInterfaceKey;
501 std::end(m_interfaces));
502 m_interfacesByKey.erase(lostInterfaceKey);
503 m_interfacesKeyById.erase(lostInterfaceId);
Encapsulates the GenTL functions associated with a GenTL Interface module's ID.
Definition: peak_interface_descriptor.hpp:39
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_GetCharacterEncoding(PEAK_SYSTEM_HANDLE systemHandle, PEAK_CHARACTER_ENCODING *characterEncoding)
Asks the given system for its character encoding.
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.
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition: peak_system.hpp:407
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.
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.
struct PEAK_INTERFACE_DESCRIPTOR * PEAK_INTERFACE_DESCRIPTOR_HANDLE
Definition: peak_backend.h:357
std::shared_ptr< ProducerLibrary > ParentLibrary() const
Returns the parent library.
Definition: peak_system.hpp:394
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition: peak_dynamic_loader.h:32
PEAK_C_API PEAK_System_GetVendorName(PEAK_SYSTEM_HANDLE systemHandle, char *vendorName, size_t *vendorNameSize)
Asks the given system for its vendor name.
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition: peak_backend.h:457
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition: peak_system.hpp:399
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition: peak_system.hpp:56
PEAK_C_API PEAK_System_GetTLType(PEAK_SYSTEM_HANDLE systemHandle, char *tlType, size_t *tlTypeSize)
Asks the given system for its TL type.
std::vector< std::shared_ptr< InterfaceDescriptor > > Interfaces() const
Returns the interface list.
Definition: peak_system.hpp:388
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.
int32_t PEAK_CHARACTER_ENCODING
Definition: peak_backend.h:90
std::string ID() const
Returns the ID.
Definition: peak_system.hpp:301
std::function< void(const std::shared_ptr< InterfaceDescriptor > &foundInterface)> InterfaceFoundCallback
Definition: peak_system.hpp:54
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_GetKey(PEAK_SYSTEM_HANDLE systemHandle, char *key, size_t *keySize)
Asks the given system for its key.
PEAK_C_API PEAK_System_ToModule(PEAK_SYSTEM_HANDLE systemHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given system to a module.
Represents a GenTL System module.
Definition: peak_system.hpp:48
std::string ModelName() const
Returns the model name.
Definition: peak_system.hpp:322
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_C_API PEAK_System_GetDisplayName(PEAK_SYSTEM_HANDLE systemHandle, char *displayName, size_t *displayNameSize)
Asks the given system for its display name.
std::string Key() const
Returns the unique key.
Definition: peak_system.hpp:289
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.
uint32_t GenTLVersionMajor() const
Returns the GenTL major version of the GenTL producer library this system belongs to...
Definition: peak_system.hpp:357
std::string DisplayName() const
Returns the display name.
Definition: peak_system.hpp:308
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition: peak_system.hpp:294
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.
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition: peak_system.hpp:413
uint32_t GenTLVersionMinor() const
Returns the GenTL minor version of the GenTL producer library this system belongs to...
Definition: peak_system.hpp:364
Represents a timeout value in milliseconds.
Definition: peak_timeout.hpp:30
std::string CTIFileName() const
Returns the file name of the GenTL producer library this system belongs to.
Definition: peak_system.hpp:343
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.
std::string TLType() const
Returns the TL (transport layer) type.
Definition: peak_system.hpp:336
struct PEAK_SYSTEM * PEAK_SYSTEM_HANDLE
Definition: peak_backend.h:353
void UpdateInterfaces(Timeout timeout_ms)
Searches for interfaces.
Definition: peak_system.hpp:380
The base class for all exceptions thrown by the library.
Definition: peak_exception.hpp:25
std::string VendorName() const
Returns the vendor name.
Definition: peak_system.hpp:315
~System() override
Definition: peak_system.hpp:276
CharacterEncoding
Possible character encodings.
Definition: peak_common_system_enums.hpp:24
#define PEAK_CALL_CONV
Definition: peak_dynamic_loader.h:29
#define PEAK_C_ABI_PREFIX
Definition: peak_dll_defines.h:16
System & operator=(const System &other)=delete
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_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_GetID(PEAK_SYSTEM_HANDLE systemHandle, char *id, size_t *idSize)
Asks the given system for its ID.
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition: peak_backend.h:401
PEAK_C_API PEAK_System_Destruct(PEAK_SYSTEM_HANDLE systemHandle)
Destroys the given system.
std::function< void(const std::string &lostInterfaceId)> InterfaceLostCallback
Definition: peak_system.hpp:58
peak::core::CharacterEncoding CharacterEncoding() const
Returns the character encoding of the GenTL producer library this system belongs to.
Definition: peak_system.hpp:371
std::string CTIFullPath() const
Returns the full path of the GenTL producer library this system belongs to.
Definition: peak_system.hpp:350
The base class for all modules being able to raise events.
Definition: peak_event_supporting_module.hpp:33
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters a interface lost callback.
Definition: peak_system.hpp:419
PEAK_INTERFACE_LOST_CALLBACK * PEAK_INTERFACE_LOST_CALLBACK_HANDLE
Definition: peak_backend.h:489
PEAK_INTERFACE_FOUND_CALLBACK * PEAK_INTERFACE_FOUND_CALLBACK_HANDLE
Definition: peak_backend.h:487
std::string Version() const
Returns the version.
Definition: peak_system.hpp:329
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.
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition: peak_system.hpp:60
PEAK_C_API PEAK_System_GetModelName(PEAK_SYSTEM_HANDLE systemHandle, char *modelName, size_t *modelNameSize)
Asks the given system for its model name.