12#include "comms/details/tag.h"
14#include "comms/field/basic/CommonFuncs.h"
19#include "comms/util/MaxSizeOf.h"
30COMMS_MSVC_WARNING_PUSH
31COMMS_MSVC_WARNING_DISABLE(4100)
45template <
typename TStorage>
46struct StringMaxLengthRetrieveHelper
48 static const std::size_t Value = CommonFuncs::maxSupportedLength();
51template <std::
size_t TSize>
52struct StringMaxLengthRetrieveHelper<
comms::util::StaticString<TSize> >
54 static const std::size_t Value = TSize - 1;
59template <
typename TFieldBase,
typename TStorage>
60class String :
public TFieldBase
62 using BaseImpl = TFieldBase;
64 using Endian =
typename BaseImpl::Endian;
66 using ValueType = TStorage;
67 using ElementType =
typename TStorage::value_type;
68 using CommsTag = comms::field::tag::String;
70 static_assert(std::is_integral<ElementType>::value,
"String of characters only supported");
71 static_assert(
sizeof(ElementType) ==
sizeof(
char),
"Single byte charactes only supported");
75 explicit String(
const ValueType& val)
80 explicit String(ValueType&& val)
81 : m_value(
std::move(val))
85 String(
const String&) =
default;
86 String(String&&) =
default;
87 String& operator=(
const String&) =
default;
88 String& operator=(String&&) =
default;
89 ~String() noexcept = default;
91 const ValueType& value()
const
101 const ValueType& getValue()
const
106 template <
typename T>
107 void setValue(T&& val)
109 value() = std::forward<T>(val);
112 ValueType& createBack()
114 m_value.push_back(ValueType());
115 return m_value.back();
120 static_assert(comms::util::detect::hasClearFunc<ValueType>(),
121 "The string type must have clear() member function");
125 constexpr std::size_t length()
const
127 return m_value.size() *
sizeof(ElementType);
130 static constexpr std::size_t minLength()
135 static constexpr std::size_t maxLength()
138 details::StringMaxLengthRetrieveHelper<TStorage>::Value *
142 static constexpr bool valid()
147 static constexpr std::size_t minElementLength()
149 return sizeof(ElementType);
152 static constexpr std::size_t maxElementLength()
154 return minElementLength();
157 static constexpr std::size_t elementLength(
const ElementType& elem)
159 return sizeof(
typename std::decay<
decltype(elem)>::type);
162 template <
typename TIter>
163 static ErrorStatus readElement(ElementType& elem, TIter& iter, std::size_t& len)
165 if (len <
sizeof(ElementType)) {
166 return ErrorStatus::NotEnoughData;
169 elem = comms::util::readData<ElementType>(iter,
Endian());
170 len -=
sizeof(ElementType);
171 return ErrorStatus::Success;
174 template <
typename TIter>
175 static void readElementNoStatus(ElementType& elem, TIter& iter)
177 elem = comms::util::readData<ElementType>(iter,
Endian());
180 template <
typename TIter>
183 using IterType =
typename std::decay<
decltype(iter)>::type;
185 typename std::iterator_traits<IterType>::iterator_category;
186 static_assert(std::is_base_of<std::random_access_iterator_tag, IterCategory>::value,
187 "Iterator for reading is expected to be random access one");
189 using ConstPointer =
typename ValueType::const_pointer;
190 auto* str =
reinterpret_cast<ConstPointer
>(&(*iter));
192 std::advance(endStr, std::min(len, comms::util::maxSizeOf(m_value)));
194 std::advance(iter, len);
195 return ErrorStatus::Success;
198 static constexpr bool hasReadNoStatus()
203 template <
typename TIter>
204 void readNoStatus(TIter& iter) =
delete;
206 template <
typename TIter>
207 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len)
213 return read(iter, count);
216 template <
typename TIter>
217 void readNoStatusN(std::size_t count, TIter& iter)
222 template <
typename TIter>
223 static ErrorStatus writeElement(
const ElementType& elem, TIter& iter, std::size_t& len)
225 if (len <
sizeof(ElementType)) {
226 return ErrorStatus::BufferOverflow;
230 len -=
sizeof(ElementType);
231 return ErrorStatus::Success;
234 template <
typename TIter>
235 static void writeElementNoStatus(
const ElementType& elem, TIter& iter)
240 template <
typename TIter>
241 ErrorStatus write(TIter& iter, std::size_t len)
const
243 if (len < length()) {
251 template <
typename TIter>
252 void writeNoStatus(TIter& iter)
const
254 std::copy_n(m_value.begin(), m_value.size(), iter);
255 doAdvance(iter, m_value.size());
258 template <
typename TIter>
259 ErrorStatus writeN(std::size_t count, TIter& iter, std::size_t& len)
const
261 count = std::min(count, m_value.size());
267 writeNoStatusN(count, iter);
271 template <
typename TIter>
272 void writeNoStatusN(std::size_t count, TIter& iter)
const
274 count = std::min(count, m_value.size());
275 std::copy_n(m_value.begin(), count, iter);
276 doAdvance(iter, count);
280 template<
typename... TParams>
281 using AdvancableTag = comms::details::tag::Tag1<>;
283 template<
typename... TParams>
284 using NotAdvancableTag = comms::details::tag::Tag2<>;
286 template <
typename TIter>
287 static void doAdvance(TIter& iter, std::size_t len)
289 using IterType =
typename std::decay<
decltype(iter)>::type;
290 using IterCategory =
typename std::iterator_traits<IterType>::iterator_category;
291 static const bool InputIter =
292 std::is_base_of<std::input_iterator_tag, IterCategory>::value;
294 typename comms::util::LazyShallowConditional<
300 doAdvance(iter, len, Tag());
303 template <
typename TIter,
typename... TParams>
304 static void doAdvance(TIter& iter, std::size_t len, AdvancableTag<TParams...>)
306 std::advance(iter, len);
309 template <
typename TIter,
typename... TParams>
310 static void doAdvance(TIter&, std::size_t, NotAdvancableTag<TParams...>)
323COMMS_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:1460
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.