From b4c2d21aee93286b5a78b752bbd852730e0d9218 Mon Sep 17 00:00:00 2001 From: jyelon Date: Tue, 17 Oct 2023 19:55:34 -0400 Subject: [PATCH] Add template string to SimpleDynamic --- luprex/cpp/core/animqueue.cpp | 4 ++-- luprex/cpp/core/animqueue.hpp | 2 +- luprex/ext/base-writer.hpp | 22 +++++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/luprex/cpp/core/animqueue.cpp b/luprex/cpp/core/animqueue.cpp index 8dcfb685..079a2bf4 100644 --- a/luprex/cpp/core/animqueue.cpp +++ b/luprex/cpp/core/animqueue.cpp @@ -153,7 +153,7 @@ void AnimState::decode(std::string_view s) { void AnimState::decode_persistent(std::string_view s) { map_.clear(); StreamBuffer sb(s); - SimpleDynamic dummy; + AnimValue dummy; while (!sb.empty()) { eng::string name = sb.read_string(); bool persistent = sb.read_bool(); @@ -297,7 +297,7 @@ void AnimCoreState::decode(std::string_view s) { plane.clear(); xyz = 0.0; StreamBuffer sb(s); - SimpleDynamic value; + AnimValue value; while (!sb.empty()) { eng::string name = sb.read_string(); bool persistent = sb.read_bool(); diff --git a/luprex/cpp/core/animqueue.hpp b/luprex/cpp/core/animqueue.hpp index 9022cd8f..4a745da0 100644 --- a/luprex/cpp/core/animqueue.hpp +++ b/luprex/cpp/core/animqueue.hpp @@ -102,7 +102,7 @@ #include #include -struct AnimValue : public SimpleDynamic { +struct AnimValue : public SimpleDynamic { bool persistent; AnimValue() { persistent = false; } diff --git a/luprex/ext/base-writer.hpp b/luprex/ext/base-writer.hpp index 5722c79a..1129f977 100644 --- a/luprex/ext/base-writer.hpp +++ b/luprex/ext/base-writer.hpp @@ -38,11 +38,13 @@ enum class SimpleDynamicTag { VECTOR, }; +template struct SimpleDynamic { + using string = STRING; SimpleDynamicTag type; double x, y, z; - std::string s; - + string s; + SimpleDynamic() { type = SimpleDynamicTag::UNINITIALIZED; x=y=z=0; @@ -63,16 +65,20 @@ struct SimpleDynamic { return type_name_of(type); } + void set_uninitialized() { + type=SimpleDynamicTag::UNINITIALIZED; s.clear(); x=y=z=0; + } + void set_string(std::string_view is) { - type=SimpleDynamicTag::STRING; s=is; x=0; y=0; z=0; + type=SimpleDynamicTag::STRING; s=is; x=y=z=0; } void set_number(double n) { - type = SimpleDynamicTag::NUMBER; s.clear(); x=n; y=0; z=0; + type = SimpleDynamicTag::NUMBER; s.clear(); x=n; y=z=0; } void set_boolean(bool b) { - type = SimpleDynamicTag::BOOLEAN; s.clear(); x=(b?1:0); y=0; z=0; + type = SimpleDynamicTag::BOOLEAN; s.clear(); x=(b?1:0); y=z=0; } void set_vector(double ix, double iy, double iz) { @@ -162,7 +168,8 @@ public: static_cast(this)->write_bytes(s.data(), s.size()); } - void write_simple_dynamic(const SimpleDynamic &sd) { + template + void write_simple_dynamic(const SimpleDynamic &sd) { write_uint8(uint8_t(sd.type)); switch(sd.type) { case SimpleDynamicTag::NUMBER: write_double(sd.x); break; @@ -273,7 +280,8 @@ public: auto read_string() { return read_string_limit(0x1000000); } // 16MB limit default - void read_simple_dynamic(SimpleDynamic *result) { + template + void read_simple_dynamic(SimpleDynamic *result) { SimpleDynamicTag type = SimpleDynamicTag(read_uint8()); switch (type) { case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break;