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) {
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();

View File

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

View File

@@ -38,11 +38,13 @@ enum class SimpleDynamicTag {
VECTOR,
};
template<class STRING>
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<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));
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<class STRING>
void read_simple_dynamic(SimpleDynamic<STRING> *result) {
SimpleDynamicTag type = SimpleDynamicTag(read_uint8());
switch (type) {
case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break;