iox::cxx::NewType🔗
Implementation of the haskell NewType pattern: https://wiki.haskell.org/Newtype Lets say you would like to have an index which is in the end an integer but with certain restraints. The users should be forced to set it when they are creating it but afterwards it should be immutable. You would like to be able to compare the type as well as to sort it so that it can be stored in a map for instance. An example could be that you would like to have an index class with those properties and some additional methods. Then you can inherit from NewType and add your methods. More...
#include <newtype.hpp>
Inherits from Policies< NewType< T, Policies... > >
Public Types🔗
Name | |
---|---|
using NewType< T, Policies... > | ThisType the type of *this |
using T | value_type the type of the underlying value |
Public Functions🔗
Name | |
---|---|
NewType() default constructor |
|
NewType(const T & rhs) construct with value copy |
|
NewType(const NewType & rhs) copy constructor |
|
NewType(NewType && rhs) move constructor |
|
NewType & | operator=(const NewType & rhs) copy assignment |
NewType & | operator=(NewType && rhs) move assignment |
NewType & | operator=(const T & rhs) copy by value assignment |
NewType & | operator=(T && rhs) copy by value assignment |
operator T() const conversion operator |
Protected Functions🔗
Name | |
---|---|
NewType(newtype::internal::ProtectedConstructor_t , const T & rhs) |
Friends🔗
Name | |
---|---|
Type::value_type | newtype::internal::newTypeAccessor(const Type & ) |
Detailed Description🔗
template <typename T ,
template< typename > class... Policies>
class iox::cxx::NewType;
Implementation of the haskell NewType pattern: https://wiki.haskell.org/Newtype Lets say you would like to have an index which is in the end an integer but with certain restraints. The users should be forced to set it when they are creating it but afterwards it should be immutable. You would like to be able to compare the type as well as to sort it so that it can be stored in a map for instance. An example could be that you would like to have an index class with those properties and some additional methods. Then you can inherit from NewType and add your methods.
class Index : public NewType<int,
newtype::ConstructByValueCopy,
newtype::Comparable,
newtype::Sortable,
newtype::AssignByValueCopy>
{
public:
// VERY IMPORTANT: we have to put the constructors and operator= in scope
// otherwise the code will not compile
using ThisType::ThisType; // this makes all constructors of NewType available for Index
using ThisType::operator=; // put the assignment operators in scope
};
Index a(123), c(456); // allowed since we are using the policy ConstructByValueCopy
// Index b; // not allowed since we are not using the policy DefaultConstructable
if ( a < c ) {} // allowed since we are Sortable
a = 567; // allowed since we are assignable
Public Types Documentation🔗
using ThisType🔗
using iox::cxx::NewType< T, Policies >::ThisType = NewType<T, Policies...>;
the type of *this
using value_type🔗
using iox::cxx::NewType< T, Policies >::value_type = T;
the type of the underlying value
Public Functions Documentation🔗
function NewType🔗
inline NewType()
default constructor
function NewType🔗
inline explicit NewType(
const T & rhs
)
construct with value copy
function NewType🔗
inline NewType(
const NewType & rhs
)
copy constructor
function NewType🔗
inline NewType(
NewType && rhs
)
move constructor
function operator=🔗
inline NewType & operator=(
const NewType & rhs
)
copy assignment
function operator=🔗
inline NewType & operator=(
NewType && rhs
)
move assignment
function operator=🔗
inline NewType & operator=(
const T & rhs
)
copy by value assignment
function operator=🔗
inline NewType & operator=(
T && rhs
)
copy by value assignment
function operator T🔗
inline explicit operator T() const
conversion operator
Protected Functions Documentation🔗
function NewType🔗
inline NewType(
newtype::internal::ProtectedConstructor_t ,
const T & rhs
)
Friends🔗
friend newtype::internal::newTypeAccessor🔗
friend Type::value_type newtype::internal::newTypeAccessor(
const Type &
);
Updated on 26 April 2021 at 15:31:01 CEST