summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-09 00:23:16 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-09 00:23:16 +0000
commitbfea0319aa07ad2c921ba29ba216e921d65f2ed1 (patch)
tree8a61e8b5a1280445c94d1443205cc1d2a5a23736
parentFix test-persistence dependencies (diff)
downloadilt-bfea0319aa07ad2c921ba29ba216e921d65f2ed1.tar.bz2
ilt-bfea0319aa07ad2c921ba29ba216e921d65f2ed1.tar.xz
ilt-bfea0319aa07ad2c921ba29ba216e921d65f2ed1.zip
Integer support in persistence
Splits bool and arithmatic types, pass arithmatic values as string views from JSON and parse according to the target type.
-rw-r--r--lib/jsonParse-persistence.cpp8
-rw-r--r--lib/jsonParse-persistence.h3
-rw-r--r--lib/jsonParse.h2
-rw-r--r--lib/jsonParse.ll2
-rw-r--r--lib/persistence.cpp4
-rw-r--r--lib/persistence.h57
-rw-r--r--test/fixtures/json/load_object.json5
-rw-r--r--test/test-persistence.cpp9
-rw-r--r--test/testStructures.cpp4
-rw-r--r--test/testStructures.h1
10 files changed, 64 insertions, 31 deletions
diff --git a/lib/jsonParse-persistence.cpp b/lib/jsonParse-persistence.cpp
index 5c0011a..ba61849 100644
--- a/lib/jsonParse-persistence.cpp
+++ b/lib/jsonParse-persistence.cpp
@@ -36,7 +36,7 @@ namespace Persistence {
}
void
- JsonParsePersistence::pushNumber(float value)
+ JsonParsePersistence::pushNumber(std::string_view value)
{
pushValue(value);
}
@@ -164,6 +164,12 @@ namespace Persistence {
}
void
+ JsonWritePersistence::pushValue(int value) const
+ {
+ strm << value;
+ }
+
+ void
JsonWritePersistence::pushValue(std::nullptr_t) const
{
strm << "null";
diff --git a/lib/jsonParse-persistence.h b/lib/jsonParse-persistence.h
index a676282..6edebc7 100644
--- a/lib/jsonParse-persistence.h
+++ b/lib/jsonParse-persistence.h
@@ -27,7 +27,7 @@ namespace Persistence {
void beginObject() override;
void beginArray() override;
void pushBoolean(bool value) override;
- void pushNumber(float value) override;
+ void pushNumber(std::string_view value) override;
void pushNull() override;
void pushText(std::string && value) override;
void pushKey(std::string && k) override;
@@ -54,6 +54,7 @@ namespace Persistence {
void beginArray() const override;
void pushValue(bool value) const override;
void pushValue(float value) const override;
+ void pushValue(int value) const override;
void pushValue(std::nullptr_t) const override;
void pushValue(const std::string_view value) const override;
void nextValue() const override;
diff --git a/lib/jsonParse.h b/lib/jsonParse.h
index 4205572..c776643 100644
--- a/lib/jsonParse.h
+++ b/lib/jsonParse.h
@@ -25,7 +25,7 @@ namespace json {
virtual void beginArray() = 0;
virtual void pushBoolean(bool) = 0;
- virtual void pushNumber(float) = 0;
+ virtual void pushNumber(std::string_view) = 0;
virtual void pushNull() = 0;
virtual void pushText(std::string &&) = 0;
virtual void pushKey(std::string &&) = 0;
diff --git a/lib/jsonParse.ll b/lib/jsonParse.ll
index 8029830..100bc46 100644
--- a/lib/jsonParse.ll
+++ b/lib/jsonParse.ll
@@ -52,7 +52,7 @@ text [^\\\"]*
}
<ARRAY_ITEM,INITIAL>{number} {
- pushNumber(std::strtof(YYText(), NULL));
+ pushNumber(YYText());
yy_pop_state();
}
diff --git a/lib/persistence.cpp b/lib/persistence.cpp
index 38e2ac6..f6f0c17 100644
--- a/lib/persistence.cpp
+++ b/lib/persistence.cpp
@@ -103,9 +103,9 @@ namespace Persistence {
}
void
- Selection::setValue(float)
+ Selection::setValue(std::string_view)
{
- throw std::runtime_error("Unexpected float");
+ throw std::runtime_error("Unexpected numeric");
}
void
diff --git a/lib/persistence.h b/lib/persistence.h
index 92f3052..c53ff99 100644
--- a/lib/persistence.h
+++ b/lib/persistence.h
@@ -37,6 +37,7 @@ namespace Persistence {
virtual void beginArray() const = 0;
virtual void pushValue(bool value) const = 0;
virtual void pushValue(float value) const = 0;
+ virtual void pushValue(int value) const = 0;
virtual void pushValue(std::nullptr_t) const = 0;
virtual void pushValue(std::string_view value) const = 0;
virtual void nextValue() const = 0;
@@ -50,7 +51,7 @@ namespace Persistence {
virtual ~Selection() = default;
DEFAULT_MOVE_COPY(Selection);
- virtual void setValue(float);
+ virtual void setValue(std::string_view);
virtual void setValue(bool);
virtual void setValue(std::nullptr_t);
virtual void setValue(std::string &&);
@@ -91,34 +92,24 @@ namespace Persistence {
};
template<typename T>
- concept Scalar = std::is_scalar_v<T>;
- template<typename T>
- concept NotScalar = (!Scalar<T>);
+ concept Arithmatic = std::is_arithmetic_v<T>;
- template<Scalar T> struct SelectionT<T> : public SelectionV<T> {
- using SelectionV<T>::SelectionV;
+ template<> struct SelectionT<bool> : public SelectionV<bool> {
+ using SelectionV<bool>::SelectionV;
using Selection::setValue;
void
- setValue(T evalue) override
+ setValue(bool evalue) override
{
- std::swap(this->v, evalue);
+ this->v = evalue;
}
void
setValue(std::string && evalue) override
{
- if constexpr (std::same_as<T, bool>) {
- using namespace std::literals;
- if (!(this->v = evalue == "true"sv)) {
- if (evalue != "false"sv) {
- throw std::runtime_error("Value conversion failure");
- }
- }
- }
- else {
- if (auto res = std::from_chars(evalue.c_str(), evalue.c_str() + evalue.length(), this->v).ec;
- res != std::errc {}) {
+ using namespace std::literals;
+ if (!(this->v = evalue == "true"sv)) {
+ if (evalue != "false"sv) {
throw std::runtime_error("Value conversion failure");
}
}
@@ -131,7 +122,33 @@ namespace Persistence {
}
};
- template<NotScalar T> struct SelectionT<T> : public SelectionV<T> {
+ template<Arithmatic T> struct SelectionT<T> : public SelectionV<T> {
+ using SelectionV<T>::SelectionV;
+ using Selection::setValue;
+
+ void
+ setValue(std::string_view evalue) override
+ {
+ if (auto res = std::from_chars(evalue.data(), evalue.data() + evalue.length(), this->v).ec;
+ res != std::errc {}) {
+ throw std::runtime_error("Value conversion failure");
+ }
+ }
+
+ void
+ setValue(std::string && evalue) override
+ {
+ setValue(std::string_view {evalue});
+ }
+
+ void
+ write(const Writer & out) const override
+ {
+ out.pushValue(this->v);
+ }
+ };
+
+ template<typename T> struct SelectionT : public SelectionV<T> {
using SelectionV<T>::SelectionV;
using Selection::setValue;
diff --git a/test/fixtures/json/load_object.json b/test/fixtures/json/load_object.json
index c622885..5848e87 100644
--- a/test/fixtures/json/load_object.json
+++ b/test/fixtures/json/load_object.json
@@ -8,6 +8,11 @@
6.28,
1.57
],
+ "gpos": [
+ 2147483647,
+ 2147483646,
+ -2147483648
+ ],
"flts": [
3.14,
6.28,
diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp
index abb68c2..ce53f72 100644
--- a/test/test-persistence.cpp
+++ b/test/test-persistence.cpp
@@ -42,6 +42,9 @@ BOOST_FIXTURE_TEST_CASE(load_object, JPP)
BOOST_CHECK_CLOSE(to->pos[0], 3.14, 0.01);
BOOST_CHECK_CLOSE(to->pos[1], 6.28, 0.01);
BOOST_CHECK_CLOSE(to->pos[2], 1.57, 0.01);
+ BOOST_CHECK_EQUAL(to->gpos[0], 2147483647);
+ BOOST_CHECK_EQUAL(to->gpos[1], 2147483646);
+ BOOST_CHECK_EQUAL(to->gpos[2], -2147483648);
BOOST_REQUIRE_EQUAL(to->flts.size(), 6);
BOOST_CHECK_CLOSE(to->flts[0], 3.14, 0.01);
BOOST_CHECK_CLOSE(to->flts[1], 6.28, 0.01);
@@ -255,7 +258,7 @@ BOOST_AUTO_TEST_CASE(write_test_dfl)
std::stringstream ss;
Persistence::JsonWritePersistence {ss}.saveState(to);
BOOST_CHECK_EQUAL(ss.str(),
- R"({"p.typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"vptr":[]})");
+ R"({"p.typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"gpos":[0,0,0],"flts":[],"poss":[],"nest":[],"vptr":[]})");
}
BOOST_FIXTURE_TEST_CASE(write_test_loaded, JPP)
@@ -264,7 +267,7 @@ BOOST_FIXTURE_TEST_CASE(write_test_loaded, JPP)
std::stringstream ss;
Persistence::JsonWritePersistence {ss}.saveState(to);
BOOST_CHECK_EQUAL(ss.str(),
- R"({"p.typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":true,"pos":[3.14,6.28,1.57],"flts":[3.14,6.28,1.57,0,-1,-3.14],"poss":[[3.14,6.28,1.57],[0,-1,-3.14]],"nest":[[["a","b"],["c","d","e"]],[["f"]],[]],"ptr":{"p.typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"vptr":[]},"vptr":[]})");
+ R"({"p.typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":true,"pos":[3.14,6.28,1.57],"gpos":[2147483647,2147483646,-2147483648],"flts":[3.14,6.28,1.57,0,-1,-3.14],"poss":[[3.14,6.28,1.57],[0,-1,-3.14]],"nest":[[["a","b"],["c","d","e"]],[["f"]],[]],"ptr":{"p.typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":false,"pos":[0,0,0],"gpos":[0,0,0],"flts":[],"poss":[],"nest":[],"vptr":[]},"vptr":[]})");
}
BOOST_FIXTURE_TEST_CASE(write_test_loaded_abs, JPP)
@@ -273,7 +276,7 @@ BOOST_FIXTURE_TEST_CASE(write_test_loaded_abs, JPP)
std::stringstream ss;
Persistence::JsonWritePersistence {ss}.saveState(to);
BOOST_CHECK_EQUAL(ss.str(),
- R"({"p.typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"aptr":{"p.typeid":"SubObject","base":"set base","sub":"set sub"},"vptr":[]})");
+ R"({"p.typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"gpos":[0,0,0],"flts":[],"poss":[],"nest":[],"aptr":{"p.typeid":"SubObject","base":"set base","sub":"set sub"},"vptr":[]})");
}
BOOST_FIXTURE_TEST_CASE(write_test_loaded_shared, JPP)
diff --git a/test/testStructures.cpp b/test/testStructures.cpp
index 469ec37..fd07158 100644
--- a/test/testStructures.cpp
+++ b/test/testStructures.cpp
@@ -38,8 +38,8 @@ bool
TestObject::persist(Persistence::PersistenceStore & store)
{
return STORE_TYPE && STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos)
- && STORE_MEMBER(flts) && STORE_MEMBER(poss) && STORE_MEMBER(nest) && STORE_MEMBER(ptr) && STORE_MEMBER(aptr)
- && STORE_MEMBER(vptr);
+ && STORE_MEMBER(gpos) && STORE_MEMBER(flts) && STORE_MEMBER(poss) && STORE_MEMBER(nest) && STORE_MEMBER(ptr)
+ && STORE_MEMBER(aptr) && STORE_MEMBER(vptr);
}
void
diff --git a/test/testStructures.h b/test/testStructures.h
index 064c00c..4eb4764 100644
--- a/test/testStructures.h
+++ b/test/testStructures.h
@@ -33,6 +33,7 @@ struct TestObject : public Persistence::Persistable {
std::string str {};
bool bl {};
RelativePosition3D pos {};
+ GlobalPosition3D gpos {};
std::vector<float> flts;
std::vector<RelativePosition3D> poss;
std::vector<std::vector<std::vector<std::string>>> nest;