iceoryx_hoofs 2.0.3
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
DesignPattern::Creation< DerivedClass, ErrorType > Class Template Reference

This pattern can be used if you write an abstraction where you have to throw an exception in the constructor when you for instance would like to manage a resource and the constructor was unable to acquire that resource. In this case you inherit from Creation and your class has three more static factory methods - create, placementCreate and verify. create forwards all arguments to the underlying class constructor and if the construction was successful an expected containing the type is returned, otherwise an error value which describes the error. Additionally, this class is providing two protected member variables m_isInitialized and m_errorValue. The user always has to set m_isInitialized to true when the object construction was successful otherwise one sets it to false and write the corresponding error cause in the provided m_errorValue variable which is then returned to the user. More...

#include <iceoryx_hoofs/design_pattern/creation.hpp>

Public Types

using CreationPattern_t = Creation< DerivedClass, ErrorType >
 
using result_t = iox::cxx::expected< DerivedClass, ErrorType >
 
using errorType_t = ErrorType
 

Public Member Functions

 Creation (Creation &&rhs) noexcept
 
Creationoperator= (Creation &&rhs) noexcept
 
 Creation (const Creation &rhs) noexcept=default
 
Creationoperator= (const Creation &rhs) noexcept=default
 
bool isInitialized () const noexcept
 returns true if the object was constructed successfully, otherwise false
 

Static Public Member Functions

template<typename... Targs>
static result_t create (Targs &&... args) noexcept
 factory method which guarantees that either a working object is produced or an error value describing the error during construction More...
 
static result_t verify (DerivedClass &&newObject) noexcept
 verifies if a class was created successfully More...
 
template<typename... Targs>
static iox::cxx::expected< ErrorType > placementCreate (void *const memory, Targs &&... args) noexcept
 factory method which guarantees that either a working object is produced or an error value describing the error during construction More...
 

Protected Attributes

bool m_isInitialized {false}
 
ErrorType m_errorValue
 

Detailed Description

template<typename DerivedClass, typename ErrorType>
class DesignPattern::Creation< DerivedClass, ErrorType >

This pattern can be used if you write an abstraction where you have to throw an exception in the constructor when you for instance would like to manage a resource and the constructor was unable to acquire that resource. In this case you inherit from Creation and your class has three more static factory methods - create, placementCreate and verify. create forwards all arguments to the underlying class constructor and if the construction was successful an expected containing the type is returned, otherwise an error value which describes the error. Additionally, this class is providing two protected member variables m_isInitialized and m_errorValue. The user always has to set m_isInitialized to true when the object construction was successful otherwise one sets it to false and write the corresponding error cause in the provided m_errorValue variable which is then returned to the user.

enum class MyResourceAbstractionError {
ResourceNotAvailable,
BlaBlubError
};
class MyResourceAbstraction : public Creation<MyResourceAbstraction, MyResourceAbstractionError> {
public:
// some public methods
MyResourceAbstraction & operator=(MyResourceAbstraction && rhs) noexcept {
if ( this != &rhs ) {
// always call the creation move assignment operator when you have a user defined
// move operation
CreationPattern_t::operator=(std::move(rhs));
// user move code
}
return *this;
}
// the creation pattern is the only one which should be allowed to construct
// the class, therefore it has to be friend of that class
friend class Creation<MyResourceAbstraction, MyResourceAbstractionError>;
private:
MyResourceAbstraction(int a) {
if ( a > 0) {
// we are able to initialize the class an set m_isInitialized to true
m_isInitialized = true;
} else {
// we are unable to construct the class therefore we have to set
// m_isInitialized to false and store the error code in the
// provided m_errorValue member
m_errorValue = MyResourceAbstractionError::ResourceNotAvailable;
m_isInitialized = false;
}
}
}
// if the system resource is movable
auto resource = MyResourceAbstraction::Create(123);
if ( resource.has_error() && resource.get_error() == MyResourceAbstractionError::ResourceNotAvailable )
// perform error handling
else
// perform some work
// if the system resource is not movable
MyResourceAbstraction * resource = malloc(sizeof(MyResourceAbstraction));
auto result = MyResourceAbstraction::placementCreate(resource, 123);
if ( result.has_error() )
// perform error handling
else
resource->DoStuff();
delete resource;
This pattern can be used if you write an abstraction where you have to throw an exception in the cons...
Definition: creation.hpp:99
Template Parameters
DerivedClassthe class which inherits from the creation pattern
ErrorTypethe error type which is going to be used when an error occurs

Member Function Documentation

◆ create()

template<typename DerivedClass , typename ErrorType >
template<typename... Targs>
static result_t DesignPattern::Creation< DerivedClass, ErrorType >::create ( Targs &&...  args)
staticnoexcept

factory method which guarantees that either a working object is produced or an error value describing the error during construction

Template Parameters
Targsthe argument types which will be forwarded to the ctor
Parameters
[in]argsthe argument values which will be forwarded to the ctor
Returns
returns an expected which either contains the object in a valid constructed state or an error value stating why the construction failed.

◆ placementCreate()

template<typename DerivedClass , typename ErrorType >
template<typename... Targs>
static iox::cxx::expected< ErrorType > DesignPattern::Creation< DerivedClass, ErrorType >::placementCreate ( void *const  memory,
Targs &&...  args 
)
staticnoexcept

factory method which guarantees that either a working object is produced or an error value describing the error during construction

Template Parameters
Targsthe argument types which will be forwarded to the ctor
Parameters
[in]memorya piece of memory where the object is created into with placement new
[in]argsthe argument values which will be forwarded to the ctor
Returns
returns an expected which either contains the object in a valid constructed state or an error value stating why the construction failed.

◆ verify()

template<typename DerivedClass , typename ErrorType >
static result_t DesignPattern::Creation< DerivedClass, ErrorType >::verify ( DerivedClass &&  newObject)
staticnoexcept

verifies if a class was created successfully

Parameters
[in]newObjectrvalue of the object which should be verified
Returns
returns an expected which either contains the object in a valid constructed state or an error value stating why it was in an invalid state.

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