ActiveLib
Loading...
Searching...
No Matches
XMLPolygon.h
1
6#ifndef ACTIVE_SERIALISE_XML_XML_POLYGON
7#define ACTIVE_SERIALISE_XML_XML_POLYGON
8
9#include "Active/Geometry/Polygon.h"
10#include "Active/Serialise/Package/Package.h"
11#include "Active/Serialise/Item/Wrapper/ValueWrap.h"
12#include "Active/Serialise/XML/Package/Wrapper/Geometry/XMLPolyPoint.h"
13#include "Active/Utility/String.h"
14
15#include <concepts>
16
17namespace active::serialise::xml {
18
20 template<class Vert>
21 concept IsPolyPoint = std::is_base_of<geometry::PolyPoint, Vert>::value;
23 template<class VertWrap>
24 concept IsPointWrap = std::is_base_of<XMLPolyPoint, VertWrap>::value;
25
32 template<class Vert = geometry::PolyPoint, class VertWrap = XMLPolyPoint> requires IsPolyPoint<Vert> && IsPointWrap<VertWrap>
33 class XMLSolidPolygon : public Package, public std::reference_wrapper<geometry::Polygon> {
34 public:
35
36 // MARK: - Types
37
38 //Base class type
39 using base = std::reference_wrapper<geometry::Polygon>;
40
43 vertex,
44 topID,
45 };
46
47 // MARK: - Static variables
48
50 static inline utility::String tag = "polygon";
51
53 static inline std::array fieldID = {
54 Identity{"vertex"},
55 Identity{"topID"},
56 };
57
58 // MARK: - Constructor
59
64 //NB: Value is only mutated within import processes, in which case the object must be mutable (i.e. const discard is safe)
65 XMLSolidPolygon(const geometry::Polygon& polygon) : base(const_cast<geometry::Polygon&>(polygon)) { m_topID = polygon.getTopID(); }
71 //NB: Value is only mutated within import processes, in which case the object must be mutable (i.e. const discard is safe)
72 XMLSolidPolygon(const geometry::Polygon& polygon, const utility::String& customTag) : base(const_cast<geometry::Polygon&>(polygon)), m_tag(customTag) {
73 m_topID = polygon.getTopID();
74 }
75
76 // MARK: - Functions (const)
77
83 virtual bool fillInventory(Inventory& inventory) const override {
84 using enum Entry::Type;
85 inventory.merge(Inventory{
86 {
87 { fieldID[topID], topID, attribute, (m_topID != 0) },
88 { fieldID[vertex], vertex, static_cast<uint32_t>(get().size()), std::nullopt, (get().size() > 0) },
89 }
90 }.withType(&typeid(XMLSolidPolygon<Vert, VertWrap>)));
91 return true;
92 }
98 virtual Cargo::Unique getCargo(const Inventory::Item& item) const override {
99 if (item.ownerType != &typeid(XMLSolidPolygon<Vert>))
100 return nullptr;
101 switch (item.index) {
102 case FieldIndex::vertex:
103 return std::make_unique<VertWrap>((item.available < static_cast<uint32_t>(get().vertSize())) ?
104 *(get()[static_cast<geometry::vertex_index>(item.available)]) : m_vertex);
105 case FieldIndex::topID:
106 return std::make_unique<ValueWrap<geometry::vertex_id>>(m_topID);
107 default:
108 return nullptr; //Requested an unknown index
109 }
110 }
111
112 // MARK: - Functions (mutating)
113
117 virtual void setDefault() override {
118 base::get().clear(); //The default state is an empty polygon
119 }
124 virtual bool validate() override {
125 base::get().setTopID(m_topID);
126 return true;
127 }
134 virtual bool insert(Cargo::Unique&& cargo, const Inventory::Item& item) override {
135 if (item.ownerType != &typeid(XMLSolidPolygon<Vert, VertWrap>))
136 return true;
137 switch (item.index) {
138 case FieldIndex::vertex:
139 base::get().emplace_back(m_vertex);
140 break;
141 default:
142 break;
143 }
144 return true;
145 }
146
147 private:
151 mutable Vert m_vertex;
153 mutable geometry::vertex_id m_topID = 0;
154 };
155
156
158 template<class Hole>
159 concept IsPolygon = std::is_base_of<geometry::Polygon, Hole>::value;
161 template<class HoleWrap>
162 concept IsHoleWrap = std::is_base_of<XMLSolidPolygon<>, HoleWrap>::value;
163
164
171 template<class Vert = geometry::PolyPoint, class VertWrap = XMLPolyPoint,
172 class Hole = geometry::Polygon, class HoleWrap = XMLSolidPolygon<Vert, VertWrap>>
174 class XMLPolygon : public XMLSolidPolygon<Vert, VertWrap> {
175 public:
176
177 // MARK: - Types
178
179 //Base class type
181
184 hole,
185 };
186
187 // MARK: - Static variables
188
190 static inline utility::String tag = "polygon";
191
193 static inline std::array fieldID = {
194 Identity{"hole"},
195 };
196
197 // MARK: - Constructor
198
203 XMLPolygon(const geometry::Polygon& polygon) : base{polygon} {}
209 XMLPolygon(const geometry::Polygon& polygon, const utility::String& customTag) : base{polygon, customTag} {}
210
211 // MARK: - Functions (const)
212
219 virtual bool fillInventory(Inventory& inventory) const override {
220 base::fillInventory(inventory);
221 inventory.merge(Inventory{
222 {
223 { fieldID[hole], hole, static_cast<uint32_t>(base::get().getHoleSize()), std::nullopt, (base::get().getHoleSize() > 0) },
224 }
225 }.withType(&typeid(XMLPolygon<Vert, VertWrap, Hole, HoleWrap>)));
226 return true;
227 }
233 virtual Cargo::Unique getCargo(const Inventory::Item& item) const override {
234 if (item.ownerType != &typeid(XMLPolygon<Vert>))
235 return base::getCargo(item);
236 switch (item.index) {
237 case FieldIndex::hole:
238 return std::make_unique<HoleWrap>((item.available < static_cast<uint32_t>(base::get().getHoleSize())) ?
239 *(base::get().getHole(static_cast<geometry::part_index>(item.available))) : m_hole);
240 default:
241 return nullptr; //Requested an unknown index
242 }
243 }
244
245 // MARK: - Functions (mutating)
246
253 virtual bool insert(Cargo::Unique&& cargo, const Inventory::Item& item) override {
255 return base::insert(std::move(cargo), item);
256 switch (item.index) {
257 case FieldIndex::hole:
258 base::get().emplaceHole(clone(m_hole));
259 break;
260 default:
261 break;
262 }
263 return true;
264 }
265
266 private:
268 mutable Hole m_hole;
269 };
270
271}
272
273#endif //ACTIVE_SERIALISE_XML_XML_POLYGON
Definition Polygon.h:57
vertex_id getTopID() const
Definition Polygon.h:283
std::unique_ptr< Cargo > Unique
Unique pointer.
Definition Cargo.h:23
Definition Inventory.h:19
Inventory & merge(const Inventory &inventory)
Definition Inventory.cpp:66
Definition Package.h:17
Definition XMLPolygon.h:174
static utility::String tag
The default element tag.
Definition XMLPolygon.h:190
virtual Cargo::Unique getCargo(const Inventory::Item &item) const override
Definition XMLPolygon.h:233
virtual bool insert(Cargo::Unique &&cargo, const Inventory::Item &item) override
Definition XMLPolygon.h:253
static std::array fieldID
Serialisation field IDs.
Definition XMLPolygon.h:193
FieldIndex
The package fields.
Definition XMLPolygon.h:183
XMLPolygon(const geometry::Polygon &polygon)
Definition XMLPolygon.h:203
XMLPolygon(const geometry::Polygon &polygon, const utility::String &customTag)
Definition XMLPolygon.h:209
virtual bool fillInventory(Inventory &inventory) const override
Definition XMLPolygon.h:219
Definition XMLPolygon.h:33
static std::array fieldID
Serialisation field IDs.
Definition XMLPolygon.h:53
virtual bool fillInventory(Inventory &inventory) const override
Definition XMLPolygon.h:83
XMLSolidPolygon(const geometry::Polygon &polygon, const utility::String &customTag)
Definition XMLPolygon.h:72
virtual Cargo::Unique getCargo(const Inventory::Item &item) const override
Definition XMLPolygon.h:98
virtual void setDefault() override
Definition XMLPolygon.h:117
virtual bool validate() override
Definition XMLPolygon.h:124
virtual bool insert(Cargo::Unique &&cargo, const Inventory::Item &item) override
Definition XMLPolygon.h:134
static utility::String tag
The default element tag.
Definition XMLPolygon.h:50
FieldIndex
Serialisation fields.
Definition XMLPolygon.h:42
XMLSolidPolygon(const geometry::Polygon &polygon)
Definition XMLPolygon.h:65
A Unicode-aware string class.
Definition String.h:51
std::optional< String > Option
Optional.
Definition String.h:63
Concept for classes derived from XMLSolidPolygon.
Definition XMLPolygon.h:162
Concept for classes derived from XMLPolyPoint.
Definition XMLPolygon.h:24
Concept for classes derived from PolyPoint.
Definition XMLPolygon.h:21
Concept for classes derived from Polygon.
Definition XMLPolygon.h:159
int32_t vertex_index
Index of a vertex, e.g. within a polygon.
Definition Point.h:22
int32_t part_index
Index of a part, e.g. a hole within a polygon.
Definition Point.h:26
Definition XMLDateTime.h:12
Definition Entry.h:20
size_t available
How many are currently available (also acts as the requested item on import/export in 'getCargo')
Definition Entry.h:75
const std::type_info * ownerType
The type info of the owner package.
Definition Entry.h:73
int16_t index
The entry index.
Definition Entry.h:77
Definition Identity.h:18