DynExp
Highly flexible laboratory automation for dynamically changing experiments.
DynExp::ObjectUserList Class Reference

Helper class to enable keeping track of instances of class Object making use of the owner of the respective ObjectUserList's instance. Instances of class Object can register or deregister multiple times as an user which increments or decrements their respective registration count in ObjectUserList::UserList. More...

+ Inheritance diagram for DynExp::ObjectUserList:

Public Member Functions

 ObjectUserList ()=default
 
virtual ~ObjectUserList ()=default
 
Util::ILockable::LockType AcquireLock (const std::chrono::milliseconds Timeout=DefaultTimeout) const
 Locks the user list for thread-safe manipulation. More...
 
void Register (const Object &User, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0))
 Registers a user in a thread-safe way. More...
 
LockType Register (const Object &User, LockType &&Lock)
 Registers a user making use of a lock-guard previously acquired by a call to AcquireLock(). More...
 
void Deregister (const Object &User, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0))
 Deregisters a user in a thread-safe way. More...
 
size_t CountUsers (std::chrono::milliseconds Timeout=std::chrono::milliseconds(0)) const
 Counts the registered useres in a thread-safe way. More...
 
ItemIDListType GetUserIDs (std::chrono::milliseconds Timeout=std::chrono::milliseconds(0)) const
 Returns a list of the IDs of the registered users in a thread-safe way. More...
 
std::string GetUserNamesString (std::chrono::milliseconds Timeout=std::chrono::milliseconds(0)) const
 Builds a string describing which users are registered containing their object names, categories and type names in a thread-safe way. More...
 
- Public Member Functions inherited from Util::INonCopyable
 INonCopyable (const INonCopyable &)=delete
 
INonCopyableoperator= (const INonCopyable &)=delete
 

Private Attributes

std::unordered_map< const Object *, size_t > UserList
 Map containing pointers to all users making use of this ObjectUserList instance's owner as keys and the amount of their active registrations as values. More...
 

Not thread-safe

AcquireLock() has to be called manually before and returned LockType has to be still in scope.

void DeregisterAllUnsafe ()
 Deregisters all users and notifies them that they need to check the states of their used linked objects. More...
 
size_t CountUsersUnsafe () const
 
ItemIDListType GetUserIDsUnsafe () const
 
std::string GetUserNamesStringUnsafe () const
 
void RegisterUnsafe (const Object &User)
 Registers a user. More...
 
void DeregisterUnsafe (const Object &User)
 Deregisters a user. More...
 

Additional Inherited Members

- Static Public Attributes inherited from Util::ILockable
static constexpr std::chrono::milliseconds DefaultTimeout = std::chrono::milliseconds(10)
 Duration which is used as a default timeout within all methods of this class if no different duration is passed to them. More...
 
- Protected Types inherited from Util::ILockable
using MutexType = std::timed_mutex
 
using LockType = std::unique_lock< MutexType >
 
- Protected Member Functions inherited from Util::ILockable
 ILockable ()=default
 
 ~ILockable ()=default
 
LockType AcquireLock (const std::chrono::milliseconds Timeout=DefaultTimeout) const
 Locks the internal mutex. Blocks until the mutex is locked or until the timeout duration is exceeded. More...
 
- Protected Member Functions inherited from Util::INonCopyable
constexpr INonCopyable ()=default
 
 ~INonCopyable ()=default
 

Detailed Description

Helper class to enable keeping track of instances of class Object making use of the owner of the respective ObjectUserList's instance. Instances of class Object can register or deregister multiple times as an user which increments or decrements their respective registration count in ObjectUserList::UserList.

Definition at line 79 of file Object.h.

Constructor & Destructor Documentation

◆ ObjectUserList()

DynExp::ObjectUserList::ObjectUserList ( )
default

◆ ~ObjectUserList()

virtual DynExp::ObjectUserList::~ObjectUserList ( )
virtualdefault

Member Function Documentation

◆ AcquireLock()

Util::ILockable::LockType DynExp::ObjectUserList::AcquireLock ( const std::chrono::milliseconds  Timeout = DefaultTimeout) const

Locks the user list for thread-safe manipulation.

Parameters
TimeoutTimeout of the mutex-locking operation.
Returns
Lock-guard of the underlying mutex. Only use temporarily. Do not store longer than necessary.

