File: AllOf.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (40 lines) | stat: -rw-r--r-- 1,038 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef RFL_ALLOF_HPP_
#define RFL_ALLOF_HPP_

#include <vector>

#include "Result.hpp"
#include "parsing/schema/ValidationType.hpp"

namespace rfl {

/// Requires that all of the contraints C and Cs be true.
template <class C, class... Cs>
struct AllOf {
  template <class T>
  static rfl::Result<T> validate(T _value) noexcept {
    return validate_impl<T, C, Cs...>(_value);
  }

  template <class T>
  static parsing::schema::ValidationType to_schema() {
    using ValidationType = parsing::schema::ValidationType;
    const auto types = std::vector<ValidationType>(
        {C::template to_schema<T>(), Cs::template to_schema<T>()...});
    return ValidationType{ValidationType::AllOf{.types_ = types}};
  }

 private:
  template <class T, class Head, class... Tail>
  static rfl::Result<T> validate_impl(T _value) noexcept {
    if constexpr (sizeof...(Tail) == 0) {
      return Head::validate(_value);
    } else {
      return Head::validate(_value).and_then(validate_impl<T, Tail...>);
    }
  }
};

}  // namespace rfl

#endif