26#include "comms/field/Variant.h"
27#include "FieldWrapper.h"
32namespace field_wrapper
35class CC_API VariantWrapper :
public FieldWrapper
37 using Base = FieldWrapper;
39 typedef std::unique_ptr<VariantWrapper> Ptr;
41 using MemberCreateCallbackFunc = std::function<FieldWrapperPtr ()>;
44 VariantWrapper(
const VariantWrapper&) =
delete;
45 VariantWrapper& operator=(
const VariantWrapper&) =
delete;
47 virtual ~VariantWrapper() noexcept;
49 FieldWrapperPtr& getCurrent();
51 const FieldWrapperPtr& getCurrent() const;
53 void setCurrent(FieldWrapperPtr current);
59 int getCurrentIndex() const;
61 void setCurrentIndex(
int index);
63 int getMembersCount() const;
65 template <typename TFunc>
66 void setMemberCreateCallback(TFunc&& func)
68 m_createMemberCb = std::forward<TFunc>(func);
72 virtual Ptr cloneImpl() = 0;
74 virtual void dispatchImpl(FieldWrapperHandler& handler);
75 virtual int getCurrentIndexImpl()
const = 0;
76 virtual void setCurrentIndexImpl(
int index) = 0;
77 virtual int getMembersCountImpl()
const = 0;
80 FieldWrapperPtr m_current;
81 MemberCreateCallbackFunc m_createMemberCb;
84template <
typename TField>
85class VariantWrapperT :
public FieldWrapperT<VariantWrapper, TField>
87 using Base = FieldWrapperT<VariantWrapper, TField>;
89 static_assert(comms::field::isVariant<Field>(),
"Must be of Variant field type");
92 typedef typename Base::Ptr Ptr;
94 explicit VariantWrapperT(Field& fieldRef)
99 VariantWrapperT(
const VariantWrapperT&) =
default;
100 VariantWrapperT(VariantWrapperT&&) =
default;
101 virtual ~VariantWrapperT() noexcept = default;
103 VariantWrapperT& operator=(const VariantWrapperT&) = delete;
106 virtual Ptr cloneImpl()
override
108 return Ptr(
new VariantWrapperT(Base::field()));
111 virtual int getCurrentIndexImpl()
const override
113 if (!Base::field().currentFieldValid()) {
117 return static_cast<int>(Base::field().currentField());
120 virtual void setCurrentIndexImpl(
int index)
override
123 Base::field().reset();
127 Base::field().selectField(
static_cast<std::size_t
>(index));
130 virtual int getMembersCountImpl()
const override
134 std::tuple_size<typename Base::Field::Members>::value);
139using VariantWrapperPtr = VariantWrapper::Ptr;
141template <
typename TField>
143makeVariantWrapper(TField& field)
147 new VariantWrapperT<TField>(field));