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