23#include "comms/comms.h"
25#include "cc_tools_qt/field_wrapper/IntValueWrapper.h"
26#include "cc_tools_qt/field_wrapper/UnsignedLongValueWrapper.h"
27#include "cc_tools_qt/field_wrapper/BitmaskValueWrapper.h"
28#include "cc_tools_qt/field_wrapper/EnumValueWrapper.h"
29#include "cc_tools_qt/field_wrapper/StringWrapper.h"
30#include "cc_tools_qt/field_wrapper/BitfieldWrapper.h"
31#include "cc_tools_qt/field_wrapper/OptionalWrapper.h"
32#include "cc_tools_qt/field_wrapper/BundleWrapper.h"
33#include "cc_tools_qt/field_wrapper/ArrayListRawDataWrapper.h"
34#include "cc_tools_qt/field_wrapper/ArrayListWrapper.h"
35#include "cc_tools_qt/field_wrapper/FloatValueWrapper.h"
36#include "cc_tools_qt/field_wrapper/VariantWrapper.h"
37#include "cc_tools_qt/field_wrapper/UnknownValueWrapper.h"
38#include "ret_unique_ptr.h"
46class FieldWrapperCreator
49 typedef cc_tools_qt::field_wrapper::FieldWrapperPtr FieldWrapperPtr;
51 template <
typename TField>
52 static FieldWrapperPtr createWrapper(TField& field)
54 typedef typename std::decay<
decltype(field)>::type DecayedField;
55 typedef typename DecayedField::CommsTag Tag;
56 return createWrapperInternal(field, Tag());
59 typedef comms::field::tag::Int IntValueTag;
60 typedef comms::field::tag::Bitmask BitmaskValueTag;
61 typedef comms::field::tag::Enum EnumValueTag;
62 typedef comms::field::tag::String StringTag;
63 typedef comms::field::tag::Bitfield BitfieldTag;
64 typedef comms::field::tag::Optional OptionalTag;
65 typedef comms::field::tag::Bundle BundleTag;
66 typedef comms::field::tag::RawArrayList RawDataArrayListTag;
67 typedef comms::field::tag::ArrayList FieldsArrayListTag;
68 typedef comms::field::tag::Float FloatValueTag;
69 typedef comms::field::tag::Variant VariantTag;
71 struct RegularIntTag {};
72 struct BigUnsignedTag {};
74 class SubfieldsCreateHelper
77 typedef std::function <void (FieldWrapperPtr)> WrapperDispatchFunc;
78 SubfieldsCreateHelper(WrapperDispatchFunc&& dispatchOp)
79 : m_dispatchOp(std::move(dispatchOp))
83 template <
typename TField>
84 void operator()(TField&& field)
87 FieldWrapperCreator::createWrapper(std::forward<TField>(field));
88 m_dispatchOp(std::move(fieldWidget));
91 template <std::
size_t TIdx,
typename TField>
92 void operator()(TField&& field)
94 return operator()(field);
98 WrapperDispatchFunc m_dispatchOp;
102 template <
typename TField>
103 static FieldWrapperPtr createWrapperInternal(TField& field, IntValueTag)
105 typedef typename std::decay<
decltype(field)>::type FieldType;
106 typedef typename FieldType::ValueType ValueType;
108 static_assert(std::is_integral<ValueType>::value,
109 "ValueType is expected to be integral");
111 using Tag =
typename std::conditional<
112 std::is_signed<ValueType>::value || (
sizeof(ValueType) <
sizeof(std::uint64_t)),
117 return createWrapperInternal(field, Tag());
120 template <
typename TField>
121 static FieldWrapperPtr createWrapperInternal(TField& field, RegularIntTag)
123 return field_wrapper::makeIntValueWrapper(field);
126 template <
typename TField>
127 static FieldWrapperPtr createWrapperInternal(TField& field, BigUnsignedTag)
129 return field_wrapper::makeUnsignedLongValueWrapper(field);
132 template <
typename TField>
133 static FieldWrapperPtr createWrapperInternal(TField& field, BitmaskValueTag)
135 return field_wrapper::makeBitmaskValueWrapper(field);
138 template <
typename TField>
139 static FieldWrapperPtr createWrapperInternal(TField& field, EnumValueTag)
141 return field_wrapper::makeEnumValueWrapper(field);
144 template <
typename TField>
145 static FieldWrapperPtr createWrapperInternal(TField& field, StringTag)
147 return field_wrapper::makeStringWrapper(field);
150 template <
typename TField>
151 static FieldWrapperPtr createWrapperInternal(TField& field, BitfieldTag)
153 auto wrapper = field_wrapper::makeBitfieldWrapper(field);
155 typedef typename std::decay<
decltype(wrapper)>::type WrapperPtrType;
156 typedef typename WrapperPtrType::element_type WrapperType;
157 typedef typename WrapperType::Members MembersWrappersList;
159 MembersWrappersList subWrappers;
160 auto& memberFields = field.value();
161 comms::util::tupleForEach(
163 SubfieldsCreateHelper(
164 [&subWrappers](FieldWrapperPtr fieldWrapper)
166 subWrappers.push_back(std::move(fieldWrapper));
169 wrapper->setMembers(std::move(subWrappers));
170 return CC_RET_UNIQUE_PTR(wrapper);
173 template <
typename TField>
174 static FieldWrapperPtr createWrapperInternal(TField& field, OptionalTag)
176 auto wrapper = field_wrapper::makeOptionalWrapper(field);
177 auto& wrappedField = field.field();
178 auto fieldWrapper = createWrapper(wrappedField);
179 wrapper->setFieldWrapper(std::move(fieldWrapper));
180 return CC_RET_UNIQUE_PTR(wrapper);
183 template <
typename TField>
184 static FieldWrapperPtr createWrapperInternal(TField& field, BundleTag)
186 auto wrapper = field_wrapper::makeBundleWrapper(field);
188 typedef typename std::decay<
decltype(wrapper)>::type WrapperPtrType;
189 typedef typename WrapperPtrType::element_type WrapperType;
190 typedef typename WrapperType::Members MembersWrappersList;
192 MembersWrappersList subWrappers;
193 auto& memberFields = field.value();
194 comms::util::tupleForEach(
196 SubfieldsCreateHelper(
197 [&subWrappers](FieldWrapperPtr fieldWrapper)
199 subWrappers.push_back(std::move(fieldWrapper));
202 wrapper->setMembers(std::move(subWrappers));
203 return CC_RET_UNIQUE_PTR(wrapper);
206 template <
typename TField>
207 static FieldWrapperPtr createWrapperInternal(TField& field, RawDataArrayListTag)
209 return field_wrapper::makeArrayListRawDataWrapper(field);
212 template <
typename TField>
213 static FieldWrapperPtr createWrapperInternal(TField& field, FieldsArrayListTag)
215 typedef typename std::decay<
decltype(field)>::type DecayedField;
216 typedef typename DecayedField::ValueType CollectionType;
217 typedef typename CollectionType::value_type ElementType;
219 auto wrapper = field_wrapper::makeDowncastedArrayListWrapper(field);
220 if (wrapper->hasFixedSize()) {
221 wrapper->adjustFixedSize();
224 wrapper->setWrapFieldCallback(
225 [](ElementType& memField) -> FieldWrapperPtr
227 return FieldWrapperCreator::createWrapper(memField);
230 wrapper->refreshMembers();
231 return CC_RET_UNIQUE_PTR(wrapper);
234 template <
typename TField>
235 static FieldWrapperPtr createWrapperInternal(TField& field, FloatValueTag)
237 return field_wrapper::makeFloatValueWrapper(field);
240 template <
typename TField>
241 static FieldWrapperPtr createWrapperInternal(TField& field, VariantTag)
243 auto wrapper = field_wrapper::makeVariantWrapper(field);
245 wrapper->setMemberCreateCallback(
246 [&field]() -> FieldWrapperPtr
249 if (field.currentFieldValid()) {
250 field.currentFieldExec(
251 SubfieldsCreateHelper(
252 [&ptr](FieldWrapperPtr fieldWrapper) noexcept
254 ptr = std::move(fieldWrapper);
260 if (field.currentFieldValid()) {
261 field.currentFieldExec(
262 SubfieldsCreateHelper(
263 [&wrapper](FieldWrapperPtr fieldWrapper)
265 wrapper->setCurrent(std::move(fieldWrapper));
269 wrapper->setCurrent(FieldWrapperPtr());
272 return CC_RET_UNIQUE_PTR(wrapper);
275 template <
typename TField,
typename TTag>
276 static FieldWrapperPtr createWrapperInternal(TField& field, TTag)
278 return field_wrapper::makeUnknownValueWrapper(field);