Dataquay 0.8
Node.h
Go to the documentation of this file.
1/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3/*
4 Dataquay
5
6 A C++/Qt library for simple RDF datastore management.
7 Copyright 2009-2012 Chris Cannam.
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use, copy,
13 modify, merge, publish, distribute, sublicense, and/or sell copies
14 of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the name of Chris Cannam
29 shall not be used in advertising or otherwise to promote the sale,
30 use or other dealings in this Software without prior written
31 authorization.
32*/
33
34#ifndef DATAQUAY_NODE_H
35#define DATAQUAY_NODE_H
36
37namespace Dataquay {
38class Node;
39}
40
41// Declare this early, to avoid any problems with instantiation order
42// arising from inclusion "races". If this is not found when
43// compiling, hoist this header so that it is included earlier than
44// any other header that includes <QHash>
45extern unsigned int qHash(const Dataquay::Node &);
46
47#include "Uri.h"
48
49#include <QString>
50#include <QVariant>
51
52class QDataStream;
53class QTextStream;
54
55namespace Dataquay
56{
57
64class Node
65{
66public:
71
76 Node() : type(Nothing), value() { }
77
89 Node(Uri u) : type(URI), value(u.toString()) { }
90
95 Node(QString v) : type(Literal), value(v) { }
96
100 Node(QString v, Uri dt) : type(Literal), value(v), datatype(dt) { }
101
102 Node(const Node &n) :
103 type(n.type), value(n.value), datatype(n.datatype) {
104 }
105
106 Node &operator=(const Node &n) {
107 type = n.type; value = n.value; datatype = n.datatype;
108 return *this;
109 }
110
111 ~Node() { }
112
139 static Node fromVariant(const QVariant &v);
140
151 QVariant toVariant() const;
152
167 QVariant toVariant(int metaTypeId) const;
168
169 bool operator<(const Node &n) const {
170 if (type != n.type) return type < n.type;
171 if (value != n.value) return value < n.value;
172 if (datatype != n.datatype) return datatype < n.datatype;
173 return false;
174 }
175
182 virtual ~VariantEncoder() { }
183
189 virtual QVariant toVariant(const QString &n) = 0;
190
196 virtual QString fromVariant(const QVariant &v) = 0;
197 };
198
220 QString variantTypeName,
221 VariantEncoder *encoder = 0);
222
229 static Uri getDatatype(QString variantTypeName);
230
237
239 QString value;
241};
242
246typedef QList<Node> Nodes;
247
248bool operator==(const Node &a, const Node &b);
249bool operator!=(const Node &a, const Node &b);
250
251QDataStream &operator<<(QDataStream &out, const Node &);
252QDataStream &operator>>(QDataStream &in, Node &);
253
254std::ostream &operator<<(std::ostream &out, const Node &);
255QTextStream &operator<<(QTextStream &out, const Node &);
256
257}
258
259#endif
260
unsigned int qHash(const Dataquay::Node &)
Node represents a single RDF node, with conversions to and from variant types.
Definition Node.h:65
Type type
Definition Node.h:238
bool operator<(const Node &n) const
Definition Node.h:169
static Node fromVariant(const QVariant &v)
Convert a QVariant to a Node.
QVariant toVariant() const
Convert a Node to a QVariant.
QVariant toVariant(int metaTypeId) const
Convert a Node to a QVariant, with a nudge for the variant type, used to override the default variant...
static QString getVariantTypeName(Uri datatype)
Retrieve the variant type that has been associated with the given datatype Uri using registerDatatype...
Node(QString v, Uri dt)
Construct a literal node with the given value and datatype.
Definition Node.h:100
Node()
Construct a node with no node type (used for example as an undefined node when pattern matching a tri...
Definition Node.h:76
static void registerDatatype(Uri datatype, QString variantTypeName, VariantEncoder *encoder=0)
Register an association between a particular datatype URI and a type which can be stored in a QVarian...
static Uri getDatatype(QString variantTypeName)
Retrieve the datatype URI that has been associated with the given variant type using registerDatatype...
QString value
Definition Node.h:239
Uri datatype
Definition Node.h:240
Node(Uri u)
Construct a node with a URI node type and the given URI.
Definition Node.h:89
Node(QString v)
Construct a literal node with the given value, and with no defined datatype.
Definition Node.h:95
Node & operator=(const Node &n)
Definition Node.h:106
Type
Node type.
Definition Node.h:70
Node(const Node &n)
Definition Node.h:102
Uri represents a single URI.
Definition Uri.h:77
bool operator!=(const Node &a, const Node &b)
QList< Node > Nodes
A list of node types.
Definition Node.h:246
QDataStream & operator<<(QDataStream &out, const Node &)
bool operator==(const Node &a, const Node &b)
QDataStream & operator>>(QDataStream &in, Node &)
VariantEncoder is an abstract interface for classes that can convert between QVariant and strings for...
Definition Node.h:181
virtual QVariant toVariant(const QString &n)=0
Convert a string to a variant.
virtual QString fromVariant(const QVariant &v)=0
Convert a variant to a string.