Code cleanup and refactoring.
This commit is contained in:
@@ -17,21 +17,22 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SimpleDynamic
|
||||
// LuaValueHolder
|
||||
//
|
||||
// A struct that holds a dynamically typed value.
|
||||
// This can hold a string, number, vector, or boolean.
|
||||
// This can hold a string, token, number, vector, or boolean.
|
||||
//
|
||||
// The type is stored in the 'type' field.
|
||||
//
|
||||
// If it's a STRING, the value is in the field s
|
||||
// If it's a TOKEN, the value is stored in the field s
|
||||
// If it's a NUMBER, the value is in the field x
|
||||
// If it's a BOOLEAN, it's true if (x==1.0)
|
||||
// If it's a VECTOR, the value is in x,y,z
|
||||
//
|
||||
///////////////////////////////////////////////////////////////
|
||||
|
||||
enum class SimpleDynamicTag {
|
||||
enum class LuaValueType {
|
||||
UNINITIALIZED,
|
||||
STRING,
|
||||
TOKEN,
|
||||
@@ -41,25 +42,25 @@ enum class SimpleDynamicTag {
|
||||
};
|
||||
|
||||
template<class STRING>
|
||||
struct SimpleDynamic {
|
||||
struct LuaValueHolder {
|
||||
using string = STRING;
|
||||
SimpleDynamicTag type;
|
||||
LuaValueType type;
|
||||
double x, y, z;
|
||||
string s;
|
||||
|
||||
SimpleDynamic() {
|
||||
type = SimpleDynamicTag::UNINITIALIZED;
|
||||
LuaValueHolder() {
|
||||
type = LuaValueType::UNINITIALIZED;
|
||||
x=y=z=0;
|
||||
}
|
||||
|
||||
static const char *type_name_of(SimpleDynamicTag t) {
|
||||
static const char *type_name_of(LuaValueType t) {
|
||||
switch (t) {
|
||||
case SimpleDynamicTag::UNINITIALIZED: return "uninitialized";
|
||||
case SimpleDynamicTag::STRING: return "string";
|
||||
case SimpleDynamicTag::TOKEN: return "token";
|
||||
case SimpleDynamicTag::BOOLEAN: return "boolean";
|
||||
case SimpleDynamicTag::NUMBER: return "number";
|
||||
case SimpleDynamicTag::VECTOR: return "vector";
|
||||
case LuaValueType::UNINITIALIZED: return "uninitialized";
|
||||
case LuaValueType::STRING: return "string";
|
||||
case LuaValueType::TOKEN: return "token";
|
||||
case LuaValueType::BOOLEAN: return "boolean";
|
||||
case LuaValueType::NUMBER: return "number";
|
||||
case LuaValueType::VECTOR: return "vector";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
@@ -69,30 +70,30 @@ struct SimpleDynamic {
|
||||
}
|
||||
|
||||
void set_uninitialized() {
|
||||
type=SimpleDynamicTag::UNINITIALIZED; s.clear(); x=y=z=0;
|
||||
type=LuaValueType::UNINITIALIZED; s.clear(); x=y=z=0;
|
||||
}
|
||||
|
||||
void set_string(std::string_view is) {
|
||||
type=SimpleDynamicTag::STRING; s=is; x=y=z=0;
|
||||
type=LuaValueType::STRING; s=is; x=y=z=0;
|
||||
}
|
||||
|
||||
void set_token(std::string_view is) {
|
||||
type=SimpleDynamicTag::TOKEN; s=is; x=y=z=0;
|
||||
type=LuaValueType::TOKEN; s=is; x=y=z=0;
|
||||
}
|
||||
|
||||
void set_number(double n) {
|
||||
type = SimpleDynamicTag::NUMBER; s.clear(); x=n; y=z=0;
|
||||
type = LuaValueType::NUMBER; s.clear(); x=n; y=z=0;
|
||||
}
|
||||
|
||||
void set_boolean(bool b) {
|
||||
type = SimpleDynamicTag::BOOLEAN; s.clear(); x=(b?1:0); y=z=0;
|
||||
type = LuaValueType::BOOLEAN; s.clear(); x=(b?1:0); y=z=0;
|
||||
}
|
||||
|
||||
void set_vector(double ix, double iy, double iz) {
|
||||
type = SimpleDynamicTag::VECTOR; s.clear(); x=ix; y=iy; z=iz;
|
||||
type = LuaValueType::VECTOR; s.clear(); x=ix; y=iy; z=iz;
|
||||
}
|
||||
|
||||
void copy_value(const SimpleDynamic &other) {
|
||||
void copy_value(const LuaValueHolder &other) {
|
||||
type = other.type;
|
||||
s=other.s; x=other.x; y=other.y; z=other.z;
|
||||
}
|
||||
@@ -492,26 +493,26 @@ public:
|
||||
write_bytes(s);
|
||||
}
|
||||
|
||||
// Write a SimpleDynamicTag.
|
||||
// Write a LuaValueType.
|
||||
//
|
||||
void write_simple_dynamic_tag(SimpleDynamicTag tag) {
|
||||
void write_simple_dynamic_tag(LuaValueType tag) {
|
||||
write_uint8(uint8_t(tag));
|
||||
}
|
||||
|
||||
// Write a SimpleDynamic value.
|
||||
// Write a LuaValueHolder value.
|
||||
//
|
||||
// This works regardless of what kind of data is present in the
|
||||
// SimpleDynamic.
|
||||
// LuaValueHolder.
|
||||
//
|
||||
template<class STRING>
|
||||
void write_simple_dynamic(const SimpleDynamic<STRING> &sd) {
|
||||
void write_simple_dynamic(const LuaValueHolder<STRING> &sd) {
|
||||
write_simple_dynamic_tag(sd.type);
|
||||
switch(sd.type) {
|
||||
case SimpleDynamicTag::STRING: write_string(sd.s); break;
|
||||
case SimpleDynamicTag::TOKEN: write_string(sd.s); break;
|
||||
case SimpleDynamicTag::NUMBER: write_double(sd.x); break;
|
||||
case SimpleDynamicTag::BOOLEAN: write_bool(sd.x == 1.0); break;
|
||||
case SimpleDynamicTag::VECTOR: write_double(sd.x); write_double(sd.y); write_double(sd.z); break;
|
||||
case LuaValueType::STRING: write_string(sd.s); break;
|
||||
case LuaValueType::TOKEN: write_string(sd.s); break;
|
||||
case LuaValueType::NUMBER: write_double(sd.x); break;
|
||||
case LuaValueType::BOOLEAN: write_bool(sd.x == 1.0); break;
|
||||
case LuaValueType::VECTOR: write_double(sd.x); write_double(sd.y); write_double(sd.z); break;
|
||||
default: assert(false);
|
||||
}
|
||||
}
|
||||
@@ -616,23 +617,23 @@ public:
|
||||
return read_string_limit(0x1000000);
|
||||
}
|
||||
|
||||
// Read a SimpleDynamicTag
|
||||
// Read a LuaValueType
|
||||
//
|
||||
SimpleDynamicTag read_simple_dynamic_tag() {
|
||||
return SimpleDynamicTag(read_uint8());
|
||||
LuaValueType read_simple_dynamic_tag() {
|
||||
return LuaValueType(read_uint8());
|
||||
}
|
||||
|
||||
// Read a SimpleDynamic
|
||||
// Read a LuaValueHolder
|
||||
//
|
||||
template<class STRING>
|
||||
void read_simple_dynamic(SimpleDynamic<STRING> *result) {
|
||||
SimpleDynamicTag type = read_simple_dynamic_tag();
|
||||
void read_simple_dynamic(LuaValueHolder<STRING> *result) {
|
||||
LuaValueType type = read_simple_dynamic_tag();
|
||||
switch (type) {
|
||||
case SimpleDynamicTag::STRING: result->set_string(read_string()); break;
|
||||
case SimpleDynamicTag::TOKEN: result->set_token(read_string()); break;
|
||||
case SimpleDynamicTag::NUMBER: result->set_number(read_double()); break;
|
||||
case SimpleDynamicTag::BOOLEAN: result->set_boolean(read_bool()); break;
|
||||
case SimpleDynamicTag::VECTOR: {
|
||||
case LuaValueType::STRING: result->set_string(read_string()); break;
|
||||
case LuaValueType::TOKEN: result->set_token(read_string()); break;
|
||||
case LuaValueType::NUMBER: result->set_number(read_double()); break;
|
||||
case LuaValueType::BOOLEAN: result->set_boolean(read_bool()); break;
|
||||
case LuaValueType::VECTOR: {
|
||||
double x=read_double();
|
||||
double y=read_double();
|
||||
double z=read_double();
|
||||
|
||||
Reference in New Issue
Block a user