Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_StrongType< T, UNIQUE_ENUM_CLASS > Struct Template Reference

Wrapper for strong type. More...

#include <TTL_strong_type.h>

Collaboration diagram for TTL_StrongType< T, UNIQUE_ENUM_CLASS >:

Public Types

typedef T TYPE
 
using Type = T
 Allow compile time access to the underling type, this can be useful for template parameters etc.
 

Public Member Functions

constexpr TTL_StrongType ()
 
constexpr TTL_StrongType (T value)
 
constexpr T Underlying () const
 
constexpr TTL_StrongType operator- () const
 
constexpr TTL_StrongTypeoperator+= (TTL_StrongType const &rhs)
 
constexpr TTL_StrongType operator+ (TTL_StrongType const &rhs) const
 
TTL_StrongTypeoperator++ ()
 
TTL_StrongType operator++ (int)
 
TTL_StrongTypeoperator-- ()
 
TTL_StrongType operator-- (int)
 
constexpr TTL_StrongTypeoperator-= (TTL_StrongType const &rhs)
 
constexpr TTL_StrongType operator- (TTL_StrongType const &rhs) const
 
constexpr TTL_StrongType operator* (T rhs) const
 
constexpr TTL_StrongType operator/ (T rhs) const
 
constexpr T operator/ (TTL_StrongType rhs) const
 
constexpr TTL_StrongType operator% (TTL_StrongType rhs) const
 
constexpr TTL_StrongType operator% (T rhs) const
 
constexpr bool operator> (TTL_StrongType const &rhs) const
 
constexpr bool operator>= (TTL_StrongType const &rhs) const
 
constexpr bool operator< (TTL_StrongType const &rhs) const
 
constexpr bool operator<= (TTL_StrongType const &rhs) const
 
constexpr bool operator== (TTL_StrongType const &rhs) const
 
constexpr bool operator!= (TTL_StrongType const &rhs) const
 

Friends

template<typename U>
U & operator<< (U &os, const TTL_StrongType ttl_string_type)
 

Detailed Description

template<typename T, typename UNIQUE_ENUM_CLASS>
struct TTL_StrongType< T, UNIQUE_ENUM_CLASS >

Wrapper for strong type.

Strong types reinforce interfaces by making them more expressive, and less error-prone by forcing the right type. This is implemented by the following thin wrapper.

For example, In order to define a FrequencyKHz, uint16_t type:

enum class StKHz : uint32_t; // This just needs to be a unique. using KHz = TTL_StrongType<uint16_t, StKHz>;

StrongTypes can be used in anywhere because to the outside world it looks exactly like the underlying type.

So a structure from the outside world can contain the follow when a KHz frequency is expected as a uint16_t;

  • struct ExternalData { KHz input_frequency_khz; }

would be 100% compability with the other unsafe code in the external entity that simple said

struct ExternalData { uint16_t input_frequency_khz; }

Definition at line 53 of file TTL_strong_type.h.

Member Typedef Documentation

◆ TYPE

template<typename T, typename UNIQUE_ENUM_CLASS>
typedef T TTL_StrongType< T, UNIQUE_ENUM_CLASS >::TYPE

Definition at line 54 of file TTL_strong_type.h.

◆ Type

template<typename T, typename UNIQUE_ENUM_CLASS>
using TTL_StrongType< T, UNIQUE_ENUM_CLASS >::Type = T

Allow compile time access to the underling type, this can be useful for template parameters etc.

Definition at line 282 of file TTL_strong_type.h.

Constructor & Destructor Documentation

◆ TTL_StrongType() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType< T, UNIQUE_ENUM_CLASS >::TTL_StrongType ( )
inlineconstexpr

Definition at line 57 of file TTL_strong_type.h.

◆ TTL_StrongType() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType< T, UNIQUE_ENUM_CLASS >::TTL_StrongType ( T value)
inlineconstexpr

Definition at line 60 of file TTL_strong_type.h.

Member Function Documentation

◆ operator!=()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator!= ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats != 2 Cats = true.

Definition at line 275 of file TTL_strong_type.h.

◆ operator%() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator% ( T rhs) const
inlineconstexpr

It is generally acceptable to modulo by the underlying type. The underlying type tends to mean that sign rules are obeyed.

3 Cats % 2 = 1 Cats. -3 Cats % -2 = 1 Cats.

Definition at line 221 of file TTL_strong_type.h.

◆ operator%() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator% ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > rhs) const
inlineconstexpr

It is generally acceptable to modulo by the type.

3 Cats % 2 Cats = 1 Cats. -3 Cats % -2 = 1 Cats.

Definition at line 210 of file TTL_strong_type.h.

◆ operator*()

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator* ( T rhs) const
inlineconstexpr

