summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-12 19:47:43 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-12 19:47:43 +0100
commitfa387f9bef0d1a70a4b40c28c5cf9d661c421f73 (patch)
treec1857d0d7fc0a8db8c1f3a841913b1f496d8e661 /src
parent2b15fcab4b81fa8a854e724c48ee9c771fb126f8 (diff)
parent55f943ba1b31542157b984b5955b91261c280f46 (diff)
Merge branch 'master' of somweyr.de:ousia
Conflicts: application/src/core/model/Document.hpp
Diffstat (limited to 'src')
-rw-r--r--src/core/common/Argument.cpp4
-rw-r--r--src/core/common/Function.hpp23
-rw-r--r--src/core/common/Variant.cpp6
-rw-r--r--src/core/common/Variant.hpp15
-rw-r--r--src/core/common/VariantConverter.cpp28
-rw-r--r--src/core/model/Document.cpp12
-rw-r--r--src/core/model/Document.hpp72
7 files changed, 103 insertions, 57 deletions
diff --git a/src/core/common/Argument.cpp b/src/core/common/Argument.cpp
index 78dd4b4..3461868 100644
--- a/src/core/common/Argument.cpp
+++ b/src/core/common/Argument.cpp
@@ -91,13 +91,13 @@ Argument Argument::String(std::string name,
Argument Argument::Object(std::string name, const RttiType &type)
{
- return Argument(std::move(name), type, RttiTypes::None, nullptr, false);
+ return Argument(std::move(name), type, RttiTypes::None, Variant::fromObject(nullptr), false);
}
Argument Argument::Object(std::string name, const RttiType &type,
std::nullptr_t)
{
- return Argument(std::move(name), type, RttiTypes::None, nullptr, true);
+ return Argument(std::move(name), type, RttiTypes::None, Variant::fromObject(nullptr), true);
}
Argument Argument::Function(std::string name)
diff --git a/src/core/common/Function.hpp b/src/core/common/Function.hpp
index 04030c8..79ee6b9 100644
--- a/src/core/common/Function.hpp
+++ b/src/core/common/Function.hpp
@@ -42,7 +42,7 @@ namespace ousia {
*/
class Function {
protected:
- Function() {};
+ Function(){};
public:
Function(const Function &) = delete;
@@ -62,6 +62,23 @@ public:
};
/**
+ * Function doing nothing. Instances of this class are used as default values
+ * for instances of the Function class.
+ */
+class FunctionStub : public Function {
+public:
+ /**
+ * Constructor of the FunctionStub class.
+ */
+ FunctionStub() {}
+
+ Variant call(const Variant::arrayType &, void *) const override
+ {
+ return nullptr;
+ }
+};
+
+/**
* The Method class refers to a method in the C++ code, belonging to an object
* of a certain type T.
*
@@ -93,7 +110,7 @@ public:
*
* @param method is a pointer at the C++ function that should be called.
*/
- Method(Callback method) : method(method) {};
+ Method(Callback method) : method(method){};
/**
* Calls the underlying method.
@@ -106,7 +123,7 @@ public:
void *thisRef = nullptr) const override
{
// Call the method
- return method(args, static_cast<T*>(thisRef));
+ return method(args, static_cast<T *>(thisRef));
}
};
}
diff --git a/src/core/common/Variant.cpp b/src/core/common/Variant.cpp
index e199bc7..81e6339 100644
--- a/src/core/common/Variant.cpp
+++ b/src/core/common/Variant.cpp
@@ -158,8 +158,10 @@ const RttiType& Variant::getRttiType() const
return RttiTypes::Map;
case VariantType::FUNCTION:
return RttiTypes::Function;
- case VariantType::OBJECT:
- return asObject()->type();
+ case VariantType::OBJECT: {
+ Variant::objectType o = asObject();
+ return (o == nullptr) ? RttiTypes::Nullptr : o->type();
+ }
}
return RttiTypes::None;
}
diff --git a/src/core/common/Variant.hpp b/src/core/common/Variant.hpp
index 27dfda8..e5bca4d 100644
--- a/src/core/common/Variant.hpp
+++ b/src/core/common/Variant.hpp
@@ -342,6 +342,19 @@ public:
Variant(mapType m) : ptrVal(nullptr) { setMap(std::move(m)); }
/**
+ * Named constructor for object values.
+ *
+ * @param o is an object that can be converted to a Rooted handle.
+ */
+ template<class T>
+ static Variant fromObject(T o)
+ {
+ Variant res;
+ res.setObject(o);
+ return res;
+ }
+
+ /**
* Named constructor for function values.
*
* @param f is a shared pointer pointing at the Function instance.
@@ -900,7 +913,7 @@ public:
* to a Rooted handle.
*/
template <class T>
- void setObject(Handle<T> o)
+ void setObject(T o)
{
destroy();
type = VariantType::OBJECT;
diff --git a/src/core/common/VariantConverter.cpp b/src/core/common/VariantConverter.cpp
index 1f5f514..1c23c43 100644
--- a/src/core/common/VariantConverter.cpp
+++ b/src/core/common/VariantConverter.cpp
@@ -243,8 +243,11 @@ bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode)
// Print object address and type
Variant::objectType obj = var.asObject();
std::stringstream ss;
- ss << "<object " << obj.get() << " (" << obj->type().name << ")"
- << ">";
+ ss << "<object " << obj.get();
+ if (obj.get() != nullptr) {
+ ss << " (" << obj->type().name << ")";
+ }
+ ss << ">";
var = ss.str().c_str();
return true;
}
@@ -290,11 +293,7 @@ bool VariantConverter::toArray(Variant &var, const RttiType &innerType,
for (Variant &v : var.asArray()) {
res = convert(v, innerType, RttiTypes::None, logger, mode) & res;
}
-
- // Return on successful conversion, otherwise output the default value
- if (res) {
- return true;
- }
+ return res;
}
// No conversion possible, assign the default value and log an error
@@ -321,11 +320,7 @@ bool VariantConverter::toMap(Variant &var, const RttiType &innerType,
res = convert(e.second, innerType, RttiTypes::None, logger, mode) &
res;
}
-
- // Return on successful conversion, otherwise output the default value
- if (res) {
- return true;
- }
+ return res;
}
// No conversion possible, assign the default value and log an error
@@ -341,9 +336,8 @@ bool VariantConverter::toFunction(Variant &var, Logger &logger)
}
// No conversion possible, assign the default value and log an error
- logger.error(msgUnexpectedType(var, VariantType::MAP));
- var.setFunction(std::shared_ptr<Function>{new Method<void>([](
- const Variant::arrayType &args, void *thisRef) { return Variant{}; })});
+ logger.error(msgUnexpectedType(var, VariantType::FUNCTION));
+ var.setFunction(std::shared_ptr<Function>{new FunctionStub()});
return false;
}
@@ -383,7 +377,7 @@ bool VariantConverter::convert(Variant &var, const RttiType &type,
// obviously asked for a managed object.
if (!var.isObject()) {
logger.error(msgUnexpectedType(var, VariantType::OBJECT));
- var.setNull();
+ var.setObject(nullptr);
return false;
}
@@ -391,7 +385,7 @@ bool VariantConverter::convert(Variant &var, const RttiType &type,
if (!var.getRttiType().isa(type)) {
logger.error(std::string("Expected object of type ") + type.name +
" but got object of type " + var.getRttiType().name);
- var.setNull();
+ var.setObject(nullptr);
return false;
}
return true;
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp
index 723aafd..022b91c 100644
--- a/src/core/model/Document.cpp
+++ b/src/core/model/Document.cpp
@@ -103,19 +103,17 @@ void Document::continueResolve(ResolutionState &state)
/* Type registrations */
namespace RttiTypes {
-const Rtti<model::DocumentEntity> DocumentEntity =
- RttiBuilder("DocumentEntity").parent(&Node);
const Rtti<model::Document> Document =
RttiBuilder("Document").parent(&Node).composedOf(
{&AnnotationEntity, &StructuredEntity});
-const Rtti<model::AnnotationEntity> AnnotationEntity =
- RttiBuilder("AnnotationEntity").parent(&DocumentEntity).composedOf(
- &StructuredEntity);
const Rtti<model::StructureNode> StructureNode =
RttiBuilder("StructureNode").parent(&Node);
+const Rtti<model::AnnotationEntity> AnnotationEntity =
+ RttiBuilder("AnnotationEntity").parent(&Node).composedOf(
+ &StructureNode);
const Rtti<model::StructuredEntity> StructuredEntity =
- RttiBuilder("StructuredEntity").parent(&DocumentEntity).parent(&StructureNode).composedOf(
- {&StructuredEntity, &Anchor, &DocumentPrimitive});
+ RttiBuilder("StructuredEntity").parent(&StructureNode).composedOf(
+ {&StructureNode});
const Rtti<model::DocumentPrimitive> DocumentPrimitive =
RttiBuilder("DocumentPrimitive").parent(&StructureNode);
const Rtti<model::AnnotationEntity::Anchor> Anchor =
diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp
index 7f365b7..d22196a 100644
--- a/src/core/model/Document.hpp
+++ b/src/core/model/Document.hpp
@@ -138,7 +138,7 @@ class StructureNode;
* name.
*
*/
-class DocumentEntity : public virtual Node {
+class DocumentEntity {
private:
Owned<Descriptor> descriptor;
const Variant attributes;
@@ -151,11 +151,22 @@ private:
bool enforce) const;
public:
- DocumentEntity(Manager &mgr, Handle<Node> parent,
- Handle<Descriptor> descriptor, Variant attributes = {},
- std::string name = "")
- : Node(mgr, std::move(name), parent),
- descriptor(acquire(descriptor)),
+ /**
+ * The constructor for a DocumentEntity. Node that this does not inherit
+ * from Node. Therefore we need to have a handle to the child Node instance
+ * to create NodeVectors and Owned references.
+ *
+ * @param owner is a handle to the child instance
+ * (e.g. StructuredEntity), such that the fields vectors
+ * and the descriptor reference can be obtained.
+ * @param descriptor is the Descriptor for this DocumentEntity, which will
+ * transformed to an Owned reference of the given owner.
+ * @param attributes is a Map Variant adhering to the attribute StructType
+ * in the given descriptor.
+ */
+ DocumentEntity(Handle<Node> owner, Handle<Descriptor> descriptor,
+ Variant attributes = {})
+ : descriptor(owner->acquire(descriptor)),
attributes(std::move(attributes))
{
// TODO: Validation at construction time?
@@ -163,13 +174,27 @@ public:
if (!descriptor.isNull()) {
for (size_t f = 0; f < descriptor->getFieldDescriptors().size();
f++) {
- fields.push_back(NodeVector<StructureNode>(this));
+ fields.push_back(NodeVector<StructureNode>(owner));
}
}
}
+ virtual ~DocumentEntity(){};
+
+ /**
+ * Returns the Descriptor for this DocumentEntity.
+ *
+ * @return the Descriptor for this DocumentEntity.
+ */
Rooted<Descriptor> getDescriptor() const { return descriptor; }
+ /**
+ * Returns a Map Variant adhering to the attribute StructType in the given
+ * descriptor.
+ *
+ * @return a Map Variant adhering to the attribute StructType in the given
+ * descriptor.
+ */
Variant getAttributes() const { return attributes; }
/**
@@ -303,7 +328,7 @@ public:
* A StructureNode is a Node of the StructureTree of the document. This is a
* common superclass for StructuredEntity, Anchor and DocumentPrimitive.
*/
-class StructureNode : public virtual Node {
+class StructureNode : public Node {
public:
StructureNode(Manager &mgr, std::string name, Handle<Node> parent)
: Node(mgr, std::move(name), parent)
@@ -315,15 +340,13 @@ public:
* A StructuredEntity is an instance of a StructuredClass. For more
* information please refer to the header documentation above.
*/
-class StructuredEntity : public DocumentEntity, public StructureNode {
+class StructuredEntity : public StructureNode, public DocumentEntity {
public:
StructuredEntity(Manager &mgr, Handle<Node> parent,
Handle<StructuredClass> descriptor, Variant attributes,
std::string name = "")
- : Node(mgr, std::move(name), parent),
- DocumentEntity(mgr, parent, descriptor, std::move(attributes),
- std::move(name)),
- StructureNode(mgr, std::move(name), parent)
+ : StructureNode(mgr, std::move(name), parent),
+ DocumentEntity(this, descriptor, std::move(attributes))
{
}
};
@@ -338,17 +361,17 @@ private:
Variant content;
public:
- DocumentPrimitive(Manager &mgr, Handle<DocumentEntity> parent,
- Variant content = {})
- : Node(mgr, parent),
- StructureNode(mgr, "", parent),
- content(content)
+ DocumentPrimitive(Manager &mgr, Handle<Node> parent, Variant content = {})
+ : StructureNode(mgr, "", parent), content(content)
{
}
+ /**
+ * Returns the content of this DocumentPrimitive.
+ *
+ * @return the content of this DocumentPrimitive.
+ */
Variant getContent() const { return content; }
-
- // TODO: Override such methods like "getField" to disable them?
};
/**
@@ -371,7 +394,7 @@ public:
* the two text exerpts "emphasized" and "and" separately.
*
*/
-class AnnotationEntity : public DocumentEntity {
+class AnnotationEntity : public Node, public DocumentEntity {
public:
/**
* An Anchor is an elementary StructuredEntity without any children that
@@ -387,9 +410,8 @@ public:
* not the AnnotationEntity that references this Anchor.
* @param name is the Anchor id.
*/
- Anchor(Manager &mgr, std::string name, Handle<DocumentEntity> parent)
- : Node(mgr, std::move(name), parent),
- StructureNode(mgr, std::move(name), parent)
+ Anchor(Manager &mgr, std::string name, Handle<Node> parent)
+ : StructureNode(mgr, std::move(name), parent)
{
}
};
@@ -419,7 +441,7 @@ public:
Handle<Anchor> end, Variant attributes = {},
std::string name = "")
: Node(mgr, std::move(name), parent),
- DocumentEntity(mgr, parent, descriptor, attributes, std::move(name)),
+ DocumentEntity(this, descriptor, attributes),
start(acquire(start)),
end(acquire(end))
{