diff options
-rw-r--r-- | src/core/XML.cpp | 11 | ||||
-rw-r--r-- | src/core/common/Rtti.cpp | 10 | ||||
-rw-r--r-- | src/core/common/Rtti.hpp | 17 | ||||
-rw-r--r-- | src/core/managed/Manager.cpp | 14 | ||||
-rw-r--r-- | src/core/model/Node.cpp | 26 | ||||
-rw-r--r-- | src/core/model/Node.hpp | 7 | ||||
-rw-r--r-- | test/core/model/NodeTest.cpp | 5 |
7 files changed, 67 insertions, 23 deletions
diff --git a/src/core/XML.cpp b/src/core/XML.cpp index 528e1cd..475e345 100644 --- a/src/core/XML.cpp +++ b/src/core/XML.cpp @@ -19,6 +19,7 @@ #include <sstream> #include <core/common/Rtti.hpp> +#include <core/common/TypedRttiBuilder.hpp> #include "XML.hpp" @@ -115,10 +116,18 @@ void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) } } +static Variant getXmlElementName(const xml::Element *obj) +{ + return Variant::fromString(obj->name); +} + namespace RttiTypes { const Rtti<xml::Node> XMLNode = RttiBuilder("XMLNode"); const Rtti<xml::Element> XMLElement = - RttiBuilder("XMLElement").parent(&XMLNode).composedOf(&XMLNode); + TypedRttiBuilder<xml::Element>("XMLElement") + .parent(&XMLNode) + .composedOf(&XMLNode) + .property("name", {RttiTypes::String, getXmlElementName}); const Rtti<xml::Text> XMLText = RttiBuilder("XMLText").parent(&XMLNode); } } diff --git a/src/core/common/Rtti.cpp b/src/core/common/Rtti.cpp index 239f2b4..6809911 100644 --- a/src/core/common/Rtti.cpp +++ b/src/core/common/Rtti.cpp @@ -161,6 +161,16 @@ std::shared_ptr<PropertyDescriptor> RttiType::getProperty(const std::string &nam return it->second; } +bool RttiType::hasMethod(const std::string &name) const +{ + return methods.count(name) > 0; +} + +bool RttiType::hasProperty(const std::string &name) const +{ + return properties.count(name) > 0; +} + /* Constant initialization */ namespace RttiTypes { diff --git a/src/core/common/Rtti.hpp b/src/core/common/Rtti.hpp index 6449c75..3549474 100644 --- a/src/core/common/Rtti.hpp +++ b/src/core/common/Rtti.hpp @@ -435,6 +435,23 @@ public: */ std::shared_ptr<PropertyDescriptor> getProperty(const std::string &name) const; + /** + * Returns true if a method with the given name is registered for this type. + * + * @param name is the name of the method that should be looked up. + * @return true if a method with this name exists, false otherwise. + */ + bool hasMethod(const std::string &name) const; + + /** + * Returns true if a property with the given name is registered for this + * type. + * + * @param name is the name of the property that should be looked up. + * @return true if a property with this name exists, false otherwise. + */ + bool hasProperty(const std::string &name) const; + }; /** diff --git a/src/core/managed/Manager.cpp b/src/core/managed/Manager.cpp index c6b162b..60f7332 100644 --- a/src/core/managed/Manager.cpp +++ b/src/core/managed/Manager.cpp @@ -27,8 +27,7 @@ #include <iostream> #include <fstream> #include <core/common/Rtti.hpp> -#include <core/model/Node.hpp> -#include <core/XML.hpp> +#include <core/common/Property.hpp> #endif namespace ousia { @@ -595,12 +594,11 @@ void Manager::exportGraphviz(const char *filename) // Read type information and Node name (if available) const RttiType &type = objectPtr->type(); const std::string &typeName = type.name; - std::string name = ""; - if (type.isa(RttiTypes::Node)) { - name = dynamic_cast<const Node *>(objectPtr)->getName(); - } - if (type.isa(RttiTypes::XMLElement)) { - name = dynamic_cast<const xml::Element *>(objectPtr)->name; + + // Fetch the name of the object if the object has a "name" property + std::string name; + if (type.hasProperty("name")) { + name = type.getProperty("name")->get(objectPtr).toString(); } // Print the node diff --git a/src/core/model/Node.cpp b/src/core/model/Node.cpp index bac420b..7f684ce 100644 --- a/src/core/model/Node.cpp +++ b/src/core/model/Node.cpp @@ -21,6 +21,7 @@ #include <core/common/Exceptions.hpp> #include <core/common/Rtti.hpp> +#include <core/common/TypedRttiBuilder.hpp> #include "Node.hpp" @@ -351,8 +352,29 @@ std::vector<ResolutionResult> Node::resolve(const std::string &name, return resolve(std::vector<std::string>{name}, type); } -/* RTTI type registrations */ +/* Reflection Methods */ + +static Variant getNodeName(const Node *obj) +{ + return Variant::fromString(obj->getName()); +} + +static void setNodeName(const Variant &value, Node *obj) +{ + obj->setName(value.asString()); +} + +static Variant getNodeParent(const Node *obj) +{ + return Variant::fromObject(obj->getParent()); +} -const Rtti<Node> RttiTypes::Node{"Node"}; +/* RTTI type registrations */ +namespace RttiTypes { +const Rtti<ousia::Node> Node = + TypedRttiBuilder<ousia::Node>("Node") + .property("name", {RttiTypes::String, getNodeName, setNodeName}) + .property("parent", {Node, getNodeParent}); +} } diff --git a/src/core/model/Node.hpp b/src/core/model/Node.hpp index c24aacb..68a6ec7 100644 --- a/src/core/model/Node.hpp +++ b/src/core/model/Node.hpp @@ -311,13 +311,6 @@ public: bool hasName() const { return !name.empty(); } /** - * Sets the parent node. - * - * @param parent is a Handle to the parent node. - */ - void setParent(Handle<Node> parent) { this->parent = acquire(parent); } - - /** * Returns a handle to the parent node of the Node instance. * * @return a handle to the root node. diff --git a/test/core/model/NodeTest.cpp b/test/core/model/NodeTest.cpp index 2a63acc..973ce22 100644 --- a/test/core/model/NodeTest.cpp +++ b/test/core/model/NodeTest.cpp @@ -73,11 +73,6 @@ TEST(Node, isRoot) ASSERT_TRUE(n1->isRoot()); ASSERT_TRUE(n2->isRoot()); ASSERT_FALSE(n3->isRoot()); - - n2->setParent(n1); - ASSERT_TRUE(n1->isRoot()); - ASSERT_FALSE(n2->isRoot()); - ASSERT_FALSE(n3->isRoot()); } TEST(Node, resolveCompositaSimple) |