It is generally acceptable to multiple by the underlying type. The underlying type tends to mean that sign rules are obeyed.

2 Cats * 2 = 4 Cats. -2 Cats * -2 = 4 Cats.

Definition at line 179 of file TTL_strong_type.h.

◆ operator+()

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator+ ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is acceptable to add 2 things of the same type. The rules of the underlying value are used for the addition.

2 Cats + 2 Cats = 4 Cats.

Definition at line 93 of file TTL_strong_type.h.

◆ operator++() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType & TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator++ ( )
inline

Prefix increment

It is acceptable to add another thing of the same type. The rules of the underlying value are used for the addition.

2 Cats + 1 Cats = 3 Cats.

Definition at line 105 of file TTL_strong_type.h.

◆ operator++() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator++ ( int )
inline

Postfix increment

It is acceptable to add another thing of the same type. The rules of the underlying value are used for the addition.

2 Cats + 1 Cats = 3 Cats.

Definition at line 118 of file TTL_strong_type.h.

◆ operator+=()

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType & TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator+= ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs)
inlineconstexpr

It is acceptable to add 2 things of the same type. The rules of the underlying value are used for the addition.

2 Cats + 2 Cats = 4 Cats.

Definition at line 82 of file TTL_strong_type.h.

◆ operator-() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator- ( ) const
inlineconstexpr

It is acceptable to negate the underlying type

  • -2 Cats = 2 Cats.

Definition at line 72 of file TTL_strong_type.h.

◆ operator-() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator- ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is acceptable to subtract 2 things of the same type. The rules of the underlying value are used for the addition.

2 Cats - 2 Cats = 0 Cats.

Definition at line 168 of file TTL_strong_type.h.

◆ operator--() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType & TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator-- ( )
inline

Prefix increment

It is acceptable to subtract another thing of the same type. The rules of the underlying value are used for the subtraction.

2 Cats - 1 Cats = 1 Cats.

Definition at line 132 of file TTL_strong_type.h.

◆ operator--() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator-- ( int )
inline

Postfix increment

It is acceptable to subtract another thing of the same type. The rules of the underlying value are used for the subtraction.

2 Cats - 1 Cats = 1 Cats.

Definition at line 145 of file TTL_strong_type.h.

◆ operator-=()

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType & TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator-= ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs)
inlineconstexpr

It is acceptable to subtract 2 things of the same type. The rules of the underlying value are used for the addition.

2 Cats - 2 Cats = 0 Cats.

Definition at line 157 of file TTL_strong_type.h.

◆ operator/() [1/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
TTL_StrongType TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator/ ( T rhs) const
inlineconstexpr

It is generally acceptable to divide by the underlying type. The underlying type tends to mean that sign rules are obeyed.

2 Cats / 2 = 1 Cats. -2 Cats / -2 = 1 Cats.

Definition at line 190 of file TTL_strong_type.h.

◆ operator/() [2/2]

template<typename T, typename UNIQUE_ENUM_CLASS>
T TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator/ ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > rhs) const
inlineconstexpr

It is generally acceptable to divide by the type.

2 Cats / 2 Cats = 1. -2 Cats / 2 Cats = -1.

Definition at line 200 of file TTL_strong_type.h.

◆ operator<()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator< ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats < 2 Cats = false.

Definition at line 248 of file TTL_strong_type.h.

◆ operator<=()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator<= ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats <= 2 Cats = false.

Definition at line 257 of file TTL_strong_type.h.

◆ operator==()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator== ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats == 2 Cats = false.

Definition at line 266 of file TTL_strong_type.h.

◆ operator>()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator> ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats > 2 Cats = true.

Definition at line 230 of file TTL_strong_type.h.

◆ operator>=()

template<typename T, typename UNIQUE_ENUM_CLASS>
bool TTL_StrongType< T, UNIQUE_ENUM_CLASS >::operator>= ( TTL_StrongType< T, UNIQUE_ENUM_CLASS > const & rhs) const
inlineconstexpr

It is generally acceptable to compare.

3 Cats >= 3 Cats = true.

Definition at line 239 of file TTL_strong_type.h.

◆ Underlying()

template<typename T, typename UNIQUE_ENUM_CLASS>
T TTL_StrongType< T, UNIQUE_ENUM_CLASS >::Underlying ( ) const
inlineconstexpr

Definition at line 63 of file TTL_strong_type.h.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T, typename UNIQUE_ENUM_CLASS>
template<typename U>
U & operator<< ( U & os,
const TTL_StrongType< T, UNIQUE_ENUM_CLASS > ttl_string_type )
friend

Definition at line 285 of file TTL_strong_type.h.


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