MarlinMT  0.1.0
Validator.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std headers
4 #include <type_traits>
5 #include <limits>
6 #include <cmath>
7 #include <functional>
8 
9 namespace marlinmt {
10 
11  template <typename T>
12  using ValidatorFunctionT = std::function<bool(const T &)> ;
13 
14  namespace validator {
15 
20  template <typename T>
22  static_assert( std::numeric_limits<T>::has_infinity, "Infinity not defined for this type" ) ;
23  return [] (const T &value) {
24  return not std::isinf(value) ;
25  };
26  }
27 
34  template <typename T>
36  return [func=std::move(func)] (const std::vector<T> &value) {
37  return ( value.end() == std::find_if_not(value.begin(), value.end(), func ) ) ;
38  };
39  }
40 
47  template <typename T>
48  constexpr ValidatorFunctionT<T> greater( T limit ) {
49  return [limit=std::move(limit)] (const T &value) {
50  return value > limit ;
51  };
52  }
53 
60  template <typename T>
61  constexpr ValidatorFunctionT<T> greaterEqual( T limit ) {
62  return [limit=std::move(limit)] (const T &value) {
63  return value >= limit ;
64  };
65  }
66 
73  template <typename T>
74  constexpr ValidatorFunctionT<T> less( T limit ) {
75  return [limit=std::move(limit)] (const T &value) {
76  return value < limit ;
77  };
78  }
79 
86  template <typename T>
87  constexpr ValidatorFunctionT<T> lessEqual( T limit ) {
88  return [limit=std::move(limit)] (const T &value) {
89  return value <= limit ;
90  };
91  }
92 
101  template <typename T>
102  constexpr ValidatorFunctionT<T> inRange( T low, T high ) {
103  return [low=std::move(low),high=std::move(high)] (const T &value) {
104  return !(value < low) && !(high < value) ;
105  };
106  }
107 
115  template <typename T>
116  constexpr ValidatorFunctionT<T> sizeModulo( std::size_t mod ) {
117  return [mod] (const T &value) {
118  return (value.size() % mod) != 0 ;
119  };
120  }
121 
128  template <typename T>
129  constexpr inline ValidatorFunctionT<T> expectSize( std::size_t s ) {
130  return [s] (const T &value) {
131  return (value.size() == s) ;
132  };
133  }
134 
139  template <typename T>
140  constexpr inline ValidatorFunctionT<T> notEmpty() {
141  return [] (const T &value) {
142  return not value.empty() ;
143  };
144  }
145 
153  template <typename T>
155  return [lhs=std::move(lhs),rhs=std::move(rhs)]( const T &value ) {
156  return ( lhs(value) || rhs(value) ) ;
157  } ;
158  }
159 
167  template <typename T>
169  return [lhs=std::move(lhs),rhs=std::move(rhs)]( const T &value ) {
170  return ( lhs(value) && rhs(value) ) ;
171  } ;
172  }
173 
179  template <typename T>
180  constexpr ValidatorFunctionT<T> constraints( std::set<T> cnsts ) {
181  return [cnsts=std::move(cnsts)]( const T &value ) {
182  return cnsts.find( value ) != cnsts.end() ;
183  } ;
184  }
185 
186  }
187 
188 } // end namespace marlinmt
constexpr ValidatorFunctionT< std::vector< T > > forEach(ValidatorFunctionT< T > func)
Apply a validator on each element of the vector.
Definition: Validator.h:35
constexpr ValidatorFunctionT< T > expectSize(std::size_t s)
Validator function checking for the value to have an exact size.
Definition: Validator.h:129
constexpr ValidatorFunctionT< T > notEmpty()
Validator function checking for the value to be non-empty.
Definition: Validator.h:140
constexpr ValidatorFunctionT< T > less(T limit)
Validator function checking for the value to be less than a limit.
Definition: Validator.h:74
constexpr ValidatorFunctionT< T > greaterEqual(T limit)
Validator function checking for the value to be greater or equal to a limit.
Definition: Validator.h:61
constexpr ValidatorFunctionT< T > lessEqual(T limit)
Validator function checking for the value to be less or equal to a limit.
Definition: Validator.h:87
constexpr unsigned long long value(const Flag_t &flag)
Definition: Flags.h:106
constexpr ValidatorFunctionT< T > sizeModulo(std::size_t mod)
Validator function checking for the value to have a size multiple of the input value Only valid if th...
Definition: Validator.h:116
std::function< bool(const T &)> ValidatorFunctionT
Definition: Validator.h:12
constexpr ValidatorFunctionT< T > combineAnd(ValidatorFunctionT< T > lhs, ValidatorFunctionT< T > rhs)
Create a combination of two validator function with &#39;and&#39; combination.
Definition: Validator.h:168
constexpr ValidatorFunctionT< T > constraints(std::set< T > cnsts)
Create a validator checking for the value that is constrained to the input list.
Definition: Validator.h:180
constexpr ValidatorFunctionT< T > combineOr(ValidatorFunctionT< T > lhs, ValidatorFunctionT< T > rhs)
Create a combination of two validator function with &#39;or&#39; combination.
Definition: Validator.h:154
constexpr ValidatorFunctionT< T > greater(T limit)
Validator function checking for the value to be greater than a limit.
Definition: Validator.h:48
constexpr ValidatorFunctionT< T > inRange(T low, T high)
Validator function checking for the value to be within a specific range, boundaries included Only val...
Definition: Validator.h:102
constexpr ValidatorFunctionT< T > notInfinity()
Validator function checking for non-infinite values.
Definition: Validator.h:21