COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
ExistsBetweenVersions.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/Assert.h"
14#include "comms/details/tag.h"
17
18#include <algorithm>
19#include <limits>
20#include <utility>
21
22COMMS_GNU_WARNING_PUSH
23COMMS_GNU_WARNING_DISABLE("-Wtype-limits")
24
25namespace comms
26{
27
28namespace field
29{
30
31namespace adapter
32{
33
34template <std::uintmax_t TFrom, std::uintmax_t TUntil, typename TBase>
35class ExistsBetweenVersions : public TBase
36{
37 using BaseImpl = TBase;
38 static_assert(TFrom <= TUntil, "Invalid parameters");
39public:
40
41 using ValueType = typename BaseImpl::ValueType;
42 using VersionType = typename BaseImpl::VersionType;
43
44 ExistsBetweenVersions() = default;
45
46 explicit ExistsBetweenVersions(const ValueType& val)
47 : BaseImpl(val)
48 {
49 }
50
51 explicit ExistsBetweenVersions(ValueType&& val)
52 : BaseImpl(std::move(val))
53 {
54 }
55
56 ExistsBetweenVersions(const ExistsBetweenVersions&) = default;
57 ExistsBetweenVersions(ExistsBetweenVersions&&) = default;
58 ExistsBetweenVersions& operator=(const ExistsBetweenVersions&) = default;
59 ExistsBetweenVersions& operator=(ExistsBetweenVersions&&) = default;
60
61 static constexpr bool isVersionDependent()
62 {
63 return true;
64 }
65
66 bool setVersion(VersionType version)
67 {
68 bool updated = BaseImpl::setVersion(version);
70 if (aboveFrom(version) && belowUntil(version)) {
72 }
73
74 if (mode == BaseImpl::getMode()) {
75 return updated;
76 }
77
78 BaseImpl::setMode(mode);
79 return true;
80 }
81
82private:
83 template <typename... TParams>
84 using AlwaysTrueTag = comms::details::tag::Tag1<>;
85 template <typename... TParams>
86 using CompareTag = comms::details::tag::Tag2<>;
87
88 static bool aboveFrom(VersionType version)
89 {
90 using Tag =
91 typename comms::util::LazyShallowConditional<
92 TFrom == 0
93 >::template Type<
94 AlwaysTrueTag,
95 CompareTag
96 >;
97 return aboveFrom(version, Tag());
98 }
99
100 template <typename... TParams>
101 static constexpr bool aboveFrom(VersionType, AlwaysTrueTag<TParams...>)
102 {
103 return true;
104 }
105
106 template <typename... TParams>
107 static bool aboveFrom(VersionType version, CompareTag<TParams...>)
108 {
109 static const VersionType MinVersion =
110 static_cast<VersionType>(
111 std::min(
112 static_cast<decltype(TFrom)>(std::numeric_limits<VersionType>::max()),
113 TFrom));
114
115 return MinVersion <= version;
116 }
117
118 static bool belowUntil(VersionType version)
119 {
120 using Tag =
121 typename comms::util::LazyShallowConditional<
122 static_cast<decltype(TUntil)>(std::numeric_limits<VersionType>::max()) <= TUntil
123 >::template Type<
124 AlwaysTrueTag,
125 CompareTag
126 >;
127 return belowUntil(version, Tag());
128 }
129
130 template <typename... TParams>
131 static constexpr bool belowUntil(VersionType, AlwaysTrueTag<TParams...>)
132 {
133 return true;
134 }
135
136 template <typename... TParams>
137 static bool belowUntil(VersionType version, CompareTag<TParams...>)
138 {
139 static const VersionType MaxVersion =
140 static_cast<VersionType>(
141 std::min(
142 static_cast<decltype(TUntil)>(std::numeric_limits<VersionType>::max()),
143 TUntil));
144
145 return version <= MaxVersion;
146 }
147};
148
149} // namespace adapter
150
151} // namespace field
152
153} // namespace comms
154
155COMMS_GNU_WARNING_POP
This file contains classes required for generic custom assertion functionality.
Contains various compiler related definitions.
Contains definition of the mode used for comms::field::Optional fields.
@ Exists
Field must exist.
@ Missing
Field doesn't exist.
comms::option::def::VersionType< T > VersionType
Same as comms::option::def::VersionType.
Definition options.h:1930
comms::option::def::ExistsBetweenVersions< TFrom, TUntil > ExistsBetweenVersions
Same as comms::option::def::ExistsBetweenVersions.
Definition options.h:1939
Main namespace for all classes / functions of COMMS library.
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:66
STL namespace.
Replacement to some types from standard type_traits.