20#include "comms/util/MaxSizeOf.h"
25#include "comms/details/tag.h"
27#include "CommonFuncs.h"
29COMMS_MSVC_WARNING_PUSH
30COMMS_MSVC_WARNING_DISABLE(4100)
44template <
typename TStorage>
45struct StringMaxLengthRetrieveHelper
47 static const std::size_t Value = CommonFuncs::maxSupportedLength();
50template <std::
size_t TSize>
51struct StringMaxLengthRetrieveHelper<
comms::util::StaticString<TSize> >
53 static const std::size_t Value = TSize - 1;
58template <
typename TFieldBase,
typename TStorage>
59class String :
public TFieldBase
61 using BaseImpl = TFieldBase;
63 using Endian =
typename BaseImpl::Endian;
65 using ValueType = TStorage;
66 using ElementType =
typename TStorage::value_type;
67 using CommsTag = comms::field::tag::String;
69 static_assert(std::is_integral<ElementType>::value,
"String of characters only supported");
70 static_assert(
sizeof(ElementType) ==
sizeof(
char),
"Single byte charactes only supported");
74 explicit String(
const ValueType& val)
79 explicit String(ValueType&& val)
80 : value_(
std::move(val))
84 String(
const String&) =
default;
85 String(String&&) =
default;
86 String& operator=(
const String&) =
default;
87 String& operator=(String&&) =
default;
88 ~String() noexcept = default;
90 const ValueType& value()
const
100 const ValueType& getValue()
const
105 template <
typename T>
106 void setValue(T&& val)
108 value() = std::forward<T>(val);
111 ValueType& createBack()
113 value_.push_back(ValueType());
114 return value_.back();
119 static_assert(comms::util::detect::hasClearFunc<ValueType>(),
120 "The string type must have clear() member function");
124 constexpr std::size_t length()
const
126 return value_.size() *
sizeof(ElementType);
129 static constexpr std::size_t minLength()
134 static constexpr std::size_t maxLength()
137 details::StringMaxLengthRetrieveHelper<TStorage>::Value *
141 static constexpr bool valid()
146 static constexpr std::size_t minElementLength()
148 return sizeof(ElementType);
151 static constexpr std::size_t maxElementLength()
153 return minElementLength();
156 static constexpr std::size_t elementLength(
const ElementType& elem)
158 return sizeof(
typename std::decay<
decltype(elem)>::type);
161 template <
typename TIter>
162 static ErrorStatus readElement(ElementType& elem, TIter& iter, std::size_t& len)
164 if (len <
sizeof(ElementType)) {
165 return ErrorStatus::NotEnoughData;
168 elem = comms::util::readData<ElementType>(iter,
Endian());
169 len -=
sizeof(ElementType);
170 return ErrorStatus::Success;
173 template <
typename TIter>
174 static void readElementNoStatus(ElementType& elem, TIter& iter)
176 elem = comms::util::readData<ElementType>(iter,
Endian());
179 template <
typename TIter>
182 using IterType =
typename std::decay<
decltype(iter)>::type;
184 typename std::iterator_traits<IterType>::iterator_category;
185 static_assert(std::is_base_of<std::random_access_iterator_tag, IterCategory>::value,
186 "Iterator for reading is expected to be random access one");
188 using ConstPointer =
typename ValueType::const_pointer;
189 auto* str =
reinterpret_cast<ConstPointer
>(&(*iter));
191 std::advance(endStr, std::min(len, comms::util::maxSizeOf(value_)));
193 std::advance(iter, len);
194 return ErrorStatus::Success;
197 static constexpr bool hasReadNoStatus()
202 template <
typename TIter>
203 void readNoStatus(TIter& iter) =
delete;
205 template <
typename TIter>
206 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len)
212 return read(iter, count);
215 template <
typename TIter>
216 void readNoStatusN(std::size_t count, TIter& iter)
221 template <
typename TIter>
222 static ErrorStatus writeElement(
const ElementType& elem, TIter& iter, std::size_t& len)
224 if (len <
sizeof(ElementType)) {
225 return ErrorStatus::BufferOverflow;
229 len -=
sizeof(ElementType);
230 return ErrorStatus::Success;
233 template <
typename TIter>
234 static void writeElementNoStatus(
const ElementType& elem, TIter& iter)
239 template <
typename TIter>
240 ErrorStatus write(TIter& iter, std::size_t len)
const
242 if (len < length()) {
250 template <
typename TIter>
251 void writeNoStatus(TIter& iter)
const
253 std::copy_n(value_.begin(), value_.size(), iter);
254 doAdvance(iter, value_.size());
257 template <
typename TIter>
258 ErrorStatus writeN(std::size_t count, TIter& iter, std::size_t& len)
const
260 count = std::min(count, value_.size());
266 writeNoStatusN(count, iter);
270 template <
typename TIter>
271 void writeNoStatusN(std::size_t count, TIter& iter)
const
273 count = std::min(count, value_.size());
274 std::copy_n(value_.begin(), count, iter);
275 doAdvance(iter, count);
279 template<
typename... TParams>
280 using AdvancableTag = comms::details::tag::Tag1<>;
282 template<
typename... TParams>
283 using NotAdvancableTag = comms::details::tag::Tag2<>;
285 template <
typename TIter>
286 static void doAdvance(TIter& iter, std::size_t len)
288 using IterType =
typename std::decay<
decltype(iter)>::type;
289 using IterCategory =
typename std::iterator_traits<IterType>::iterator_category;
290 static const bool InputIter =
291 std::is_base_of<std::input_iterator_tag, IterCategory>::value;
293 typename comms::util::LazyShallowConditional<
299 doAdvance(iter, len, Tag());
302 template <
typename TIter,
typename... TParams>
303 static void doAdvance(TIter& iter, std::size_t len, AdvancableTag<TParams...>)
305 std::advance(iter, len);
308 template <
typename TIter,
typename... TParams>
309 static void doAdvance(TIter&, std::size_t, NotAdvancableTag<TParams...>)
322COMMS_MSVC_WARNING_POP
This file contains classes required for generic custom assertion functionality.
Contains various compiler related definitions.
This file contain definition of error statuses used by comms module.
Contains comms::util::StaticString class.
Contains comms::util::StaticVector class.
Contains functions for raw data access / (de)serialization.
Provides helper assign() function to allow easy assignment of values to collections or views.
Contains definition of various tag classes.
comms::option::def::Endian< TEndian > Endian
Same as comms::option::def::Endian.
Definition options.h:1438
void writeData(T value, TIter &iter, const traits::endian::Big &endian)
Same as writeBig<T, TIter>()
Definition access.h:706
void assign(T &obj, TIter from, TIter to)
Assigns a new value to provided object.
Definition assign.h:39
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:17
@ Success
Used to indicate successful outcome of the operation.
Replacement to some types from standard type_traits.
Various compile-time detection functions of whether specific member functions and/or types exist.