xrpld
Loading...
Searching...
No Matches
xrpl::TaggedInteger< Int, Tag > Class Template Reference

A type-safe wrap around standard integral types. More...

#include <tagged_integer.h>

Inheritance diagram for xrpl::TaggedInteger< Int, Tag >:
Collaboration diagram for xrpl::TaggedInteger< Int, Tag >:

Public Types

using value_type = Int
using tag_type = Tag

Public Member Functions

 TaggedInteger ()=default
template<class OtherInt>
requires (std::is_integral_v<OtherInt> && sizeof(OtherInt) <= sizeof(Int))
constexpr TaggedInteger (OtherInt value) noexcept
bool operator< (TaggedInteger const &rhs) const noexcept
bool operator== (TaggedInteger const &rhs) const noexcept
TaggedIntegeroperator+= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator-= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator*= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator/= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator%= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator|= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator&= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator^= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator<<= (TaggedInteger const &rhs) noexcept
TaggedIntegeroperator>>= (TaggedInteger const &rhs) noexcept
TaggedInteger operator~ () const noexcept
TaggedInteger operator+ () const noexcept
TaggedInteger operator- () const noexcept
TaggedIntegeroperator++ () noexcept
TaggedIntegeroperator-- () noexcept
 operator Int () const noexcept

Private Attributes

Int value_

Friends

std::ostreamoperator<< (std::ostream &s, TaggedInteger const &t)
std::istreamoperator>> (std::istream &s, TaggedInteger &t)
std::string to_string (TaggedInteger const &t)

Detailed Description

template<class Int, class Tag>
class xrpl::TaggedInteger< Int, Tag >

A type-safe wrap around standard integral types.

The tag is used to implement type safety, catching mismatched types at compile time. Multiple instantiations wrapping the same underlying integral type are distinct types (distinguished by tag) and will not interoperate. A tagged_integer supports all the usual assignment, arithmetic, comparison and shifting operations defined for the underlying type

The tag is not meant as a unit, which would require restricting the set of allowed arithmetic operations.

Definition at line 28 of file tagged_integer.h.

Member Typedef Documentation

◆ value_type

template<class Int, class Tag>
using xrpl::TaggedInteger< Int, Tag >::value_type = Int

Definition at line 42 of file tagged_integer.h.

◆ tag_type

template<class Int, class Tag>
using xrpl::TaggedInteger< Int, Tag >::tag_type = Tag

Definition at line 43 of file tagged_integer.h.

Constructor & Destructor Documentation

◆ TaggedInteger() [1/2]

template<class Int, class Tag>
xrpl::TaggedInteger< Int, Tag >::TaggedInteger ( )
default

◆ TaggedInteger() [2/2]

template<class Int, class Tag>
template<class OtherInt>
requires (std::is_integral_v<OtherInt> && sizeof(OtherInt) <= sizeof(Int))
xrpl::TaggedInteger< Int, Tag >::TaggedInteger ( OtherInt value)
explicitconstexprnoexcept

Definition at line 48 of file tagged_integer.h.

Member Function Documentation

◆ operator<()

template<class Int, class Tag>
bool xrpl::TaggedInteger< Int, Tag >::operator< ( TaggedInteger< Int, Tag > const & rhs) const
noexcept

Definition at line 55 of file tagged_integer.h.

◆ operator==()

template<class Int, class Tag>
bool xrpl::TaggedInteger< Int, Tag >::operator== ( TaggedInteger< Int, Tag > const & rhs) const
noexcept

Definition at line 62 of file tagged_integer.h.

◆ operator+=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator+= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 68 of file tagged_integer.h.

◆ operator-=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator-= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 75 of file tagged_integer.h.

◆ operator*=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator*= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 82 of file tagged_integer.h.

◆ operator/=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator/= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 89 of file tagged_integer.h.

◆ operator%=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator%= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 96 of file tagged_integer.h.

◆ operator|=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator|= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 103 of file tagged_integer.h.

◆ operator&=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator&= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 110 of file tagged_integer.h.

◆ operator^=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator^= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 117 of file tagged_integer.h.

◆ operator<<=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator<<= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 123 of file tagged_integer.h.

◆ operator>>=()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator>>= ( TaggedInteger< Int, Tag > const & rhs)
noexcept

Definition at line 131 of file tagged_integer.h.

◆ operator~()

template<class Int, class Tag>
TaggedInteger xrpl::TaggedInteger< Int, Tag >::operator~ ( ) const
noexcept

Definition at line 138 of file tagged_integer.h.

◆ operator+()

template<class Int, class Tag>
TaggedInteger xrpl::TaggedInteger< Int, Tag >::operator+ ( ) const
noexcept

Definition at line 144 of file tagged_integer.h.

◆ operator-()

template<class Int, class Tag>
TaggedInteger xrpl::TaggedInteger< Int, Tag >::operator- ( ) const
noexcept

Definition at line 150 of file tagged_integer.h.

◆ operator++()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator++ ( )
noexcept

Definition at line 156 of file tagged_integer.h.

◆ operator--()

template<class Int, class Tag>
TaggedInteger & xrpl::TaggedInteger< Int, Tag >::operator-- ( )
noexcept

Definition at line 163 of file tagged_integer.h.

◆ operator Int()

template<class Int, class Tag>
xrpl::TaggedInteger< Int, Tag >::operator Int ( ) const
explicitnoexcept

Definition at line 170 of file tagged_integer.h.

◆ operator<<

template<class Int, class Tag>
std::ostream & operator<< ( std::ostream & s,
TaggedInteger< Int, Tag > const & t )
friend

Definition at line 175 of file tagged_integer.h.

◆ operator>>

template<class Int, class Tag>
std::istream & operator>> ( std::istream & s,
TaggedInteger< Int, Tag > & t )
friend

Definition at line 183 of file tagged_integer.h.

◆ to_string

template<class Int, class Tag>
std::string to_string ( TaggedInteger< Int, Tag > const & t)
friend

Definition at line 190 of file tagged_integer.h.

Member Data Documentation

◆ value_

template<class Int, class Tag>
Int xrpl::TaggedInteger< Int, Tag >::value_
private

Definition at line 39 of file tagged_integer.h.