COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
NumValueMultiRangeValidator.h
1//
2// Copyright 2017 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#pragma once
11
12#include "comms/util/Tuple.h"
13
14#include <type_traits>
15#include <utility>
16
17namespace comms
18{
19
20namespace field
21{
22
23namespace adapter
24{
25
26template <typename TRanges, typename TBase>
27class NumValueMultiRangeValidator : public TBase
28{
29 using BaseImpl = TBase;
30
31 static_assert(comms::util::isTuple<TRanges>(), "TRanges must be a tuple");
32
33public:
34
35 using ValueType = typename BaseImpl::ValueType;
36
37 static_assert(
38 std::is_integral<ValueType>::value || std::is_enum<ValueType>::value || std::is_floating_point<ValueType>::value,
39 "Only numeric fields are supported for multi range validation.");
40
41 NumValueMultiRangeValidator() = default;
42
43 explicit NumValueMultiRangeValidator(const ValueType& val)
44 : BaseImpl(val)
45 {
46 }
47
48 explicit NumValueMultiRangeValidator(ValueType&& val)
49 : BaseImpl(std::move(val))
50 {
51 }
52
53 NumValueMultiRangeValidator(const NumValueMultiRangeValidator&) = default;
54 NumValueMultiRangeValidator(NumValueMultiRangeValidator&&) = default;
55 NumValueMultiRangeValidator& operator=(const NumValueMultiRangeValidator&) = default;
56 NumValueMultiRangeValidator& operator=(NumValueMultiRangeValidator&&) = default;
57
58 bool valid() const
59 {
60 return BaseImpl::valid() &&
61 comms::util::tupleTypeAccumulate<TRanges>(false, Validator(BaseImpl::getValue()));
62 }
63
64private:
65 class Validator
66 {
67 public:
68 Validator(ValueType val) : m_val(val) {}
69
70 template <typename TRange>
71 bool operator()(bool val) const
72 {
73 static_cast<void>(val);
74 static_assert(comms::util::isTuple<TRange>(), "TRange must be a tuple");
75 static_assert(std::tuple_size<TRange>::value == 2, "Tuple with 2 elements is expected");
76 using MinVal = typename std::tuple_element<0, TRange>::type;
77 using MaxVal = typename std::tuple_element<1, TRange>::type;
78 static_assert(MinVal::value <= MaxVal::value, "Invalid range");
79 return
80 val ||
81 ((static_cast<ValueType>(MinVal::value) <= m_val) &&
82 (m_val <= static_cast<ValueType>(MaxVal::value)));
83 }
84
85 private:
86 ValueType m_val;
87 };
88};
89
90} // namespace adapter
91
92} // namespace field
93
94} // namespace comms
95
Contains various tuple type manipulation classes and functions.
Main namespace for all classes / functions of COMMS library.
STL namespace.