COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Field.h
Go to the documentation of this file.
1//
2// Copyright 2014 - 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
12
13#pragma once
14
15#include "comms/details/FieldBase.h"
16#include "comms/details/field_alias.h"
17#include "comms/details/fields_access.h"
18#include "comms/details/macro_common.h"
19#include "comms/util/access.h"
20
21#include <cstddef>
22#include <type_traits>
23
24namespace comms
25{
26
34template <typename... TOptions>
35class Field : public details::FieldBase<TOptions...>
36{
37 using BaseImpl = details::FieldBase<TOptions...>;
38public:
42 using Endian = typename BaseImpl::Endian;
43
45 using VersionType = typename BaseImpl::VersionType;
46
50 static constexpr bool valid()
51 {
52 return true;
53 }
54
59 static constexpr bool refresh()
60 {
61 return false;
62 }
63
66 static constexpr bool isVersionDependent()
67 {
68 return false;
69 }
70
73 static constexpr bool hasNonDefaultRefresh()
74 {
75 return false;
76 }
77
80 static constexpr bool hasReadNoStatus()
81 {
82 return true;
83 }
84
87 static constexpr bool hasWriteNoStatus()
88 {
89 return true;
90 }
91
95 static constexpr bool hasVarLength()
96 {
97 return false;
98 }
99
103 static constexpr bool canWrite()
104 {
105 return true;
106 }
107
112 static constexpr bool setVersion(VersionType)
113 {
114 return false;
115 }
116
119 static constexpr bool hasName()
120 {
121 return false;
122 }
123
124protected:
137 template <typename T, typename TIter>
138 static void writeData(T value, TIter& iter)
139 {
140 writeData<sizeof(T), T>(value, iter);
141 }
142
157 template <std::size_t TSize, typename T, typename TIter>
158 static void writeData(T value, TIter& iter)
159 {
160 static_assert(TSize <= sizeof(T),
161 "Cannot put more bytes than type contains");
162 return util::writeData<TSize, T>(value, iter, Endian());
163 }
164
178 template <typename T, typename TIter>
179 static T readData(TIter& iter)
180 {
181 return readData<T, sizeof(T)>(iter);
182 }
183
198 template <typename T, std::size_t TSize, typename TIter>
199 static T readData(TIter& iter)
200 {
201 static_assert(TSize <= sizeof(T),
202 "Cannot get more bytes than type contains");
203 return util::readData<T, TSize>(iter, Endian());
204 }
205
206};
207
246#define COMMS_FIELD_MEMBERS_ACCESS(...) \
247 COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
248 COMMS_FIELD_VALUE_ACCESS_FUNC { \
249 auto& val = comms::field::toFieldBase(*this).value(); \
250 using AllFieldsTuple = typename std::decay<decltype(val)>::type; \
251 static_assert(std::tuple_size<AllFieldsTuple>::value == FieldIdx_numOfValues, \
252 "Invalid number of names for fields tuple"); \
253 return val; \
254 } \
255 COMMS_FIELD_VALUE_ACCESS_CONST_FUNC { \
256 auto& val = comms::field::toFieldBase(*this).value(); \
257 using AllFieldsTuple = typename std::decay<decltype(val)>::type; \
258 static_assert(std::tuple_size<AllFieldsTuple>::value == FieldIdx_numOfValues, \
259 "Invalid number of names for fields tuple"); \
260 return val; \
261 } \
262 COMMS_EXPAND(COMMS_DO_FIELD_ACC_FUNC(ValueType, value(), __VA_ARGS__))
263
276#define COMMS_FIELD_MEMBERS_ACCESS_NOTEMPLATE(...) \
277 COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
278 COMMS_EXPAND(COMMS_DO_FIELD_ACC_FUNC_NOTEMPLATE(__VA_ARGS__))
279
390#define COMMS_FIELD_MEMBERS_NAMES(...) \
391 COMMS_EXPAND(COMMS_FIELD_MEMBERS_ACCESS(__VA_ARGS__)) \
392 COMMS_EXPAND(COMMS_DO_FIELD_TYPEDEF(typename Base::ValueType, Field_, FieldIdx_, __VA_ARGS__))
393
400#define COMMS_FIELD_ALIAS(f_, ...) COMMS_DO_ALIAS(field_, f_, __VA_ARGS__)
401
402} // namespace comms
403
Contains functions for raw data access / (de)serialization.
Base class to all the field classes.
Definition Field.h:36
static constexpr bool canWrite()
Default check of whether the field has a consistent value for writing.
Definition Field.h:103
static void writeData(T value, TIter &iter)
Write data into the output buffer.
Definition Field.h:138
static constexpr bool setVersion(VersionType)
Default version update functionality.
Definition Field.h:112
static constexpr bool isVersionDependent()
Default check of whether the field is version dependent.
Definition Field.h:66
static constexpr bool refresh()
Default refresh functionality.
Definition Field.h:59
static T readData(TIter &iter)
Read data from input buffer.
Definition Field.h:179
static constexpr bool hasReadNoStatus()
Default check of whether the field has readNoStatus() member function.
Definition Field.h:80
typename BaseImpl::Endian Endian
Endian type.
Definition Field.h:42
typename BaseImpl::VersionType VersionType
Version type.
Definition Field.h:45
static constexpr bool hasNonDefaultRefresh()
Default check of whether the field has defines refresh functionality.
Definition Field.h:73
static constexpr bool hasWriteNoStatus()
Default check of whether the field has writeNoStatus() member function.
Definition Field.h:87
static void writeData(T value, TIter &iter)
Write partial data into the output buffer.
Definition Field.h:158
static T readData(TIter &iter)
Read partial data from input buffer.
Definition Field.h:199
static constexpr bool valid()
Default validity check.
Definition Field.h:50
static constexpr bool hasVarLength()
Default check of whether the field has variable length definition via comms::option::def::VarLength o...
Definition Field.h:95
static constexpr bool hasName()
Check of whether the field class defines name() function.
Definition Field.h:119
Main namespace for all classes / functions of COMMS library.