Definition at line 9 of file Object.cpp.

◆ CountUsers()

size_t DynExp::ObjectUserList::CountUsers ( std::chrono::milliseconds  Timeout = std::chrono::milliseconds(0)) const

Counts the registered useres in a thread-safe way.

Parameters
TimeoutTimeout of the mutex-locking operation.
Returns
Number of registered users.

Definition at line 35 of file Object.cpp.

◆ CountUsersUnsafe()

size_t DynExp::ObjectUserList::CountUsersUnsafe ( ) const

Definition at line 64 of file Object.cpp.

◆ Deregister()

void DynExp::ObjectUserList::Deregister ( const Object User,
const std::chrono::milliseconds  Timeout = std::chrono::milliseconds(0) 
)

Deregisters a user in a thread-safe way.

Parameters
UserInstance of class Object to be deregistered as a user.
TimeoutTimeout of the mutex-locking operation.

Definition at line 28 of file Object.cpp.

◆ DeregisterAllUnsafe()

void DynExp::ObjectUserList::DeregisterAllUnsafe ( )

Deregisters all users and notifies them that they need to check the states of their used linked objects.

Definition at line 56 of file Object.cpp.

◆ DeregisterUnsafe()

void DynExp::ObjectUserList::DeregisterUnsafe ( const Object User)
private

Deregisters a user.

Parameters
UserInstance of class Object to be deregistered as a user.
Exceptions
Util::UnderflowExceptionis thrown if UserList contains an entry for User with a registration count of zero. This should never happen.

Definition at line 101 of file Object.cpp.

◆ GetUserIDs()

ItemIDListType DynExp::ObjectUserList::GetUserIDs ( std::chrono::milliseconds  Timeout = std::chrono::milliseconds(0)) const

Returns a list of the IDs of the registered users in a thread-safe way.

Parameters
TimeoutTimeout of the mutex-locking operation.
Returns
List of IDs of registered users.

Definition at line 42 of file Object.cpp.

◆ GetUserIDsUnsafe()

ItemIDListType DynExp::ObjectUserList::GetUserIDsUnsafe ( ) const

Definition at line 72 of file Object.cpp.

◆ GetUserNamesString()

std::string DynExp::ObjectUserList::GetUserNamesString ( std::chrono::milliseconds  Timeout = std::chrono::milliseconds(0)) const

Builds a string describing which users are registered containing their object names, categories and type names in a thread-safe way.

Parameters
TimeoutTimeout of the mutex-locking operation.
Returns
String containing information about the registered users.

Definition at line 49 of file Object.cpp.

◆ GetUserNamesStringUnsafe()

std::string DynExp::ObjectUserList::GetUserNamesStringUnsafe ( ) const

Definition at line 82 of file Object.cpp.

◆ Register() [1/2]

void DynExp::ObjectUserList::Register ( const Object User,
const std::chrono::milliseconds  Timeout = std::chrono::milliseconds(0) 
)

Registers a user in a thread-safe way.

Parameters
UserInstance of class Object to be registered as a user.
TimeoutTimeout of the mutex-locking operation.

Definition at line 14 of file Object.cpp.

◆ Register() [2/2]

ObjectUserList::LockType DynExp::ObjectUserList::Register ( const Object User,
LockType &&  Lock 
)

Registers a user making use of a lock-guard previously acquired by a call to AcquireLock().

Parameters
UserInstance of class Object to be registered as a user.
LockLock-guard of the underlying mutex. Move to this function.
Returns
Lock-guard of the underlying mutex (the same one passed to this function).

Definition at line 21 of file Object.cpp.

◆ RegisterUnsafe()

void DynExp::ObjectUserList::RegisterUnsafe ( const Object User)
private

Registers a user.

Parameters
UserInstance of class Object to be registered as a user.
Exceptions
Util::OverflowExceptionis thrown if a value in UserList cannot be incremented due to size limitations in UserList's value data type.

Definition at line 91 of file Object.cpp.

Member Data Documentation

◆ UserList

std::unordered_map<const Object*, size_t> DynExp::ObjectUserList::UserList
private

Map containing pointers to all users making use of this ObjectUserList instance's owner as keys and the amount of their active registrations as values.

Definition at line 176 of file Object.h.


The documentation for this class was generated from the following files: