Skip to content

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 <iceoryx_hoofs/cxx/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)

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

        // implement ctors and assignment operators when they are implemented by the base class
        // this is necessary to prevent warnings from some compilers
        Index() noexcept = default;
        Index(const Index&) noexcept = default;
        Index(Index&&) noexcept = default;
        Index& operator=(const Index&) noexcept = default;
        Index& operator=(Index&&) noexcept = default;
};

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🔗

NewType()

default constructor

function NewType🔗

explicit NewType(
    const T & rhs
)

construct with value copy

function NewType🔗

NewType(
    const NewType & rhs
)

copy constructor

function NewType🔗

NewType(
    NewType && rhs
)

move constructor

function operator=🔗

NewType & operator=(
    const NewType & rhs
)

copy assignment

function operator=🔗

NewType & operator=(
    NewType && rhs
)

move assignment

function operator=🔗

NewType & operator=(
    const T & rhs
)

copy by value assignment

function operator=🔗

NewType & operator=(
    T && rhs
)

copy by value assignment

function operator T🔗

explicit operator T() const

conversion operator

Protected Functions Documentation🔗

function NewType🔗

NewType(
    newtype::internal::ProtectedConstructor_t ,
    const T & rhs
)

Updated on 31 May 2022 at 11:34:55 CEST