iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | List of all members
iox::cxx::NewType< T, Policies > Class Template Reference

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>

Inheritance diagram for iox::cxx::NewType< T, Policies >:
Inheritance graph
[legend]
Collaboration diagram for iox::cxx::NewType< T, Policies >:
Collaboration graph
[legend]

Public Types

using ThisType = NewType< T, Policies... >
 the type of *this
 
using value_type = T
 the type of the underlying value
 

Public Member Functions

 NewType () noexcept
 default constructor
 
 NewType (const T &rhs) noexcept
 construct with value copy
 
 NewType (const NewType &rhs) noexcept
 copy constructor
 
 NewType (NewType &&rhs) noexcept
 move constructor
 
NewTypeoperator= (const NewType &rhs) noexcept
 copy assignment
 
NewTypeoperator= (NewType &&rhs) noexcept
 move assignment
 
NewTypeoperator= (const T &rhs) noexcept
 copy by value assignment
 
NewTypeoperator= (T &&rhs) noexcept
 copy by value assignment
 
 operator T () const noexcept
 conversion operator
 

Protected Member Functions

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

Detailed Description

template<typename T, template< typename > class... Policies>
class iox::cxx::NewType< T, Policies >

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
Implementation of the haskell NewType pattern: https://wiki.haskell.org/Newtype Lets say you would li...
Definition newtype.hpp:74

The documentation for this class was generated from the following file: