mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
mrs_lib::Pimpl< Interface, StorageType > Class Template Reference

Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implementation (PIMPL) pattern. More...

#include <pimpl.hpp>

Public Member Functions

 Pimpl (std::nullptr_t)=delete
 Deleted constructor to prevent construction from nullptr.
 Pimpl (StorageType impl)
 Construct Pimpl from a pointer to the object.
template<typename T, typename... Args>
 Pimpl (std::in_place_type_t< T >, Args &&... args)
 Construct the wrapped object in place.
 ~Pimpl ()=default
 Default destructor.
 Pimpl (const Pimpl &)=delete
 Pimpl is not copyable.
Pimpl & operator= (const Pimpl &)=delete
 Pimpl is not copyable.
 Pimpl (Pimpl &&)=default
 Default move constructor.
Pimpl & operator= (Pimpl &&)=default
 Default move assignment operator.
Interface & operator* () noexcept
 Dereference operator for non-const access.
const Interface & operator* () const noexcept
 Dereference operator for const access.
Interface * operator-> () noexcept
 Arrow operator for non-const access.
const Interface * operator-> () const noexcept
 Arrow operator for const access.
bool valueless_after_move () const noexcept
 Checks if the object is in moved from state.

Detailed Description

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
class mrs_lib::Pimpl< Interface, StorageType >

Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implementation (PIMPL) pattern.

Template Parameters
InterfaceThe interface / class type that the Pimpl will hold.
StorageTypeThe type of value used to store for the implementation (defaults to std::unique_ptr<Interface>).

Contains the specified value using the StorageType. Compared to std::*_ptr, this class uses deep const semantics and is not nullable (with the exception of the moved from state in which it should not be used).

When implementing the PIMPL pattern for your own classes, the default pointer type should be correct.

This class can also be used to wrap non nullable ros interfaces (eg. rclcpp::Publisher), which are provided as std::shared_ptr. In those cases you can set StorageType to std::shared_ptr (eg. for the publisher: mrs_lib::Pimpl<rclcpp::Publisher, std::shared_ptr<rclcpp::Publisher>>). The class will keep the same semantics, but will use different storage.

Constructor & Destructor Documentation

◆ Pimpl() [1/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
mrs_lib::Pimpl< Interface, StorageType >::Pimpl ( StorageType impl)
inlineexplicit

Construct Pimpl from a pointer to the object.

Parameters
implThe pointer to the implementation object.
Exceptions
std::logic_errorif impl is nullptr.

◆ Pimpl() [2/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
template<typename T, typename... Args>
mrs_lib::Pimpl< Interface, StorageType >::Pimpl ( std::in_place_type_t< T > ,
Args &&... args )
inlineexplicit

Construct the wrapped object in place.

Template Parameters
TThe concrete type of the object to construct.
ArgsTypes of arguments passed to the inner object's constructor.
Parameters
-Tag type to distinguish between overloads and select the concrete type.
argsArguments to construct the inner object with.

Member Function Documentation

◆ operator*() [1/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
const Interface & mrs_lib::Pimpl< Interface, StorageType >::operator* ( ) const
inlinenodiscardnoexcept

Dereference operator for const access.

Returns
Reference to the underlying implementation object.

◆ operator*() [2/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
Interface & mrs_lib::Pimpl< Interface, StorageType >::operator* ( )
inlinenodiscardnoexcept

Dereference operator for non-const access.

Returns
Reference to the underlying implementation object.

◆ operator->() [1/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
const Interface * mrs_lib::Pimpl< Interface, StorageType >::operator-> ( ) const
inlinenodiscardnoexcept

Arrow operator for const access.

Returns
Pointer to the underlying implementation object.

◆ operator->() [2/2]

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
Interface * mrs_lib::Pimpl< Interface, StorageType >::operator-> ( )
inlinenodiscardnoexcept

Arrow operator for non-const access.

Returns
Pointer to the underlying implementation object.

◆ valueless_after_move()

template<typename Interface, typename StorageType = std::unique_ptr<Interface>>
bool mrs_lib::Pimpl< Interface, StorageType >::valueless_after_move ( ) const
inlinenodiscardnoexcept

Checks if the object is in moved from state.

Can be used on moved from object.

Note
This function should not be needed in most cases. You should not keep moved from objects alive for long periods of time.

The documentation for this class was generated from the following file:
  • /home/runner/work/mrs_lib/mrs_lib/include/mrs_lib/utility/pimpl.hpp