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...
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:
MyResourceAbstraction & operator=(MyResourceAbstraction && rhs) noexcept {
if ( this != &rhs ) {
CreationPattern_t::operator=(std::move(rhs));
}
return *this;
}
friend class Creation<MyResourceAbstraction, MyResourceAbstractionError>;
private:
MyResourceAbstraction(int a) {
if ( a > 0) {
m_isInitialized = true;
} else {
m_errorValue = MyResourceAbstractionError::ResourceNotAvailable;
m_isInitialized = false;
}
}
}
auto resource = MyResourceAbstraction::Create(123);
if ( resource.has_error() && resource.get_error() == MyResourceAbstractionError::ResourceNotAvailable )
else
MyResourceAbstraction * resource = malloc(sizeof(MyResourceAbstraction));
auto result = MyResourceAbstraction::placementCreate(resource, 123);
if ( result.has_error() )
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
-
DerivedClass | the class which inherits from the creation pattern |
ErrorType | the error type which is going to be used when an error occurs |