summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-04-13 01:33:46 +0200
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2016-04-25 22:24:17 +0200
commita9f2e3dfc70250c13df29091defa67fe19525f6b (patch)
treebf586ff96e647e1c291c9488499930d388888492 /src
parentdc3c159379939887835abff175200b9c4b7103cd (diff)
Implement ReferenceType type class with some rudimentary behaviour
Diffstat (limited to 'src')
-rw-r--r--src/core/model/Typesystem.cpp47
-rw-r--r--src/core/model/Typesystem.hpp78
2 files changed, 123 insertions, 2 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp
index 688cb60..6ae92b4 100644
--- a/src/core/model/Typesystem.cpp
+++ b/src/core/model/Typesystem.cpp
@@ -17,6 +17,7 @@
*/
#include "Typesystem.hpp"
+#include "Ontology.hpp"
#include <core/common/RttiBuilder.hpp>
#include <core/common/Utils.hpp>
@@ -640,6 +641,48 @@ bool StructType::hasAttribute(const std::string &name) const
return indexOf(name) >= 0;
}
+/* Class ReferenceType */
+
+ReferenceType::ReferenceType(Manager &mgr, const std::string &name,
+ Handle<Descriptor> descriptor)
+ : Type(mgr, name, nullptr, false), descriptor(acquire(descriptor))
+{
+}
+
+bool ReferenceType::doBuild(Variant &data, Logger &logger,
+ const MagicCallback &magicCallback) const
+{
+ // Null references are valid references
+ if (data.isNull()) {
+ return true;
+ }
+
+ if (data.isObject()) {
+ // TODO: Check: object is an instance of the entity descriptor
+ return true;
+ } else if (data.isString()) {
+ // TODO: Lookup referenced object
+ if (!Utils::isNamespacedIdentifier(data.asString())) {
+ throw LoggableException("Reference must be a valid identifier",
+ data);
+ }
+ }
+ return false;
+}
+
+bool ReferenceType::doCheckIsa(Handle<const Type> type) const
+{
+ // TODO: Implement correctly, check descriptor inheritance
+ return type->isa(&RttiTypes::ReferenceType);
+}
+
+Handle<Descriptor> ReferenceType::getDescriptor() { return descriptor; }
+
+void ReferenceType::setDescriptor(Handle<Descriptor> descriptor)
+{
+ this->descriptor = acquire(descriptor);
+}
+
/* Class ArrayType */
bool ArrayType::doBuild(Variant &data, Logger &logger,
@@ -811,6 +854,8 @@ const Rtti EnumType = RttiBuilder<ousia::EnumType>("EnumType").parent(&Type);
const Rtti StructType = RttiBuilder<ousia::StructType>("StructType")
.parent(&Type)
.composedOf(&Attribute);
+const Rtti ReferenceType =
+ RttiBuilder<ousia::ReferenceType>("ReferenceType").parent(&Type);
const Rtti ArrayType = RttiBuilder<ousia::ArrayType>("ArrayType").parent(&Type);
const Rtti UnknownType =
RttiBuilder<ousia::UnknownType>("UnknownType").parent(&Type);
@@ -823,4 +868,4 @@ const Rtti Typesystem =
const Rtti SystemTypesystem = RttiBuilder<ousia::SystemTypesystem>(
"SystemTypesystem").parent(&Typesystem);
}
-} \ No newline at end of file
+}
diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp
index ac29c81..ce3cee0 100644
--- a/src/core/model/Typesystem.hpp
+++ b/src/core/model/Typesystem.hpp
@@ -47,6 +47,7 @@ class CharReader;
class Rtti;
class Typesystem;
class SystemTypesystem;
+class Descriptor;
/**
* The abstract Type class represents a type descriptor. Each Type node is part
@@ -980,6 +981,76 @@ public:
};
/**
+ * The ReferenceType class represents a reference to an entity in the document.
+ * The type of the entity can be specified by the user in the form of a
+ * ontology descriptor.
+ */
+class ReferenceType : public Type {
+private:
+ /**
+ * Contains the reference at the descriptor element or nullptr if no such
+ * element was given (the reference is a general reference) or the reference
+ * type could not be resolved.
+ */
+ Owned<Descriptor> descriptor;
+
+protected:
+ /**
+ * Makes sure the given variant is either an array or a string that can be
+ * resolved to an object of the given structure type.
+ *
+ * @param data is a Variant containing the data that should be checked.
+ * @param logger is the Logger instance into which errors should be written.
+ * @return true if the conversion was successful, false otherwise.
+ */
+ bool doBuild(Variant &data, Logger &logger,
+ const MagicCallback &magicCallback) const override;
+
+ /**
+ * Returns true if this type is equivalent to another given ReferenceType,
+ * uses the inheritance hierarchy of the underlying descriptor.
+ *
+ * @param type is the type that should be checked for the "isa"
+ * relationship.
+ * @return true if the given type is equivalent to this type, false
+ * otherwise.
+ */
+ bool doCheckIsa(Handle<const Type> type) const override;
+
+public:
+ /**
+ * Constructor of the ReferenceType class.
+ *
+ * @param mgr is the parent Manager instance.
+ * @param name is the name of the type.
+ * @param descriptor is the entity descriptor specifying the ontological
+ * type of the reference objects.
+ */
+ ReferenceType(Manager &mgr, const std::string &name,
+ Handle<Descriptor> descriptor);
+
+ /**
+ * Create a new, empty variant containing an invalid (null) reference.
+ *
+ * @return a null reference.
+ */
+ Variant create() const override { return Variant{}; }
+
+ /**
+ * Returns the descriptor containing the ontological type of which an
+ * instance is being referenced.
+ *
+ * @return the descriptor or nullptr if no descriptor has been set.
+ */
+ Handle<Descriptor> getDescriptor();
+
+ /**
+ * Sets the descriptor to the given value.
+ */
+ void setDescriptor(Handle<Descriptor> descriptor);
+};
+
+/**
* The ArrayType class represents an array with elements of a fixed inner type.
* ArrayTypes are anonymous (they have an empty name) and always have the
* Typesystem instance of the inner type as parent. ArrayType instances are
@@ -1446,6 +1517,11 @@ extern const Rtti EnumType;
extern const Rtti StructType;
/**
+ * Type information for the ReferenceType class.
+ */
+extern const Rtti ReferenceType;
+
+/**
* Type information for the ArrayType class.
*/
extern const Rtti ArrayType;
@@ -1477,4 +1553,4 @@ extern const Rtti SystemTypesystem;
}
}
-#endif /* _OUSIA_MODEL_TYPESYSTEM_HPP_ */ \ No newline at end of file
+#endif /* _OUSIA_MODEL_TYPESYSTEM_HPP_ */