refactor(error): replaced tl::expected with result
This commit is contained in:
parent
58b53a0f02
commit
166c5c5ff8
7 changed files with 51 additions and 2518 deletions
|
@ -16,3 +16,13 @@ class ServiceAlreadyExists : public Error {
|
||||||
public:
|
public:
|
||||||
inline ServiceAlreadyExists(std::string className) : Error("ServiceAlreadyExists", "Service " + className + " is already inserted") {}
|
inline ServiceAlreadyExists(std::string className) : Error("ServiceAlreadyExists", "Service " + className + " is already inserted") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MemberNotFound : public Error {
|
||||||
|
public:
|
||||||
|
inline MemberNotFound(std::string className, std::string memberName) : Error("MemberNotFound", "Could not find member '" + memberName + "' in class " + className) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class AssignToReadOnlyMember : public Error {
|
||||||
|
public:
|
||||||
|
inline AssignToReadOnlyMember(std::string className, std::string memberName) : Error("AssignToReadOnlyMember", "Attempt to assign value to read-only member '" + memberName + "' in class " + className) {}
|
||||||
|
};
|
|
@ -2,6 +2,7 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "datatypes/meta.h"
|
#include "datatypes/meta.h"
|
||||||
#include "datatypes/base.h"
|
#include "datatypes/base.h"
|
||||||
|
#include "error/instance.h"
|
||||||
#include "objects/base/member.h"
|
#include "objects/base/member.h"
|
||||||
#include "objects/meta.h"
|
#include "objects/meta.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
@ -157,24 +158,27 @@ void Instance::OnAncestryChanged(std::optional<std::shared_ptr<Instance>> child,
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
|
|
||||||
tl::expected<Data::Variant, MemberNotFound> Instance::GetPropertyValue(std::string name) {
|
result<Data::Variant, MemberNotFound> Instance::GetPropertyValue(std::string name) {
|
||||||
auto meta = GetPropertyMeta(name);
|
auto meta_ = GetPropertyMeta(name);
|
||||||
if (!meta) return tl::make_unexpected(MemberNotFound());
|
if (!meta_) return MemberNotFound(GetClass()->className, name);
|
||||||
|
auto meta = meta_.expect();
|
||||||
|
|
||||||
return meta->codec.read(meta->backingField);
|
return meta.codec.read(meta.backingField);
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<void, MemberNotFound> Instance::SetPropertyValue(std::string name, Data::Variant value) {
|
fallible<MemberNotFound, AssignToReadOnlyMember> Instance::SetPropertyValue(std::string name, Data::Variant value) {
|
||||||
auto meta = GetPropertyMeta(name);
|
auto meta_ = GetPropertyMeta(name);
|
||||||
if (!meta || meta->flags & PROP_READONLY) return tl::make_unexpected(MemberNotFound());
|
if (!meta_) return MemberNotFound(GetClass()->className, name);
|
||||||
|
auto meta = meta_.expect();
|
||||||
|
if (meta.flags & PROP_READONLY) AssignToReadOnlyMember(GetClass()->className, name);
|
||||||
|
|
||||||
meta->codec.write(value, meta->backingField);
|
meta.codec.write(value, meta.backingField);
|
||||||
if (meta->updateCallback) meta->updateCallback.value()(name);
|
if (meta.updateCallback) meta.updateCallback.value()(name);
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<PropertyMeta, MemberNotFound> Instance::GetPropertyMeta(std::string name) {
|
result<PropertyMeta, MemberNotFound> Instance::GetPropertyMeta(std::string name) {
|
||||||
MemberMap* current = &*memberMap;
|
MemberMap* current = &*memberMap;
|
||||||
while (true) {
|
while (true) {
|
||||||
// We look for the property in current member map
|
// We look for the property in current member map
|
||||||
|
@ -186,7 +190,7 @@ tl::expected<PropertyMeta, MemberNotFound> Instance::GetPropertyMeta(std::string
|
||||||
|
|
||||||
// It is not found, If there are no other maps to search, return null
|
// It is not found, If there are no other maps to search, return null
|
||||||
if (!current->super.has_value())
|
if (!current->super.has_value())
|
||||||
return tl::make_unexpected(MemberNotFound());
|
return MemberNotFound(GetClass()->className, name);
|
||||||
|
|
||||||
// Search in the parent
|
// Search in the parent
|
||||||
current = current->super->get();
|
current = current->super->get();
|
||||||
|
@ -223,12 +227,12 @@ void Instance::Serialize(pugi::xml_node parent) {
|
||||||
// Add properties
|
// Add properties
|
||||||
pugi::xml_node propertiesNode = node.append_child("Properties");
|
pugi::xml_node propertiesNode = node.append_child("Properties");
|
||||||
for (std::string name : GetProperties()) {
|
for (std::string name : GetProperties()) {
|
||||||
PropertyMeta meta = GetPropertyMeta(name).value();
|
PropertyMeta meta = GetPropertyMeta(name).expect("Meta of declared property is missing");
|
||||||
if (meta.flags & PropertyFlags::PROP_NOSAVE) continue; // This property should not be serialized. Skip...
|
if (meta.flags & PropertyFlags::PROP_NOSAVE) continue; // This property should not be serialized. Skip...
|
||||||
|
|
||||||
pugi::xml_node propertyNode = propertiesNode.append_child(meta.type->name);
|
pugi::xml_node propertyNode = propertiesNode.append_child(meta.type->name);
|
||||||
propertyNode.append_attribute("name").set_value(name);
|
propertyNode.append_attribute("name").set_value(name);
|
||||||
GetPropertyValue(name)->Serialize(propertyNode);
|
GetPropertyValue(name).expect("Declared property is missing").Serialize(propertyNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add children
|
// Add children
|
||||||
|
@ -254,12 +258,12 @@ result<InstanceRef, NoSuchInstance> Instance::Deserialize(pugi::xml_node node) {
|
||||||
for (pugi::xml_node propertyNode : propertiesNode) {
|
for (pugi::xml_node propertyNode : propertiesNode) {
|
||||||
std::string propertyName = propertyNode.attribute("name").value();
|
std::string propertyName = propertyNode.attribute("name").value();
|
||||||
auto meta_ = object->GetPropertyMeta(propertyName);
|
auto meta_ = object->GetPropertyMeta(propertyName);
|
||||||
if (!meta_.has_value()) {
|
if (!meta_) {
|
||||||
Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
|
Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Data::Variant value = Data::Variant::Deserialize(propertyNode);
|
Data::Variant value = Data::Variant::Deserialize(propertyNode);
|
||||||
object->SetPropertyValue(propertyName, value);
|
object->SetPropertyValue(propertyName, value).expect("Declared property was missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read children
|
// Read children
|
||||||
|
|
|
@ -9,8 +9,6 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
// #include <../../include/expected.hpp>
|
|
||||||
#include <expected.hpp>
|
|
||||||
#include <pugixml.hpp>
|
#include <pugixml.hpp>
|
||||||
|
|
||||||
#include "error/instance.h"
|
#include "error/instance.h"
|
||||||
|
@ -92,10 +90,9 @@ public:
|
||||||
inline void AddChild(std::shared_ptr<Instance> object) { object->SetParent(this->shared_from_this()); }
|
inline void AddChild(std::shared_ptr<Instance> object) { object->SetParent(this->shared_from_this()); }
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
// Do I like using expected?
|
result<Data::Variant, MemberNotFound> GetPropertyValue(std::string name);
|
||||||
tl::expected<Data::Variant, MemberNotFound> GetPropertyValue(std::string name);
|
fallible<MemberNotFound, AssignToReadOnlyMember> SetPropertyValue(std::string name, Data::Variant value);
|
||||||
tl::expected<void, MemberNotFound> SetPropertyValue(std::string name, Data::Variant value);
|
result<PropertyMeta, MemberNotFound> GetPropertyMeta(std::string name);
|
||||||
tl::expected<PropertyMeta, MemberNotFound> GetPropertyMeta(std::string name);
|
|
||||||
// Returning a list of property names feels kinda janky. Is this really the way to go?
|
// Returning a list of property names feels kinda janky. Is this really the way to go?
|
||||||
std::vector<std::string> GetProperties();
|
std::vector<std::string> GetProperties();
|
||||||
|
|
||||||
|
|
|
@ -76,5 +76,3 @@ struct MemberMap {
|
||||||
std::optional<std::unique_ptr<MemberMap>> super;
|
std::optional<std::unique_ptr<MemberMap>> super;
|
||||||
std::map<std::string, PropertyMeta> members;
|
std::map<std::string, PropertyMeta> members;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MemberNotFound {};
|
|
|
@ -82,12 +82,12 @@ void DataModel::DeserializeService(pugi::xml_node node) {
|
||||||
for (pugi::xml_node propertyNode : propertiesNode) {
|
for (pugi::xml_node propertyNode : propertiesNode) {
|
||||||
std::string propertyName = propertyNode.attribute("name").value();
|
std::string propertyName = propertyNode.attribute("name").value();
|
||||||
auto meta_ = object->GetPropertyMeta(propertyName);
|
auto meta_ = object->GetPropertyMeta(propertyName);
|
||||||
if (!meta_.has_value()) {
|
if (!meta_) {
|
||||||
Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
|
Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Data::Variant value = Data::Variant::Deserialize(propertyNode);
|
Data::Variant value = Data::Variant::Deserialize(propertyNode);
|
||||||
object->SetPropertyValue(propertyName, value);
|
object->SetPropertyValue(propertyName, value).expect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add children
|
// Add children
|
||||||
|
|
|
@ -41,8 +41,8 @@ public:
|
||||||
|
|
||||||
std::string propertyName = !isComposite ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
std::string propertyName = !isComposite ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
||||||
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
||||||
PropertyMeta meta = inst->GetPropertyMeta(propertyName).value();
|
PropertyMeta meta = inst->GetPropertyMeta(propertyName).expect();
|
||||||
Data::Variant currentValue = inst->GetPropertyValue(propertyName).value();
|
Data::Variant currentValue = inst->GetPropertyValue(propertyName).expect();
|
||||||
|
|
||||||
if (isComposite) {
|
if (isComposite) {
|
||||||
if (meta.type == &Data::Vector3::TYPE) {
|
if (meta.type == &Data::Vector3::TYPE) {
|
||||||
|
@ -107,8 +107,8 @@ public:
|
||||||
|
|
||||||
std::string propertyName = !index.parent().parent().isValid() ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
std::string propertyName = !index.parent().parent().isValid() ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
||||||
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
||||||
PropertyMeta meta = inst->GetPropertyMeta(propertyName).value();
|
PropertyMeta meta = inst->GetPropertyMeta(propertyName).expect();
|
||||||
Data::Variant currentValue = inst->GetPropertyValue(propertyName).value();
|
Data::Variant currentValue = inst->GetPropertyValue(propertyName).expect();
|
||||||
|
|
||||||
if (isComposite) {
|
if (isComposite) {
|
||||||
if (meta.type == &Data::Vector3::TYPE) {
|
if (meta.type == &Data::Vector3::TYPE) {
|
||||||
|
@ -159,19 +159,19 @@ public:
|
||||||
|
|
||||||
std::string propertyName = !index.parent().parent().isValid() ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
std::string propertyName = !index.parent().parent().isValid() ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString()
|
||||||
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
: view->itemFromIndex(index.parent())->data(0, Qt::DisplayRole).toString().toStdString();
|
||||||
PropertyMeta meta = inst->GetPropertyMeta(propertyName).value();
|
PropertyMeta meta = inst->GetPropertyMeta(propertyName).expect();
|
||||||
|
|
||||||
if (isComposite) {
|
if (isComposite) {
|
||||||
if (meta.type == &Data::Vector3::TYPE) {
|
if (meta.type == &Data::Vector3::TYPE) {
|
||||||
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
||||||
float value = spinBox->value();
|
float value = spinBox->value();
|
||||||
|
|
||||||
Data::Vector3 prev = inst->GetPropertyValue(propertyName).value().get<Data::Vector3>();
|
Data::Vector3 prev = inst->GetPropertyValue(propertyName).expect().get<Data::Vector3>();
|
||||||
Data::Vector3 newVector = componentName == "X" ? Data::Vector3(value, prev.Y(), prev.Z())
|
Data::Vector3 newVector = componentName == "X" ? Data::Vector3(value, prev.Y(), prev.Z())
|
||||||
: componentName == "Y" ? Data::Vector3(prev.X(), value, prev.Z())
|
: componentName == "Y" ? Data::Vector3(prev.X(), value, prev.Z())
|
||||||
: componentName == "Z" ? Data::Vector3(prev.X(), prev.Y(), value) : prev;
|
: componentName == "Z" ? Data::Vector3(prev.X(), prev.Y(), value) : prev;
|
||||||
|
|
||||||
inst->SetPropertyValue(propertyName, newVector);
|
inst->SetPropertyValue(propertyName, newVector).expect();
|
||||||
view->rebuildCompositeProperty(view->itemFromIndex(index.parent()), &Data::Vector3::TYPE, newVector);
|
view->rebuildCompositeProperty(view->itemFromIndex(index.parent()), &Data::Vector3::TYPE, newVector);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -182,24 +182,24 @@ public:
|
||||||
if (meta.type == &Data::Float::TYPE) {
|
if (meta.type == &Data::Float::TYPE) {
|
||||||
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
QDoubleSpinBox* spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
|
||||||
|
|
||||||
inst->SetPropertyValue(propertyName, Data::Float((float)spinBox->value()));
|
inst->SetPropertyValue(propertyName, Data::Float((float)spinBox->value())).expect();
|
||||||
model->setData(index, spinBox->value());
|
model->setData(index, spinBox->value());
|
||||||
} else if (meta.type == &Data::Int::TYPE) {
|
} else if (meta.type == &Data::Int::TYPE) {
|
||||||
QSpinBox* spinBox = dynamic_cast<QSpinBox*>(editor);
|
QSpinBox* spinBox = dynamic_cast<QSpinBox*>(editor);
|
||||||
|
|
||||||
inst->SetPropertyValue(propertyName, Data::Int((float)spinBox->value()));
|
inst->SetPropertyValue(propertyName, Data::Int((float)spinBox->value())).expect();
|
||||||
model->setData(index, spinBox->value());
|
model->setData(index, spinBox->value());
|
||||||
} else if (meta.type == &Data::String::TYPE) {
|
} else if (meta.type == &Data::String::TYPE) {
|
||||||
QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
|
QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
|
||||||
|
|
||||||
inst->SetPropertyValue(propertyName, Data::String(lineEdit->text().toStdString()));
|
inst->SetPropertyValue(propertyName, Data::String(lineEdit->text().toStdString())).expect();
|
||||||
model->setData(index, lineEdit->text());
|
model->setData(index, lineEdit->text());
|
||||||
} else if (meta.type == &Data::Color3::TYPE) {
|
} else if (meta.type == &Data::Color3::TYPE) {
|
||||||
QColorDialog* colorDialog = dynamic_cast<QColorDialog*>(editor);
|
QColorDialog* colorDialog = dynamic_cast<QColorDialog*>(editor);
|
||||||
|
|
||||||
QColor color = colorDialog->currentColor();
|
QColor color = colorDialog->currentColor();
|
||||||
Data::Color3 color3(color.redF(), color.greenF(), color.blueF());
|
Data::Color3 color3(color.redF(), color.greenF(), color.blueF());
|
||||||
inst->SetPropertyValue(propertyName, color3);
|
inst->SetPropertyValue(propertyName, color3).expect();
|
||||||
model->setData(index, QString::fromStdString(color3.ToString()), Qt::DisplayRole);
|
model->setData(index, QString::fromStdString(color3.ToString()), Qt::DisplayRole);
|
||||||
model->setData(index, color, Qt::DecorationRole);
|
model->setData(index, color, Qt::DecorationRole);
|
||||||
} else if (meta.type->fromString) {
|
} else if (meta.type->fromString) {
|
||||||
|
@ -207,7 +207,7 @@ public:
|
||||||
|
|
||||||
std::optional<Data::Variant> parsedResult = meta.type->fromString(lineEdit->text().toStdString());
|
std::optional<Data::Variant> parsedResult = meta.type->fromString(lineEdit->text().toStdString());
|
||||||
if (!parsedResult) return;
|
if (!parsedResult) return;
|
||||||
inst->SetPropertyValue(propertyName, parsedResult.value());
|
inst->SetPropertyValue(propertyName, parsedResult.value()).expect();
|
||||||
model->setData(index, QString::fromStdString(parsedResult.value().ToString()));
|
model->setData(index, QString::fromStdString(parsedResult.value().ToString()));
|
||||||
view->rebuildCompositeProperty(view->itemFromIndex(index), meta.type, parsedResult.value());
|
view->rebuildCompositeProperty(view->itemFromIndex(index), meta.type, parsedResult.value());
|
||||||
}
|
}
|
||||||
|
@ -283,8 +283,8 @@ void PropertiesView::setSelected(std::optional<InstanceRef> instance) {
|
||||||
std::vector<std::string> properties = inst->GetProperties();
|
std::vector<std::string> properties = inst->GetProperties();
|
||||||
|
|
||||||
for (std::string property : properties) {
|
for (std::string property : properties) {
|
||||||
PropertyMeta meta = inst->GetPropertyMeta(property).value();
|
PropertyMeta meta = inst->GetPropertyMeta(property).expect();
|
||||||
Data::Variant currentValue = inst->GetPropertyValue(property).value();
|
Data::Variant currentValue = inst->GetPropertyValue(property).expect();
|
||||||
|
|
||||||
if (meta.type == &Data::CFrame::TYPE) continue;
|
if (meta.type == &Data::CFrame::TYPE) continue;
|
||||||
|
|
||||||
|
@ -330,10 +330,10 @@ void PropertiesView::propertyChanged(QTreeWidgetItem *item, int column) {
|
||||||
InstanceRef inst = currentInstance->lock();
|
InstanceRef inst = currentInstance->lock();
|
||||||
|
|
||||||
std::string propertyName = item->data(0, Qt::DisplayRole).toString().toStdString();
|
std::string propertyName = item->data(0, Qt::DisplayRole).toString().toStdString();
|
||||||
PropertyMeta meta = inst->GetPropertyMeta(propertyName).value();
|
PropertyMeta meta = inst->GetPropertyMeta(propertyName).expect();
|
||||||
|
|
||||||
if (meta.type == &Data::Bool::TYPE) {
|
if (meta.type == &Data::Bool::TYPE) {
|
||||||
inst->SetPropertyValue(propertyName, Data::Bool(item->checkState(1)));
|
inst->SetPropertyValue(propertyName, Data::Bool(item->checkState(1))).expect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2476
include/expected.hpp
2476
include/expected.hpp
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue