ActiveLib
Loading...
Searching...
No Matches
Entry.h
1
6#ifndef ACTIVE_SERIALISE_ENTRY
7#define ACTIVE_SERIALISE_ENTRY
8
9#include "Active/Serialise/Inventory/Identity.h"
10
11#include <cstdint>
12#include <optional>
13#include <typeindex>
14
15namespace active::serialise {
16
20 struct Entry {
21
22 enum class Type {
23 attribute,
24 element,
25 };
26
27 using enum Type;
28
29 class ID {
30 public:
31 ID(Identity&& identity) : m_identity{std::move(identity)} {}
32 ID(const Identity& identity) : m_wrapped{std::reference_wrapper<const Identity>{identity}} {}
33
35 const Identity& identity() const { return m_wrapped ? m_wrapped->get() : m_identity; }
36
37 private:
38 Identity m_identity;
39 std::optional<std::reference_wrapper<const Identity>> m_wrapped;
40 };
41
42 // MARK: - Constructors
43
52 Entry(const Identity& ident, int16_t ind, Type type, bool mustHave = true, const std::type_info* owner = nullptr) :
53 Entry{ID{ident}, ind, type, mustHave, owner} {}
54 Entry(Identity&& ident, int16_t ind, Type type, bool mustHave = true, const std::type_info* owner = nullptr) :
55 Entry{ID{std::move(ident)}, ind, type, mustHave, owner} {}
64 Entry(const Identity& ident, int16_t ind, size_t howMany, std::optional<size_t> max = 1, bool mustHave = true, const std::type_info* owner = nullptr) :
65 Entry{ID{ident}, ind, howMany, max, mustHave, owner} {}
66 Entry(Identity&& ident, int16_t ind, size_t howMany, std::optional<size_t> max = 1, bool mustHave = true, const std::type_info* owner = nullptr) : Entry{ID{std::move(ident)}, ind, howMany, max, mustHave, owner} {}
67 Entry(const Entry& source) = default;
68 Entry& operator=(const Entry& source) = default;
69
70 // MARK: - Variables
71
73 const std::type_info* ownerType = nullptr;
75 size_t available = 0;
77 int16_t index = 0;
79 bool required = true;
80
81 // MARK: - Functions (const)
82
84 const Identity& identity() const { return m_id.identity(); }
85 /*
86 Determine if the entry is an attribute
87 @return True if the entry is repeating
88 */
89 bool isAttribute() const { return (m_type == attribute); }
94 bool isRepeating() const { return (!m_maximum || (m_maximum > 1)); }
99 std::optional<size_t> maximum() const { return isAttribute() ? 1 : m_maximum; }
100
101 // MARK: - Functions (mutating)
102
107 void setIdentity(const Identity& ident) { m_id = ID{ident}; }
112 void setIdentity(Identity&& ident) { m_id = ID{std::move(ident)}; }
118 Entry& withOwner(const std::type_info* owner) {
119 ownerType = owner;
120 return *this;
121 }
127 if (!maximum() || (available < maximum())) {
128 ++available;
129 return true;
130 }
131 return false;
132 }
133
134 private:
142 Entry(ID&& id, int16_t ind, Type type, bool mustHave, const std::type_info* owner) : m_id{id} {
143 index = ind;
144 m_type = type;
145 required = mustHave;
146 if (required)
147 available = 1;
148 ownerType = owner;
149 }
158 Entry(ID&& id, int16_t ind, size_t howMany, std::optional<size_t> max, bool mustHave, const std::type_info* owner) : m_id{id} {
159 index = ind;
160 m_maximum = max;
161 if (m_maximum.has_value() && (*m_maximum <= 0))
162 m_maximum = 1;
163 available = howMany;
164 required = mustHave;
165 ownerType = owner;
166 m_type = element;
167 }
168
170 ID m_id;
172 std::optional<size_t> m_maximum = 1;
174 Type m_type = element;
175 };
176
177}
178
179#endif //ACTIVE_SERIALISE_ENTRY
Definition Entry.h:29
const Identity & identity() const
Get the wrapped identity.
Definition Entry.h:35
Definition Cargo.h:12
Definition Entry.h:20
bool required
True if required for export.
Definition Entry.h:79
Entry & withOwner(const std::type_info *owner)
Definition Entry.h:118
Entry(const Identity &ident, int16_t ind, size_t howMany, std::optional< size_t > max=1, bool mustHave=true, const std::type_info *owner=nullptr)
Definition Entry.h:64
bool bumpAvailable()
Definition Entry.h:126
size_t available
How many are currently available (also acts as the requested item on import/export in 'getCargo')
Definition Entry.h:75
void setIdentity(const Identity &ident)
Definition Entry.h:107
Entry(const Identity &ident, int16_t ind, Type type, bool mustHave=true, const std::type_info *owner=nullptr)
Definition Entry.h:52
void setIdentity(Identity &&ident)
Definition Entry.h:112
bool isRepeating() const
Definition Entry.h:94
const Identity & identity() const
Get the wrapped identity.
Definition Entry.h:84
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
std::optional< size_t > maximum() const
Definition Entry.h:99
Definition Identity.h:18