template<class Resource>
Rodin::Threads::Shared class

A thread-safe wrapper for shared resources.

Template parameters
Resource The type of the resource to be encapsulated.

The Shared class encapsulates a resource and provides thread-safe read and write access. When RODIN_THREAD_SAFE is defined, access to the resource is guarded by a std::shared_mutex. In non-thread-safe builds, the class provides direct access to the underlying resource.

Constructors, destructors, conversion operators

Shared() constexpr
Default constructor.
Shared(const Resource& resource) constexpr
Constructs a Shared object from a const reference.
Shared(Resource&& resource) constexpr
Constructs a Shared object by moving a resource.
Shared(Shared&& other) constexpr
Move constructor.
Shared(const Shared& other) constexpr
Copy constructor.

Public functions

auto operator=(const Resource&) -> Shared& deleted constexpr
Copy assignment operator from a Resource.
auto operator=(Resource&&) -> Shared& deleted constexpr
Move assignment operator from a Resource.
auto operator=(const Shared& other) -> Shared& constexpr
Copy assignment operator.
auto operator=(Shared&& other) -> Shared& constexpr
Move assignment operator.
auto write() -> Resource& constexpr
Provides non-const (write) access to the encapsulated resource.
auto read() const -> const Resource& constexpr
Provides read-only access to the encapsulated resource.
template<class F>
auto read(F&& f) const -> const Shared& constexpr
Executes a callable with read-only access to the resource.
template<class F>
auto write(F&& f) -> Shared& constexpr
Executes a callable with write access to the resource.

Function documentation

template<class Resource>
Rodin::Threads::Shared<Resource>::Shared() constexpr

Default constructor.

Constructs an empty Shared object.

template<class Resource>
Rodin::Threads::Shared<Resource>::Shared(const Resource& resource) constexpr

Constructs a Shared object from a const reference.

Parameters
resource The resource to encapsulate.

template<class Resource>
Rodin::Threads::Shared<Resource>::Shared(Resource&& resource) constexpr

Constructs a Shared object by moving a resource.

Parameters
resource The resource to encapsulate.

template<class Resource>
Rodin::Threads::Shared<Resource>::Shared(Shared&& other) constexpr

Move constructor.

Parameters
other The Shared object to move from.

Transfers the resource from another Shared object.

template<class Resource>
Rodin::Threads::Shared<Resource>::Shared(const Shared& other) constexpr

Copy constructor.

Parameters
other The Shared object to copy from.

Creates a Shared object by copying the resource from another.

template<class Resource>
Shared& Rodin::Threads::Shared<Resource>::operator=(const Resource&) deleted constexpr

Copy assignment operator from a Resource.

This operation is deleted to prevent direct assignment from a resource.

template<class Resource>
Shared& Rodin::Threads::Shared<Resource>::operator=(Resource&&) deleted constexpr

Move assignment operator from a Resource.

This operation is deleted to prevent direct move-assignment from a resource.

template<class Resource>
Shared& Rodin::Threads::Shared<Resource>::operator=(const Shared& other) constexpr

Copy assignment operator.

Parameters
other The Shared object to copy from.
Returns A reference to this Shared object.

Assigns the resource from another Shared object. In thread-safe builds, this operation is protected by locking.

template<class Resource>
Shared& Rodin::Threads::Shared<Resource>::operator=(Shared&& other) constexpr

Move assignment operator.

Parameters
other The Shared object to move from.
Returns A reference to this Shared object.

Transfers the resource from another Shared object. In thread-safe builds, this operation is protected by locking.

template<class Resource>
Resource& Rodin::Threads::Shared<Resource>::write() constexpr

Provides non-const (write) access to the encapsulated resource.

Returns A reference to the encapsulated resource.

template<class Resource>
const Resource& Rodin::Threads::Shared<Resource>::read() const constexpr

Provides read-only access to the encapsulated resource.

Returns A const reference to the encapsulated resource.

template<class Resource> template<class F>
const Shared& Rodin::Threads::Shared<Resource>::read(F&& f) const constexpr

Executes a callable with read-only access to the resource.

Template parameters
F The type of the callable.
Parameters
f The callable to execute.
Returns A const reference to this Shared object.

The provided callable is invoked with a const reference to the resource. In thread-safe builds, the shared lock is acquired during the execution of the callable.

template<class Resource> template<class F>
Shared& Rodin::Threads::Shared<Resource>::write(F&& f) constexpr

Executes a callable with write access to the resource.

Template parameters
F The type of the callable.
Parameters
f The callable to execute.
Returns A reference to this Shared object.

The provided callable is invoked with a non-const reference to the resource. In thread-safe builds, the exclusive lock is acquired during the execution of the callable.