cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
OptionalWrapper.h
1//
2// Copyright 2015 - 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 <memory>
22#include <cassert>
23
24#include "comms/field/Optional.h"
25#include "FieldWrapper.h"
26
27namespace cc_tools_qt
28{
29
30namespace field_wrapper
31{
32
33class CC_API OptionalWrapper : public FieldWrapper
34{
35public:
36 typedef std::unique_ptr<OptionalWrapper> Ptr;
37 typedef comms::field::OptionalMode Mode;
38
39 OptionalWrapper();
40 virtual ~OptionalWrapper() noexcept;
41
42 Mode getMode() const;
43
44 void setMode(Mode mode);
45
46 bool hasFieldWrapper() const;
47
48 FieldWrapper& getFieldWrapper();
49
50 const FieldWrapper& getFieldWrapper() const;
51
52 void setFieldWrapper(FieldWrapperPtr fieldWrapper);
53
54 Ptr clone();
55
56protected:
57 virtual Mode getModeImpl() const = 0;
58 virtual void setModeImpl(Mode mode) = 0;
59 virtual Ptr cloneImpl() = 0;
60
61 void dispatchImpl(FieldWrapperHandler& handler);
62
63private:
64 FieldWrapperPtr m_fieldWrapper;
65};
66
67template <typename TField>
68class OptionalWrapperT : public FieldWrapperT<OptionalWrapper, TField>
69{
70 typedef FieldWrapperT<OptionalWrapper, TField> Base;
71public:
72
73 typedef typename Base::Mode Mode;
74 typedef typename Base::Ptr Ptr;
75
76 typedef TField Field;
77 explicit OptionalWrapperT(Field& fieldRef)
78 : Base(fieldRef)
79 {
80 }
81
82protected:
83 virtual Mode getModeImpl() const override
84 {
85 return Base::field().getMode();
86 }
87
88 virtual void setModeImpl(Mode mode) override
89 {
90 Base::field().setMode(mode);
91 }
92
93 virtual Ptr cloneImpl() override
94 {
95 return Ptr(new OptionalWrapperT<TField>(Base::field()));
96 }
97};
98
99using OptionalWrapperPtr = OptionalWrapper::Ptr;
100
101template <typename TField>
102OptionalWrapperPtr
103makeOptionalWrapper(TField& field)
104{
105 return
106 OptionalWrapperPtr(
107 new OptionalWrapperT<TField>(field));
108}
109
110
111} // namespace field_wrapper
112
113} // namespace cc_tools_qt
114
115
Main namespace for all classes / functions of the shared library.