Add template string to SimpleDynamic

This commit is contained in:
2023-10-17 19:55:34 -04:00
parent 5373182a59
commit b4c2d21aee
3 changed files with 18 additions and 10 deletions

View File

@@ -153,7 +153,7 @@ void AnimState::decode(std::string_view s) {
void AnimState::decode_persistent(std::string_view s) { void AnimState::decode_persistent(std::string_view s) {
map_.clear(); map_.clear();
StreamBuffer sb(s); StreamBuffer sb(s);
SimpleDynamic dummy; AnimValue dummy;
while (!sb.empty()) { while (!sb.empty()) {
eng::string name = sb.read_string(); eng::string name = sb.read_string();
bool persistent = sb.read_bool(); bool persistent = sb.read_bool();
@@ -297,7 +297,7 @@ void AnimCoreState::decode(std::string_view s) {
plane.clear(); plane.clear();
xyz = 0.0; xyz = 0.0;
StreamBuffer sb(s); StreamBuffer sb(s);
SimpleDynamic value; AnimValue value;
while (!sb.empty()) { while (!sb.empty()) {
eng::string name = sb.read_string(); eng::string name = sb.read_string();
bool persistent = sb.read_bool(); bool persistent = sb.read_bool();

View File

@@ -102,7 +102,7 @@
#include <cassert> #include <cassert>
#include <ostream> #include <ostream>
struct AnimValue : public SimpleDynamic { struct AnimValue : public SimpleDynamic<eng::string> {
bool persistent; bool persistent;
AnimValue() { persistent = false; } AnimValue() { persistent = false; }

View File

@@ -38,11 +38,13 @@ enum class SimpleDynamicTag {
VECTOR, VECTOR,
}; };
template<class STRING>
struct SimpleDynamic { struct SimpleDynamic {
using string = STRING;
SimpleDynamicTag type; SimpleDynamicTag type;
double x, y, z; double x, y, z;
std::string s; string s;
SimpleDynamic() { SimpleDynamic() {
type = SimpleDynamicTag::UNINITIALIZED; type = SimpleDynamicTag::UNINITIALIZED;
x=y=z=0; x=y=z=0;
@@ -63,16 +65,20 @@ struct SimpleDynamic {
return type_name_of(type); return type_name_of(type);
} }
void set_uninitialized() {
type=SimpleDynamicTag::UNINITIALIZED; s.clear(); x=y=z=0;
}
void set_string(std::string_view is) { 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) { 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) { 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) { void set_vector(double ix, double iy, double iz) {
@@ -162,7 +168,8 @@ public:
static_cast<Derived*>(this)->write_bytes(s.data(), s.size()); static_cast<Derived*>(this)->write_bytes(s.data(), s.size());
} }
void write_simple_dynamic(const SimpleDynamic &sd) { template<class STRING>
void write_simple_dynamic(const SimpleDynamic<STRING> &sd) {
write_uint8(uint8_t(sd.type)); write_uint8(uint8_t(sd.type));
switch(sd.type) { switch(sd.type) {
case SimpleDynamicTag::NUMBER: write_double(sd.x); break; case SimpleDynamicTag::NUMBER: write_double(sd.x); break;
@@ -273,7 +280,8 @@ public:
auto read_string() { return read_string_limit(0x1000000); } // 16MB limit default auto read_string() { return read_string_limit(0x1000000); } // 16MB limit default
void read_simple_dynamic(SimpleDynamic *result) { template<class STRING>
void read_simple_dynamic(SimpleDynamic<STRING> *result) {
SimpleDynamicTag type = SimpleDynamicTag(read_uint8()); SimpleDynamicTag type = SimpleDynamicTag(read_uint8());
switch (type) { switch (type) {
case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break; case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break;