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