1.10
serialize.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_SERIALIZE_H
7#define EGT_SERIALIZE_H
8
14#include <egt/detail/meta.h>
15#include <egt/widgetflags.h>
16#include <list>
17#include <memory>
18#include <ostream>
19#include <string>
20#include <utility>
21
22namespace egt
23{
24inline namespace v1
25{
26class Widget;
27class Image;
28class Pattern;
29
33class EGT_API Serializer
34{
35public:
36 using Context = void;
37
38 Serializer() noexcept = default;
39 Serializer(const Serializer&) = default;
40 Serializer& operator=(const Serializer&) = default;
41 Serializer(Serializer&&) noexcept = default;
42 Serializer& operator=(Serializer&&) noexcept = default;
43
45 using Attributes = std::list<std::pair<std::string, std::string>>;
46
47 using Properties = std::list<std::tuple<std::string, std::string, Serializer::Attributes>>;
48
50 virtual bool add(const Widget* widget) = 0;
51
53 virtual Context* begin_child(const std::string& nodename) = 0;
54
56 virtual void end_child(Context* context) = 0;
57
59 virtual void add_property(const std::string& name, const std::string& value,
60 const Attributes& attrs = {}) = 0;
62 void add_property(const std::string& name, const char* value,
63 const Attributes& attrs = {})
64 {
65 add_property(name, std::string(value), attrs);
66 }
67
69 void add_property(const std::string& name, int value,
70 const Attributes& attrs = {});
72 void add_property(const std::string& name, unsigned int value,
73 const Attributes& attrs = {});
75 void add_property(const std::string& name, const AlignFlags& value,
76 const Attributes& attrs = {});
78 void add_property(const std::string& name, float value,
79 const Attributes& attrs = {});
81 void add_property(const std::string& name, double value,
82 const Attributes& attrs = {});
84 virtual void add_property(const std::string& name, const Pattern& value,
85 const Attributes& attrs = {}) = 0;
86
88 void add_property(const std::string& name, bool value,
89 const Attributes& attrs = {});
90
92 virtual void write(std::ostream& out) = 0;
93
94 virtual ~Serializer() noexcept = default;
95};
96
109{
110public:
115 OstreamWidgetSerializer& operator=(OstreamWidgetSerializer&&) noexcept = default;
116
118 void reset();
119
120 bool add(const Widget* widget) override;
121
122 Context* begin_child(const std::string& nodename) override;
123
124 void end_child(Context* context) override;
125
126 using Serializer::add_property;
127
128 void add_property(const std::string& name, const std::string& value,
129 const Attributes& attrs = {}) override;
130
131 void add_property(const std::string& name, const Pattern& value,
132 const Attributes& attrs = {}) override;
133
134 void write(std::ostream& out) override;
135
136 ~OstreamWidgetSerializer() noexcept override;
137
138private:
139
140 struct OstreamSerializerImpl;
141 std::unique_ptr<OstreamSerializerImpl> m_impl;
142};
143
159class EGT_API XmlWidgetSerializer : public Serializer
160{
161public:
166 XmlWidgetSerializer& operator=(XmlWidgetSerializer&&) noexcept = default;
167
169 void reset();
170
171 bool add(const Widget* widget) override;
172
173 Context* begin_child(const std::string& nodename) override;
174
175 void end_child(Context* context) override;
176
177 using Serializer::add_property;
178
179 void add_property(const std::string& name, const std::string& value,
180 const Attributes& attrs = {}) override;
181
182 void add_property(const std::string& name, const Pattern& value,
183 const Attributes& attrs = {}) override;
184
186 void write(const std::string& filename);
187
189 void write(std::ostream& out) override;
190
191 ~XmlWidgetSerializer() noexcept override;
192
193private:
194
195 struct XmlSerializerImpl;
196 std::unique_ptr<XmlSerializerImpl> m_impl;
197};
198
199class EGT_API Deserializer
200{
201public:
202 virtual ~Deserializer() {}
203 virtual bool is_valid() const = 0;
204 virtual std::unique_ptr<Deserializer> first_child(const std::string& name = "") const = 0;
205 virtual std::unique_ptr<Deserializer> next_sibling(const std::string& name = "") const = 0;
206 virtual std::shared_ptr<Widget> parse_widget() const = 0;
207 virtual bool get_property(const std::string& name, std::string* value, Serializer::Attributes* attrs = nullptr) const = 0;
208};
209
210}
211}
212
213#endif
Alignment flags.
Definition widgetflags.h:379
Definition serialize.h:200
virtual std::unique_ptr< Deserializer > next_sibling(const std::string &name="") const =0
virtual std::unique_ptr< Deserializer > first_child(const std::string &name="") const =0
virtual bool is_valid() const =0
virtual std::shared_ptr< Widget > parse_widget() const =0
virtual bool get_property(const std::string &name, std::string *value, Serializer::Attributes *attrs=nullptr) const =0
virtual ~Deserializer()
Definition serialize.h:202
Serialize a widget tree to an std::ostream.
Definition serialize.h:109
OstreamWidgetSerializer & operator=(const OstreamWidgetSerializer &)=delete
~OstreamWidgetSerializer() noexcept override
void write(std::ostream &out) override
Write to the specified ostream.
OstreamWidgetSerializer(const OstreamWidgetSerializer &)=delete
OstreamWidgetSerializer(OstreamWidgetSerializer &&) noexcept=default
void add_property(const std::string &name, const Pattern &value, const Attributes &attrs={}) override
Add a property.
A Pattern which can store one or more colors at different offsets (steps) which can be used to create...
Definition pattern.h:55
Abstract base serializer class.
Definition serialize.h:34
virtual void add_property(const std::string &name, const Pattern &value, const Attributes &attrs={})=0
Add a property.
void add_property(const std::string &name, const AlignFlags &value, const Attributes &attrs={})
Add a property.
Serializer() noexcept=default
void add_property(const std::string &name, double value, const Attributes &attrs={})
Add a property.
virtual void write(std::ostream &out)=0
Write to the specified ostream.
void add_property(const std::string &name, float value, const Attributes &attrs={})
Add a property.
std::list< std::pair< std::string, std::string > > Attributes
Attributes array type.
Definition serialize.h:45
void add_property(const std::string &name, bool value, const Attributes &attrs={})
Add a property.
void add_property(const std::string &name, const char *value, const Attributes &attrs={})
Add a property.
Definition serialize.h:62
void add_property(const std::string &name, int value, const Attributes &attrs={})
Add a property.
void Context
Definition serialize.h:36
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
virtual ~Serializer() noexcept=default
void add_property(const std::string &name, unsigned int value, const Attributes &attrs={})
Add a property.
Base Widget class.
Definition widget.h:53
Serialize a widget tree to an XML document.
Definition serialize.h:160
~XmlWidgetSerializer() noexcept override
void write(const std::string &filename)
Write to the specified file path.
void write(std::ostream &out) override
Write to the specified ostream.
XmlWidgetSerializer & operator=(const XmlWidgetSerializer &)=delete
XmlWidgetSerializer(XmlWidgetSerializer &&) noexcept=default
XmlWidgetSerializer(const XmlWidgetSerializer &)=delete
void add_property(const std::string &name, const Pattern &value, const Attributes &attrs={}) override
Add a property.
EGT framework namespace.
Definition animation.h:24