cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
VariantWrapper.h
1//
2// Copyright 2017 - 2024 (C). Alex Robenko. All rights reserved.
3//
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19#pragma once
20
21#include <cstdint>
22#include <cassert>
23#include <memory>
24#include <functional>
25
26#include "comms/field/Variant.h"
27#include "FieldWrapper.h"
28
29namespace cc_tools_qt
30{
31
32namespace field_wrapper
33{
34
35class CC_API VariantWrapper : public FieldWrapper
36{
37 using Base = FieldWrapper;
38public:
39 typedef std::unique_ptr<VariantWrapper> Ptr;
40
41 using MemberCreateCallbackFunc = std::function<FieldWrapperPtr ()>;
42
43 VariantWrapper();
44 VariantWrapper(const VariantWrapper&) =delete;
45 VariantWrapper& operator=(const VariantWrapper&) =delete;
46
47 virtual ~VariantWrapper() noexcept;
48
49 FieldWrapperPtr& getCurrent();
50
51 const FieldWrapperPtr& getCurrent() const;
52
53 void setCurrent(FieldWrapperPtr current);
54
55 void updateCurrent();
56
57 Ptr clone();
58
59 int getCurrentIndex() const;
60
61 void setCurrentIndex(int index);
62
63 int getMembersCount() const;
64
65 template <typename TFunc>
66 void setMemberCreateCallback(TFunc&& func)
67 {
68 m_createMemberCb = std::forward<TFunc>(func);
69 }
70
71protected:
72 virtual Ptr cloneImpl() = 0;
73
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;
78
79private:
80 FieldWrapperPtr m_current;
81 MemberCreateCallbackFunc m_createMemberCb;
82};
83
84template <typename TField>
85class VariantWrapperT : public FieldWrapperT<VariantWrapper, TField>
86{
87 using Base = FieldWrapperT<VariantWrapper, TField>;
88 using Field = TField;
89 static_assert(comms::field::isVariant<Field>(), "Must be of Variant field type");
90
91public:
92 typedef typename Base::Ptr Ptr;
93
94 explicit VariantWrapperT(Field& fieldRef)
95 : Base(fieldRef)
96 {
97 }
98
99 VariantWrapperT(const VariantWrapperT&) = default;
100 VariantWrapperT(VariantWrapperT&&) = default;
101 virtual ~VariantWrapperT() noexcept = default;
102
103 VariantWrapperT& operator=(const VariantWrapperT&) = delete;
104
105protected:
106 virtual Ptr cloneImpl() override
107 {
108 return Ptr(new VariantWrapperT(Base::field()));
109 }
110
111 virtual int getCurrentIndexImpl() const override
112 {
113 if (!Base::field().currentFieldValid()) {
114 return -1;
115 }
116
117 return static_cast<int>(Base::field().currentField());
118 }
119
120 virtual void setCurrentIndexImpl(int index) override
121 {
122 if (index < 0) {
123 Base::field().reset();
124 return;
125 }
126
127 Base::field().selectField(static_cast<std::size_t>(index));
128 }
129
130 virtual int getMembersCountImpl() const override
131 {
132 return
133 static_cast<int>(
134 std::tuple_size<typename Base::Field::Members>::value);
135 }
136
137};
138
139using VariantWrapperPtr = VariantWrapper::Ptr;
140
141template <typename TField>
142VariantWrapperPtr
143makeVariantWrapper(TField& field)
144{
145 return
146 VariantWrapperPtr(
147 new VariantWrapperT<TField>(field));
148}
149
150} // namespace field_wrapper
151
152} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.