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