Template Class Creation
Defined in File creation.hpp
Class Documentation
-
template<typename DerivedClass, typename ErrorType>
class 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
Creationand your class has three more static factory methods -create,placementCreateandverify.createforwards 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 variablesm_isInitializedandm_errorValue. The user always has to setm_isInitializedto true when the object construction was successful otherwise one sets it to false and write the corresponding error cause in the providedm_errorValuevariable 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;
- 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
Public Types
-
using CreationPattern_t = Creation<DerivedClass, ErrorType>
-
using result_t = iox::cxx::expected<DerivedClass, ErrorType>
Public Functions
-
Creation() noexcept = default
-
bool isInitialized() const noexcept
returns true if the object was constructed successfully, otherwise false
Public Static 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
- Template Parameters:
Targs – the argument types which will be forwarded to the ctor
- Parameters:
args – [in] the 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.
-
static result_t verify(DerivedClass &&newObject) noexcept
verifies if a class was created successfully
- Parameters:
newObject – [in] rvalue 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.
-
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
- Template Parameters:
Targs – the argument types which will be forwarded to the ctor
- Parameters:
memory – [in] a piece of memory where the object is created into with placement new
args – [in] the 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.