A library for type level flagging.
Flagged<T>
offers multiple wrapper types which allow you to add properties to your variables at type level. The wrapped types can still be used as the inner type, thanks to operator overloading.
These wrappers can only be built from values which pass some test (e.g. Positive
), or will alter the passed value (e.g. Sorted
). This makes it possible to write functions with less or no exceptions, since the caller can now be forced to supply correct input.
Example showing Sorted<T>
//creating any Sorted<T> will directly sort its data and keep it that way
auto alwaysSorted = Sorted<std::vector<int>>(std::vector<int>({4,9,2,13,15,17}));
//alwaysSorted.get() == {2,4,9,13,15,17}
int smallest(Sorted<std::vector<int>> const& sorted) {
//no need to check whether the input is already sorted
//or even sort it just to retrieve the smallest value
return sorted.get()[0]; //this might fail if sorted is empty, see NonEmpty
}
Other types defined within FlaggedT
Immutable<T> //ensures T is never changed after construction
shared_im<T> //shared_ptr version of Immutable<T>
NonNull<T> //ensures T is never null
Sorted<T> //ensures that T is always sorted
Unique<T> //ensures T is unique
UniqueAndSorted<T> // ensures T is unique and sorted
Shuffled<T> //ensures T is shuffled
NonZero<T> //ensures T != 0
Positive<T> //ensures T > 0
NonPositive<T> //ensures T <= 0
Negative<T> //ensures T < 0
NonNegative<T> //ensures T >= 0
CeiledInclusive<T, T MAX> //ensures T <= MAX
CeiledExclusive<T, T MAX> //ensures T < MAX
FlooredInclusive<T, T MIN> //ensures T >= MIN
FlooredExclusive<T, T MIN> //ensures T > MIN
BoundedInclusive<T, T MIN, T MAX> // ensures T >= MIN && T <= MAX
BoundedExclusive<T, T MIN, T MAX> //ensures T > MIN && T < MAX
NonEmpty<T> //ensures that T contains at least 1 element
MoreThan<T,SIZE> //ensures that T contains more than SIZE elements
LessThan<T,SIZE> //ensures that T contains less than SIZE elements
FixedSized<T,SIZE> //ensures that T has SIZE elements
FixedRangeInclusive<T,MINSIZE,MAXSIZE> //ensures T has >= MINSIZE and <= MAXSIZE elements