DesignPattern::Creation🔗
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🔗
Name | |
---|---|
using Creation< DerivedClass, ErrorType > | CreationPattern_t |
using iox::cxx::expected< DerivedClass, ErrorType > | result_t |
using ErrorType | errorType_t |
Public Functions🔗
Name | |
---|---|
template <typename... Targs> result_t |
create(Targs &&... args) factory method which guarantees that either a working object is produced or an error value describing the error during construction |
result_t | verify(DerivedClass && newObject) verifies if a class was created successfully |
template <typename... Targs> iox::cxx::expected< ErrorType > |
placementCreate(void *const memory, Targs &&... args) factory method which guarantees that either a working object is produced or an error value describing the error during construction |
Creation() =default | |
Creation(Creation && rhs) | |
Creation & | operator=(Creation && rhs) |
Creation(const Creation & rhs) =default | |
Creation & | operator=(const Creation & rhs) =default |
bool | isInitialized() const returns true if the object was constructed successfully, otherwise false |
Protected Attributes🔗
Name | |
---|---|
bool | m_isInitialized |
ErrorType | m_errorValue |
Detailed Description🔗
template <typename DerivedClass ,
typename ErrorType >
class DesignPattern::Creation;
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.
Template Parameters:
- DerivedClass the class which inherits from the creation pattern
- ErrorType the error type which is going to be used when an error occurs
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;
Public Types Documentation🔗
using CreationPattern_t🔗
using DesignPattern::Creation< DerivedClass, ErrorType >::CreationPattern_t = Creation<DerivedClass, ErrorType>;
using result_t🔗
using DesignPattern::Creation< DerivedClass, ErrorType >::result_t = iox::cxx::expected<DerivedClass, ErrorType>;
using errorType_t🔗
using DesignPattern::Creation< DerivedClass, ErrorType >::errorType_t = ErrorType;
Public Functions Documentation🔗
function create🔗
template <typename... Targs>
static result_t create(
Targs &&... args
)
factory method which guarantees that either a working object is produced or an error value describing the error during construction
Parameters:
- args the argument values which will be forwarded to the ctor
Template Parameters:
- Targs the argument types which will be forwarded to the ctor
Return: returns an expected which either contains the object in a valid constructed state or an error value stating why the construction failed.
function verify🔗
static result_t verify(
DerivedClass && newObject
)
verifies if a class was created successfully
Parameters:
- newObject rvalue of the object which should be verified
Return: 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.
function placementCreate🔗
template <typename... Targs>
static iox::cxx::expected< ErrorType > placementCreate(
void *const memory,
Targs &&... args
)
factory method which guarantees that either a working object is produced or an error value describing the error during construction
Parameters:
- memory a piece of memory where the object is created into with placement new
- args the argument values which will be forwarded to the ctor
Template Parameters:
- Targs the argument types which will be forwarded to the ctor
Return: returns an expected which either contains the object in a valid constructed state or an error value stating why the construction failed.
function Creation🔗
Creation() =default
function Creation🔗
Creation(
Creation && rhs
)
function operator=🔗
Creation & operator=(
Creation && rhs
)
function Creation🔗
Creation(
const Creation & rhs
) =default
function operator=🔗
Creation & operator=(
const Creation & rhs
) =default
function isInitialized🔗
bool isInitialized() const
returns true if the object was constructed successfully, otherwise false
Protected Attributes Documentation🔗
variable m_isInitialized🔗
bool m_isInitialized {false};
variable m_errorValue🔗
ErrorType m_errorValue;
Updated on 2 April 2022 at 16:37:47 CEST