syndication/rdf
statement.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "statement.h"
00024 #include "literal.h"
00025 #include "model.h"
00026 #include "model_p.h"
00027 #include "property.h"
00028 #include "resource.h"
00029
00030 #include <QtCore/QString>
00031
00032 #include <boost/weak_ptr.hpp>
00033
00034 using namespace boost;
00035
00036 namespace Syndication {
00037 namespace RDF {
00038
00039 class Statement::StatementPrivate
00040 {
00041 public:
00042
00043 uint subjectID;
00044 uint predicateID;
00045 uint objectID;
00046 weak_ptr<Model::ModelPrivate> model;
00047
00048 bool operator==(const StatementPrivate& other) const
00049 {
00050
00051 return subjectID == other.subjectID &&
00052 predicateID == other.predicateID &&
00053 objectID == other.objectID;
00054 }
00055 };
00056
00057 Statement::Statement() : d(new StatementPrivate)
00058 {
00059 d->subjectID = 0;
00060 d->predicateID = 0;
00061 d->objectID = 0;
00062 }
00063
00064 Statement::Statement(const Statement& other)
00065 {
00066 d = other.d;
00067 }
00068
00069 Statement::Statement(ResourcePtr subject, PropertyPtr predicate,
00070 NodePtr object) : d(new StatementPrivate)
00071 {
00072 d->model = subject->model().d;
00073 d->subjectID = subject->id();
00074 d->predicateID = predicate->id();
00075 d->objectID = object->id();
00076 }
00077
00078 Statement::~Statement()
00079 {
00080 }
00081
00082 Statement& Statement::operator=(const Statement& other)
00083 {
00084 d = other.d;
00085 return *this;
00086 }
00087
00088 bool Statement::operator==(const Statement& other) const
00089 {
00090 if (!d || !other.d)
00091 return d == other.d;
00092
00093 return *d == *(other.d);
00094 }
00095
00096 bool Statement::isNull() const
00097 {
00098 return d->subjectID == 0;
00099 }
00100
00101 ResourcePtr Statement::subject() const
00102 {
00103 const shared_ptr<Model::ModelPrivate> m = d ? d->model.lock() : shared_ptr<Model::ModelPrivate>();
00104 return m ? m->resourceByID(d->subjectID) : ResourcePtr(new Resource);
00105 }
00106
00107 PropertyPtr Statement::predicate() const
00108 {
00109 const shared_ptr<Model::ModelPrivate> m = d ? d->model.lock() : shared_ptr<Model::ModelPrivate>();
00110 return m ? m->propertyByID(d->predicateID) : PropertyPtr( new Property() );
00111 }
00112
00113 NodePtr Statement::object() const
00114 {
00115 const shared_ptr<Model::ModelPrivate> m = d ? d->model.lock() : shared_ptr<Model::ModelPrivate>();
00116 return m ? m->nodeByID(d->objectID) : NodePtr( LiteralPtr( new Literal() ) );
00117 }
00118
00119 ResourcePtr Statement::asResource() const
00120 {
00121 const shared_ptr<Model::ModelPrivate> m = d ? d->model.lock() : shared_ptr<Model::ModelPrivate>();
00122
00123 if (isNull() || !m || !m->nodeByID(d->objectID)->isResource())
00124 return ResourcePtr(new Resource);
00125
00126 return m->resourceByID(d->objectID);
00127 }
00128
00129 QString Statement::asString() const
00130 {
00131 if (isNull())
00132 return QString();
00133
00134 const shared_ptr<Model::ModelPrivate> m = d ? d->model.lock() : shared_ptr<Model::ModelPrivate>();
00135 return m ? m->nodeByID(d->objectID)->text() : QString();
00136 }
00137
00138 }
00139 }