refactor(datatypes): more refactoring to allow for enums
This commit is contained in:
parent
0f44012e33
commit
5149e34723
17 changed files with 116 additions and 60 deletions
|
@ -418,10 +418,10 @@ void data::writeCodeForClass(std::ofstream& out, std::string headerPath, ClassAn
|
|||
out << "#include \"datatypes/variant.h\"\n";
|
||||
out << "#include <pugixml.hpp>\n";
|
||||
out << "#include \"lua.h\"\n\n";
|
||||
out << "const TypeInfo " << state.name << "::TYPE = {\n"
|
||||
out << "const TypeDescriptor " << state.name << "::TYPE = {\n"
|
||||
<< " .name = \"" << state.serializedName << "\",\n"
|
||||
<< " .serializer = toVariantFunction(&" << state.name << "::Serialize),"
|
||||
<< " .deserializer = &" << state.name << "::Deserialize,\n"
|
||||
<< " .deserializer = toVariantGenerator(&" << state.name << "::Deserialize),\n"
|
||||
<< " .toString = toVariantFunction(&" << state.name << "::ToString),";
|
||||
if (state.hasFromString) out << " .fromString = &" << state.name << "::FromString,\n";
|
||||
out << " .pushLuaValue = toVariantFunction(&" << state.name << "::PushLuaValue),"
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <string>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include "datatypes/enum.h"
|
||||
#include "error/result.h"
|
||||
#include "error/data.h"
|
||||
|
||||
|
@ -29,22 +28,12 @@ struct TypeDescriptor {
|
|||
FromLuaValue fromLuaValue;
|
||||
};
|
||||
|
||||
enum DataType {
|
||||
// This distinction is not currently useful, so to
|
||||
// minimize complexity, there will only be two categories
|
||||
// (for now)
|
||||
//DATA_PRIMITIVE,
|
||||
//DATA_COMPOUND,
|
||||
DATA_VALUE,
|
||||
DATA_ENUM,
|
||||
};
|
||||
class Enum;
|
||||
|
||||
struct TypeInfo {
|
||||
DataType type;
|
||||
union {
|
||||
const TypeDescriptor* descriptor;
|
||||
_EnumData* enumData;
|
||||
};
|
||||
Enum* enum_;
|
||||
|
||||
inline TypeInfo(const TypeDescriptor* descriptor) : type(DATA_VALUE), descriptor(descriptor) {}
|
||||
inline TypeInfo(const TypeDescriptor* descriptor) : descriptor(descriptor) {}
|
||||
TypeInfo(Enum*);
|
||||
};
|
|
@ -134,7 +134,7 @@ void CFrame::Serialize(pugi::xml_node node) const {
|
|||
}
|
||||
|
||||
|
||||
Variant CFrame::Deserialize(pugi::xml_node node) {
|
||||
CFrame CFrame::Deserialize(pugi::xml_node node) {
|
||||
return CFrame(
|
||||
node.child("X").text().as_float(),
|
||||
node.child("Y").text().as_float(),
|
||||
|
|
|
@ -40,7 +40,7 @@ public:
|
|||
|
||||
virtual const std::string ToString() const;
|
||||
virtual void Serialize(pugi::xml_node parent) const;
|
||||
static Variant Deserialize(pugi::xml_node node);
|
||||
static CFrame Deserialize(pugi::xml_node node);
|
||||
|
||||
static void PushLuaLibrary(lua_State*);
|
||||
|
||||
|
|
|
@ -39,6 +39,6 @@ void Color3::Serialize(pugi::xml_node node) const {
|
|||
node.text().set(this->ToHex());
|
||||
}
|
||||
|
||||
Variant Color3::Deserialize(pugi::xml_node node) {
|
||||
Color3 Color3::Deserialize(pugi::xml_node node) {
|
||||
return Color3::FromHex(node.text().get());
|
||||
}
|
|
@ -21,7 +21,7 @@ public:
|
|||
virtual const std::string ToString() const;
|
||||
DEF_DATA_METHOD std::string ToHex() const;
|
||||
virtual void Serialize(pugi::xml_node node) const;
|
||||
static Variant Deserialize(pugi::xml_node node);
|
||||
static Color3 Deserialize(pugi::xml_node node);
|
||||
|
||||
static void PushLuaLibrary(lua_State*);
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
#include "enum.h"
|
||||
#include "datatypes/base.h"
|
||||
#include "datatypes/variant.h"
|
||||
|
||||
TypeInfo::TypeInfo(Enum* enum_) : enum_(enum_), descriptor(&EnumItem::TYPE) {}
|
||||
|
||||
Enum::Enum(_EnumData* data) : data(data) {}
|
||||
|
||||
std::vector<EnumItem> Enum::GetEnumItems() {
|
||||
std::vector<EnumItem> Enum::GetEnumItems() const {
|
||||
std::vector<EnumItem> enumItems;
|
||||
|
||||
for (int i = 0; i < data->count; i++) {
|
||||
|
@ -12,7 +16,7 @@ std::vector<EnumItem> Enum::GetEnumItems() {
|
|||
return enumItems;
|
||||
}
|
||||
|
||||
std::optional<EnumItem> Enum::FromName(std::string name) {
|
||||
std::optional<EnumItem> Enum::FromName(std::string name) const {
|
||||
for (int i = 0; i < data->count; i++) {
|
||||
if (data->values[i].second == name)
|
||||
return EnumItem(data, name, data->values[i].first);
|
||||
|
@ -20,7 +24,7 @@ std::optional<EnumItem> Enum::FromName(std::string name) {
|
|||
return {};
|
||||
}
|
||||
|
||||
std::optional<EnumItem> Enum::FromValue(int value) {
|
||||
std::optional<EnumItem> Enum::FromValue(int value) const {
|
||||
for (int i = 0; i < data->count; i++) {
|
||||
if (data->values[i].first == value)
|
||||
return EnumItem(data, data->values[i].second, value);
|
||||
|
@ -29,3 +33,27 @@ std::optional<EnumItem> Enum::FromValue(int value) {
|
|||
}
|
||||
|
||||
EnumItem::EnumItem(_EnumData* parentData, std::string name, int value) : parentData(parentData), name(name), value(value) {}
|
||||
|
||||
//
|
||||
|
||||
std::string Enum::ToString() {
|
||||
return "Enum." + this->data->name;
|
||||
}
|
||||
|
||||
void Enum::PushLuaValue(lua_State*) {
|
||||
|
||||
}
|
||||
|
||||
Variant Enum::FromLuaValue(lua_State*, int) {
|
||||
|
||||
}
|
||||
|
||||
const TypeDescriptor Enum::TYPE {
|
||||
.name = "Enum",
|
||||
.toString = toVariantFunction(&Enum::ToString),
|
||||
// .fromString = Enum_FromString,
|
||||
// .pushLuaValue = &Enum_PushLuaValue,
|
||||
.fromLuaValue = toVariantGenerator(Enum::FromLuaValue),
|
||||
// .serializer = Enum_Serialize,
|
||||
// .deserializer = Enum_Deserialize,
|
||||
};
|
|
@ -1,11 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include "base.h"
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "lua.h"
|
||||
|
||||
struct _EnumData {
|
||||
std::pair<int, std::string>* values;
|
||||
std::string name;
|
||||
int count;
|
||||
};
|
||||
|
||||
|
@ -16,9 +19,16 @@ class Enum {
|
|||
public:
|
||||
Enum(_EnumData*);
|
||||
|
||||
std::vector<EnumItem> GetEnumItems();
|
||||
std::optional<EnumItem> FromName(std::string);
|
||||
std::optional<EnumItem> FromValue(int);
|
||||
static const TypeDescriptor TYPE;
|
||||
|
||||
inline _EnumData* InternalType() const { return this->data; };
|
||||
std::vector<EnumItem> GetEnumItems() const;
|
||||
std::optional<EnumItem> FromName(std::string) const;
|
||||
std::optional<EnumItem> FromValue(int) const;
|
||||
|
||||
std::string ToString();
|
||||
void PushLuaValue(lua_State*);
|
||||
static Variant FromLuaValue(lua_State*, int);
|
||||
};
|
||||
|
||||
class EnumItem {
|
||||
|
@ -27,7 +37,10 @@ class EnumItem {
|
|||
int value;
|
||||
public:
|
||||
EnumItem(_EnumData*, std::string, int);
|
||||
inline std::string Name() { return this->name; }
|
||||
inline int Value() { return this->value; }
|
||||
inline Enum EnumType() { return Enum(this->parentData); }
|
||||
|
||||
static const TypeDescriptor TYPE;
|
||||
|
||||
inline std::string Name() const { return this->name; }
|
||||
inline int Value() const { return this->value; }
|
||||
inline Enum EnumType() const { return Enum(this->parentData); }
|
||||
};
|
|
@ -39,7 +39,7 @@ void InstanceRef::Serialize(pugi::xml_node node) const {
|
|||
panic();
|
||||
}
|
||||
|
||||
Variant InstanceRef::Deserialize(pugi::xml_node node) {
|
||||
InstanceRef InstanceRef::Deserialize(pugi::xml_node node) {
|
||||
// Handled by Instance
|
||||
panic();
|
||||
}
|
||||
|
|
|
@ -20,6 +20,6 @@ public:
|
|||
virtual const std::string ToString() const;
|
||||
virtual void Serialize(pugi::xml_node node) const;
|
||||
virtual void PushLuaValue(lua_State*) const;
|
||||
static Variant Deserialize(pugi::xml_node node);
|
||||
static InstanceRef Deserialize(pugi::xml_node node);
|
||||
static result<Variant, LuaCastError> FromLuaValue(lua_State*, int idx);
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
#include "variant.h"
|
||||
#include "datatypes/base.h"
|
||||
#include "datatypes/cframe.h"
|
||||
#include "datatypes/enum.h"
|
||||
#include "datatypes/primitives.h"
|
||||
#include "datatypes/ref.h"
|
||||
#include "datatypes/signal.h"
|
||||
|
@ -31,26 +32,44 @@ const TypeDescriptor* VARIANT_TYPES[] {
|
|||
&InstanceRef::TYPE,
|
||||
&SignalRef::TYPE,
|
||||
&SignalConnectionRef::TYPE,
|
||||
// &Enum::TYPE,
|
||||
// &EnumItem::TYPE,
|
||||
};
|
||||
|
||||
const TypeInfo Variant::GetTypeInfo() const {
|
||||
return VARIANT_TYPES[wrapped.index()];
|
||||
}
|
||||
|
||||
const TypeDescriptor* Variant::GetType() const {
|
||||
return VARIANT_TYPES[wrapped.index()];
|
||||
}
|
||||
|
||||
std::string Variant::ToString() const {
|
||||
if (!VARIANT_TYPES[wrapped.index()]->pushLuaValue) {
|
||||
Logger::fatalErrorf("Data type %s does not implement toString", VARIANT_TYPES[wrapped.index()]->name.c_str());
|
||||
}
|
||||
return VARIANT_TYPES[wrapped.index()]->toString(*this);
|
||||
}
|
||||
|
||||
void Variant::Serialize(pugi::xml_node node) const {
|
||||
if (!VARIANT_TYPES[wrapped.index()]->pushLuaValue) {
|
||||
Logger::fatalErrorf("Data type %s does not implement serializer", VARIANT_TYPES[wrapped.index()]->name.c_str());
|
||||
}
|
||||
VARIANT_TYPES[wrapped.index()]->serializer(*this, node);
|
||||
}
|
||||
|
||||
void Variant::PushLuaValue(lua_State* state) const {
|
||||
printf("What %zu\n", wrapped.index());
|
||||
if (!VARIANT_TYPES[wrapped.index()]->pushLuaValue) {
|
||||
Logger::fatalErrorf("Data type %s does not implement pushLuaValue", VARIANT_TYPES[wrapped.index()]->name.c_str());
|
||||
}
|
||||
VARIANT_TYPES[wrapped.index()]->pushLuaValue(*this, state);
|
||||
}
|
||||
|
||||
Variant Variant::Deserialize(pugi::xml_node node, const TypeInfo type) {
|
||||
if (type.type == DATA_VALUE)
|
||||
if (!type.descriptor->deserializer) {
|
||||
Logger::fatalErrorf("Data type %s does not implement deserialize", type.descriptor->name.c_str());
|
||||
}
|
||||
return type.descriptor->deserializer(node);
|
||||
else
|
||||
panic(); //TODO: NYI
|
||||
}
|
||||
|
||||
std::map<std::string, const TypeDescriptor*> TYPE_MAP = {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <map>
|
||||
#include "base.h"
|
||||
#include "datatypes/color3.h"
|
||||
#include "datatypes/enum.h"
|
||||
#include "datatypes/ref.h"
|
||||
#include "datatypes/signal.h"
|
||||
#include "vector.h"
|
||||
|
@ -28,7 +29,9 @@ typedef std::variant<
|
|||
Color3,
|
||||
InstanceRef,
|
||||
SignalRef,
|
||||
SignalConnectionRef
|
||||
SignalConnectionRef,
|
||||
Enum,
|
||||
EnumItem
|
||||
> __VARIANT_TYPE;
|
||||
|
||||
class Variant {
|
||||
|
@ -38,6 +41,9 @@ public:
|
|||
template <typename T> T get() { return std::get<T>(wrapped); }
|
||||
std::string ToString() const;
|
||||
|
||||
const TypeInfo GetTypeInfo() const;
|
||||
const TypeDescriptor* GetType() const;
|
||||
|
||||
void Serialize(pugi::xml_node node) const;
|
||||
void PushLuaValue(lua_State* state) const;
|
||||
static Variant Deserialize(pugi::xml_node node, const TypeInfo);
|
||||
|
@ -57,5 +63,12 @@ std::function<R(Variant, Args...)> toVariantFunction(R(T::*f)(Args...) const) {
|
|||
};
|
||||
}
|
||||
|
||||
template <typename T, typename ...Args>
|
||||
std::function<Variant(Args...)> toVariantGenerator(T(f)(Args...)) {
|
||||
return [f](Args... args) {
|
||||
return (Variant)f(args...);
|
||||
};
|
||||
}
|
||||
|
||||
// Map of all data types to their type names
|
||||
extern std::map<std::string, const TypeDescriptor*> TYPE_MAP;
|
|
@ -87,7 +87,7 @@ void Vector3::Serialize(pugi::xml_node node) const {
|
|||
node.append_child("Z").text().set(std::to_string(this->Z()));
|
||||
}
|
||||
|
||||
Variant Vector3::Deserialize(pugi::xml_node node) {
|
||||
Vector3 Vector3::Deserialize(pugi::xml_node node) {
|
||||
return Vector3(node.child("X").text().as_float(), node.child("Y").text().as_float(), node.child("Z").text().as_float());
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ public:
|
|||
virtual const std::string ToString() const;
|
||||
virtual void Serialize(pugi::xml_node node) const;
|
||||
|
||||
static Variant Deserialize(pugi::xml_node node);
|
||||
static Vector3 Deserialize(pugi::xml_node node);
|
||||
static std::optional<Variant> FromString(std::string);
|
||||
|
||||
static void PushLuaLibrary(lua_State*);
|
||||
|
|
|
@ -270,11 +270,11 @@ void Instance::Serialize(pugi::xml_node parent, RefStateSerialize state) {
|
|||
PropertyMeta meta = GetPropertyMeta(name).expect("Meta of declared property is missing");
|
||||
if (meta.flags & (PROP_NOSAVE | PROP_READONLY)) continue; // This property should not be serialized. Skip...
|
||||
|
||||
pugi::xml_node propertyNode = propertiesNode.append_child(meta.type.type == DATA_ENUM ? "token" : meta.type.descriptor->name);
|
||||
pugi::xml_node propertyNode = propertiesNode.append_child(meta.type.descriptor->name);
|
||||
propertyNode.append_attribute("name").set_value(name);
|
||||
|
||||
// Update std::shared_ptr<Instance> properties using map above
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
if (meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
std::weak_ptr<Instance> refWeak = GetPropertyValue(name).expect("Declared property is missing").get<InstanceRef>();
|
||||
if (refWeak.expired()) continue;
|
||||
|
||||
|
@ -335,7 +335,7 @@ result<std::shared_ptr<Instance>, NoSuchInstance> Instance::Deserialize(pugi::xm
|
|||
auto meta = meta_.expect();
|
||||
|
||||
// Update std::shared_ptr<Instance> properties using map above
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
if (meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
if (propertyNode.text().empty())
|
||||
continue;
|
||||
|
||||
|
@ -437,7 +437,7 @@ std::optional<std::shared_ptr<Instance>> Instance::Clone(RefStateClone state) {
|
|||
if (meta.flags & (PROP_READONLY | PROP_NOSAVE)) continue;
|
||||
|
||||
// Update std::shared_ptr<Instance> properties using map above
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
if (meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
std::weak_ptr<Instance> refWeak = GetPropertyValue(property).expect().get<InstanceRef>();
|
||||
if (refWeak.expired()) continue;
|
||||
|
||||
|
@ -487,7 +487,7 @@ std::vector<std::pair<std::string, std::shared_ptr<Instance>>> Instance::GetRefe
|
|||
|
||||
for (std::string property : propertyNames) {
|
||||
PropertyMeta meta = GetPropertyMeta(property).expect();
|
||||
if (meta.type.type != DATA_VALUE || meta.type.descriptor != &InstanceRef::TYPE) continue;
|
||||
if (meta.type.descriptor != &InstanceRef::TYPE) continue;
|
||||
|
||||
std::weak_ptr<Instance> ref = GetPropertyValue(property).expect().get<InstanceRef>();
|
||||
if (ref.expired()) continue;
|
||||
|
|
|
@ -131,7 +131,7 @@ std::shared_ptr<DataModel> DataModel::CloneModel() {
|
|||
if (meta.flags & (PROP_READONLY | PROP_NOSAVE)) continue;
|
||||
|
||||
// Update std::shared_ptr<Instance> properties using map above
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
if (meta.type.descriptor == &InstanceRef::TYPE) {
|
||||
std::weak_ptr<Instance> refWeak = GetPropertyValue(property).expect().get<InstanceRef>();
|
||||
if (refWeak.expired()) continue;
|
||||
|
||||
|
|
|
@ -66,9 +66,7 @@ public:
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
if (meta.type.type == DATA_ENUM) {
|
||||
|
||||
} else if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
|
||||
spinBox->setValue(currentValue.get<float>());
|
||||
|
||||
|
@ -134,9 +132,7 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
if (meta.type.type == DATA_ENUM) {
|
||||
|
||||
} else if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
||||
|
||||
spinBox->setValue(currentValue.get<float>());
|
||||
|
@ -191,9 +187,7 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
if (meta.type.type == DATA_ENUM) {
|
||||
|
||||
} else if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
||||
|
||||
inst->SetPropertyValue(propertyName, (float)spinBox->value()).expect();
|
||||
|
@ -327,7 +321,7 @@ void PropertiesView::setSelected(std::optional<std::shared_ptr<Instance>> instan
|
|||
item->setData(1, Qt::DisplayRole, QString::fromStdString(currentValue.ToString()));
|
||||
}
|
||||
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor != &Color3::TYPE && (!meta.type.descriptor->fromString || meta.flags & PROP_READONLY)) {
|
||||
if (meta.type.descriptor != &Color3::TYPE && (!meta.type.descriptor->fromString || meta.flags & PROP_READONLY)) {
|
||||
item->setDisabled(true);
|
||||
}
|
||||
|
||||
|
@ -424,7 +418,7 @@ void PropertiesView::onPropertyUpdated(std::shared_ptr<Instance> inst, std::stri
|
|||
item->setData(1, Qt::DisplayRole, QString::fromStdString(currentValue.ToString()));
|
||||
}
|
||||
|
||||
if (meta.type.type == DATA_VALUE && meta.type.descriptor != &Color3::TYPE && (!meta.type.descriptor->fromString || meta.flags & PROP_READONLY)) {
|
||||
if (meta.type.descriptor != &Color3::TYPE && (!meta.type.descriptor->fromString || meta.flags & PROP_READONLY)) {
|
||||
item->setDisabled(true);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue