Wrapper for strong type. More...
#include <TTL_strong_type.h>
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_StrongType & | operator+= (TTL_StrongType const &rhs) |
constexpr TTL_StrongType | operator+ (TTL_StrongType const &rhs) const |
TTL_StrongType & | operator++ () |
TTL_StrongType | operator++ (int) |
TTL_StrongType & | operator-- () |
TTL_StrongType | operator-- (int) |
constexpr TTL_StrongType & | operator-= (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) |
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;
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.
typedef T TTL_StrongType< T, UNIQUE_ENUM_CLASS >::TYPE |
Definition at line 54 of file TTL_strong_type.h.
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.
|
inlineconstexpr |
Definition at line 57 of file TTL_strong_type.h.
|
inlineconstexpr |
Definition at line 60 of file TTL_strong_type.h.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats != 2 Cats = true.
Definition at line 275 of file TTL_strong_type.h.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
inlineconstexpr |
It is acceptable to negate the underlying type
Definition at line 72 of file TTL_strong_type.h.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats < 2 Cats = false.
Definition at line 248 of file TTL_strong_type.h.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats <= 2 Cats = false.
Definition at line 257 of file TTL_strong_type.h.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats == 2 Cats = false.
Definition at line 266 of file TTL_strong_type.h.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats > 2 Cats = true.
Definition at line 230 of file TTL_strong_type.h.
|
inlineconstexpr |
It is generally acceptable to compare.
3 Cats >= 3 Cats = true.
Definition at line 239 of file TTL_strong_type.h.
|
inlineconstexpr |
Definition at line 63 of file TTL_strong_type.h.
|
friend |
Definition at line 285 of file TTL_strong_type.h.