14#include "comms/details/tag.h"
16#include "comms/field/basic/CommonFuncs.h"
21#include "comms/util/MaxSizeOf.h"
32COMMS_MSVC_WARNING_PUSH
33COMMS_MSVC_WARNING_DISABLE(4100)
47template <
typename TStorage>
48struct StringMaxLengthRetrieveHelper
50 static const std::size_t Value = CommonFuncs::maxSupportedLength();
53template <std::
size_t TSize>
54struct StringMaxLengthRetrieveHelper<
comms::util::StaticString<TSize> >
56 static const std::size_t Value = TSize - 1;
61template <
typename TFieldBase,
typename TStorage>
62class String :
public TFieldBase
64 using BaseImpl = TFieldBase;
66 using Endian =
typename BaseImpl::Endian;
68 using ValueType = TStorage;
69 using ElementType =
typename TStorage::value_type;
70 using CommsTag = comms::field::tag::String;
72 static_assert(std::is_integral<ElementType>::value,
"String of characters only supported");
73 static_assert(
sizeof(ElementType) ==
sizeof(
char),
"Single byte charactes only supported");
77 explicit String(
const ValueType& val)
82 explicit String(ValueType&& val)
83 : m_value(
std::move(val))
87 String(
const String&) =
default;
88 String(String&&) =
default;
89 String& operator=(
const String&) =
default;
90 String& operator=(String&&) =
default;
91 ~String() noexcept = default;
93 const ValueType& value()
const
103 const ValueType& getValue()
const
108 template <
typename T>
109 void setValue(T&& val)
111 value() = std::forward<T>(val);
114 ValueType& createBack()
116 m_value.push_back(ValueType());
117 return m_value.back();
122 static_assert(comms::util::detect::hasClearFunc<ValueType>(),
123 "The string type must have clear() member function");
127 constexpr std::size_t length()
const
129 return m_value.size() *
sizeof(ElementType);
132 static constexpr std::size_t minLength()
137 static constexpr std::size_t maxLength()
140 details::StringMaxLengthRetrieveHelper<TStorage>::Value *
144 static constexpr bool valid()
149 static constexpr std::size_t minElementLength()
151 return sizeof(ElementType);
154 static constexpr std::size_t maxElementLength()
156 return minElementLength();
159 static constexpr std::size_t elementLength(
const ElementType& elem)
161 return sizeof(
typename std::decay<
decltype(elem)>::type);
164 template <
typename TIter>
165 static ErrorStatus readElement(ElementType& elem, TIter& iter, std::size_t& len)
167 if (len <
sizeof(ElementType)) {
168 return ErrorStatus::NotEnoughData;
171 elem = comms::util::readData<ElementType>(iter,
Endian());
172 len -=
sizeof(ElementType);
173 return ErrorStatus::Success;
176 template <
typename TIter>
177 static void readElementNoStatus(ElementType& elem, TIter& iter)
179 elem = comms::util::readData<ElementType>(iter,
Endian());
182 template <
typename TIter>
185 using IterType =
typename std::decay<
decltype(iter)>::type;
187 typename std::iterator_traits<IterType>::iterator_category;
188 static_assert(std::is_base_of<std::random_access_iterator_tag, IterCategory>::value,
189 "Iterator for reading is expected to be random access one");
191 using ConstPointer =
typename ValueType::const_pointer;
192 auto* str =
reinterpret_cast<ConstPointer
>(&(*iter));
194 std::advance(endStr, std::min(len, comms::util::maxSizeOf(m_value)));
196 std::advance(iter, len);
197 return ErrorStatus::Success;
200 static constexpr bool hasReadNoStatus()
205 template <
typename TIter>
206 void readNoStatus(TIter& iter) =
delete;
208 template <
typename TIter>
209 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len)
215 return read(iter, count);
218 template <
typename TIter>
219 void readNoStatusN(std::size_t count, TIter& iter)
224 template <
typename TIter>
225 static ErrorStatus writeElement(
const ElementType& elem, TIter& iter, std::size_t& len)
227 if (len <
sizeof(ElementType)) {
228 return ErrorStatus::BufferOverflow;
232 len -=
sizeof(ElementType);
233 return ErrorStatus::Success;
236 template <
typename TIter>
237 static void writeElementNoStatus(
const ElementType& elem, TIter& iter)
242 template <
typename TIter>
243 ErrorStatus write(TIter& iter, std::size_t len)
const
245 if (len < length()) {
253 template <
typename TIter>
254 void writeNoStatus(TIter& iter)
const
256 std::copy_n(m_value.begin(), m_value.size(), iter);
257 doAdvance(iter, m_value.size());
260 template <
typename TIter>
261 ErrorStatus writeN(std::size_t count, TIter& iter, std::size_t& len)
const
263 count = std::min(count, m_value.size());
269 writeNoStatusN(count, iter);
273 template <
typename TIter>
274 void writeNoStatusN(std::size_t count, TIter& iter)
const
276 count = std::min(count, m_value.size());
277 std::copy_n(m_value.begin(), count, iter);
278 doAdvance(iter, count);
282 template<
typename... TParams>
283 using AdvancableTag = comms::details::tag::Tag1<>;
285 template<
typename... TParams>
286 using NotAdvancableTag = comms::details::tag::Tag2<>;
288 template <
typename TIter>
289 static void doAdvance(TIter& iter, std::size_t len)
291 using IterType =
typename std::decay<
decltype(iter)>::type;
292 using IterCategory =
typename std::iterator_traits<IterType>::iterator_category;
293 static const bool InputIter =
294 std::is_base_of<std::input_iterator_tag, IterCategory>::value;
296 typename comms::util::LazyShallowConditional<
302 doAdvance(iter, len, Tag());
305 template <
typename TIter,
typename... TParams>
306 static void doAdvance(TIter& iter, std::size_t len, AdvancableTag<TParams...>)
308 std::advance(iter, len);
311 template <
typename TIter,
typename... TParams>
312 static void doAdvance(TIter&, std::size_t, NotAdvancableTag<TParams...>)
325COMMS_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:1473
void writeData(T value, TIter &iter, const traits::endian::Big &endian)
Same as writeBig<T, TIter>()
Definition access.h:707
void assign(T &obj, TIter from, TIter to)
Assigns a new value to provided object.
Definition assign.h:41
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
@ 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.