ActiveLib
Loading...
Searching...
No Matches
ValueWrap.h
1
6#ifndef ACTIVE_SERIALISE_VALUE_WRAP
7#define ACTIVE_SERIALISE_VALUE_WRAP
8
9#include "Active/Serialise/Item/Item.h"
10#include "Active/Setting/Values/Value.h"
11#include "Active/Utility/Guid.h"
12
13#include <functional>
14
15namespace active::serialise {
16
23 template<class T>
24 class ValueWrap : public Item, public std::reference_wrapper<T> {
25 public:
26
27 // MARK: - Types
28
30 using base = std::reference_wrapper<T>;
31
32 // MARK: - Constructors
33
38 ValueWrap(T& val) : Item(), base(val) {}
43 //NB: Value is only mutated within import processes, in which case the object must be mutable (i.e. const discard is safe)
44 ValueWrap(const T& val) : base(const_cast<T&>(val)) {}
45
46 // MARK: - Functions (const)
47
53 virtual bool write(utility::String& dest) const override {
54 dest = utility::String{base::get()};
55 return true;
56 }
57
58 // MARK: - Functions (mutating)
59
65 virtual bool read(const utility::String& source) override { base::get() = T{source}; return true; }
71 virtual bool read(const setting::Value& source) override {
72 //If Value supports conversion to this type, assign directly
73 if constexpr(requires (setting::Value& v) { v.operator T(); })
74 base::get() = source;
75 else {
76 utility::String text = source;
77 return read(text); //Otherwise use a string as an intermediate value
78 }
79 return true;
80 }
84 virtual void setDefault() override {}
89 virtual std::optional<Item::Type> type() const override { return Item::Type::number; } //Other types should specialise accordingly
90 };
91
92 // MARK: - Specialisations for string
93
98 template<> inline
99 std::optional<Item::Type> ValueWrap<utility::String>::type() const { return Item::Type::text; }
100
101 // MARK: - Specialisations for guid
102
108 template<> inline
110 dest = base::get();
111 return true;
112 }
113
118 template<> inline
119 std::optional<Item::Type> ValueWrap<utility::Guid>::type() const { return Item::Type::text; }
120
121 // MARK: - Specialisations for bool
122
128 template<> inline
130 utility::String value = source.lowercase();
131 if ((value == "true") || (value == "1"))
132 base::get() = true;
133 else if ((value == "false") || (value == "0"))
134 base::get() = false;
135 else
136 return false;
137 return true;
138 } //ValueWrap<bool>::read
139
140
146 template<> inline
148 dest = get() ? "true" : "false";
149 return true;
150 } //ValueWrap<bool>::write
151
152
157 template<> inline
158 std::optional<Item::Type> ValueWrap<bool>::type() const { return Item::Type::boolean; }
159
168
169}
170
171#endif //ACTIVE_SERIALISE_VALUE_WRAP
Definition Item.h:23
Definition ValueWrap.h:24
virtual std::optional< Item::Type > type() const override
Definition ValueWrap.h:89
ValueWrap(const T &val)
Definition ValueWrap.h:44
virtual bool read(const setting::Value &source) override
Definition ValueWrap.h:71
std::reference_wrapper< T > base
Item reference base.
Definition ValueWrap.h:30
ValueWrap(T &val)
Definition ValueWrap.h:38
virtual void setDefault() override
Definition ValueWrap.h:84
virtual bool read(const utility::String &source) override
Definition ValueWrap.h:65
virtual bool write(utility::String &dest) const override
Definition ValueWrap.h:53
Definition Value.h:31
A Unicode-aware string class.
Definition String.h:51
String lowercase() const
Definition String.cpp:998
Definition Cargo.h:12