source: trunk/src/xmlpatterns/data/qitem_p.h@ 353

Last change on this file since 353 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 17.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51
52#ifndef Patternist_Item_H
53#define Patternist_Item_H
54
55#include <QtXmlPatterns/private/qcppcastinghelper_p.h>
56#include <QtXmlPatterns/private/qitemtype_p.h>
57#include <QtXmlPatterns/private/qsingletoniterator_p.h>
58#include <QtXmlPatterns/QAbstractXmlNodeModel>
59
60#include <QUrl>
61#include <QVariant>
62
63/**
64 * @file
65 * @short Due to strong interdependencies, this file contains the definitions for
66 * the classes Item, QXmlNodeModelIndex, QAbstractXmlNodeModel and AtomicValue. The implementations are
67 * in their respective source files.
68 */
69
70/**
71 * @class QSharedData
72 * @short Qt's base class for reference counting.
73 * @author Trolltech
74 */
75
76QT_BEGIN_HEADER
77
78QT_BEGIN_NAMESPACE
79
80template<typename T> class QList;
81template<typename T> class QVector;
82template<typename T> class QAbstractXmlForwardIterator;
83
84class QSourceLocation;
85class QAbstractXmlReceiver;
86
87namespace QPatternist
88{
89 class DynamicContext;
90 class Item;
91 class ItemType;
92 class QObjectNodeModel;
93 template<typename T> class EmptyIterator;
94 template<typename T, typename ListType> class ListIterator;
95
96 /**
97 * @short Base class for all classes representing atomic values.
98 *
99 * Instantiating AtomicValues sub classes from a value of somekind,
100 * for a certain type is done in three different ways:
101 *
102 * - The static factory fromLexical which available in most classes. This
103 * function attempts to create a value from a QString that is considered
104 * a lexical representation of the value. Thus, this function performs validation, takes
105 * care of whitespace facets, and everything else related to instantiating a value from
106 * a lexical representation.
107 * - The static factory function fromValue. This function exists for
108 * values where a C++ type exists which corresponds to the type's value space.
109 * - By using instances available in CommonValues. This is the preferred method
110 * since it uses existing singleton instances and thus saves memory. CommonValues
111 * should be used whenever possible, it can be thought as a collection of constant values.
112 *
113 * For types that does not distinguish the value space and lexical space, such as <tt>xs:string</tt>,
114 * only the fromValue() function exist, and fromLexical() is omitted.
115 *
116 * @ingroup Patternist_xdm
117 * @author Frans Englich <[email protected]>
118 */
119 class AtomicValue : public QSharedData
120 , public CppCastingHelper<AtomicValue>
121 {
122 public:
123 virtual ~AtomicValue();
124
125 /**
126 * A smart pointer wrapping AtomicValue instances.
127 */
128 typedef QExplicitlySharedDataPointer<AtomicValue> Ptr;
129
130 /**
131 * Determines whether this atomic value has an error. This is used
132 * for implementing casting.
133 *
134 * @returns always @c false
135 */
136 virtual bool hasError() const;
137
138 /**
139 * Always fails by issuing the type error ReportContext::FORG0006. Sub-classes
140 * whose represented type do allow EBV to be extracted from, must thus
141 * re-implement this function.
142 */
143 virtual bool evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &context) const;
144
145 virtual QString stringValue() const = 0;
146 virtual ItemType::Ptr type() const = 0;
147
148 /**
149 * Converts @p value to a QVariant.
150 */
151 static QVariant toQt(const AtomicValue *const value);
152
153 static inline QVariant toQt(const AtomicValue::Ptr &value)
154 {
155 return toQt(value.data());
156 }
157
158 static Item toXDM(const QVariant &value);
159
160 static ItemType::Ptr qtToXDMType(const QXmlItem &item);
161 protected:
162 inline AtomicValue()
163 {
164 }
165 };
166
167 /**
168 * @short Represents an item in the XPath 2.0 Data Model.
169 *
170 * There exists two types of items: nodes and atomic values.
171 *
172 * The XQuery 1.0 and XPath 2.0 Data Model and XML Path Language (XPath) 2.0 specification
173 * makes a very strong distinction between a sequence of items and an atomized sequence.
174 *
175 * @ingroup Patternist_xdm
176 * @author Frans Englich <[email protected]>
177 */
178 class Item
179 {
180 friend class QT_PREPEND_NAMESPACE(QXmlItem);
181
182 public:
183 /**
184 * A smart pointer wrapping an Item instance.
185 */
186 typedef QAbstractXmlForwardIterator<Item> Iterator;
187
188 /**
189 * A list of Item instances, each wrapped in a smart pointer.
190 */
191 typedef QList<Item> List;
192
193 /**
194 * A vector of Item instances, each wrapped in a smart pointer.
195 */
196 typedef QVector<Item> Vector;
197
198 typedef QPatternist::SingletonIterator<Item> SingletonIterator;
199 typedef QPatternist::EmptyIterator<Item> EmptyIterator;
200
201 /**
202 * Default constructor.
203 */
204 inline Item()
205 {
206 /* Note that this function should be equal to reset(). */
207
208 /* This is the area which atomicValue uses. Becauase we want as()
209 * to return null on null-constructed objects, we initialize it. */
210 node.data = 0;
211
212 /* This signals that we're not an atomic value. */
213 node.model = 0;
214 }
215
216 inline Item(const QXmlNodeModelIndex &n) : node(n.m_storage)
217 {
218 }
219
220 inline Item(const Item &other) : node(other.node)
221 {
222 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue), Q_FUNC_INFO,
223 "Since we're only copying the node member, it must be the largest.");
224 if(isAtomicValue())
225 atomicValue->ref.ref();
226 }
227
228 inline Item(const AtomicValue::Ptr &a)
229 {
230 if(a)
231 {
232 atomicValue = a.data();
233 atomicValue->ref.ref();
234
235 /* Signal that we're housing an atomic value. */
236 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
237 }
238 else
239 node.model = 0; /* Like the default constructor. */
240 }
241
242 inline Item(const AtomicValue *const a)
243 {
244 /* Note, the implementation is a copy of the constructor above. */
245
246 if(a)
247 {
248 atomicValue = a;
249 atomicValue->ref.ref();
250
251 /* Signal that we're housing an atomic value. */
252 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
253 }
254 else
255 node.model = 0; /* Like the default constructor. */
256 }
257
258 inline ~Item()
259 {
260 if(isAtomicValue() && !atomicValue->ref.deref())
261 delete atomicValue;
262 }
263
264 inline Item &operator=(const Item &other)
265 {
266 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue *), Q_FUNC_INFO,
267 "If this doesn't hold, we won't copy all data.");
268
269 if(other.isAtomicValue())
270 other.atomicValue->ref.ref();
271
272 if(isAtomicValue())
273 {
274 if(!atomicValue->ref.deref())
275 delete atomicValue;
276 }
277
278 node = other.node;
279
280 return *this;
281 }
282
283 template<typename TCastTarget>
284 inline TCastTarget *as() const
285 {
286#if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
287/* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
288 Q_ASSERT_X(atomicValue == 0 || dynamic_cast<const TCastTarget *>(atomicValue),
289 Q_FUNC_INFO,
290 "The cast is invalid. This class does not inherit the cast target.");
291#endif
292 return const_cast<TCastTarget *>(static_cast<const TCastTarget *>(atomicValue));
293 }
294
295 /**
296 * @short Returns the string value of this Item.
297 *
298 * In the case of a node, it is the node value corresponding to
299 * the particular node type. For atomic values, it is equivalent
300 * to the value cast as <tt>xs:string</tt>.
301 *
302 * Conceptually, this functions corresponds to the <tt>dm:string-value</tt> accessor.
303 *
304 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-string-value">XQuery 1.0 and
305 * XPath 2.0 Data Model, 5.13 string-value Accessor</a>
306 * @returns the string value.
307 */
308 inline QString stringValue() const
309 {
310 if(isAtomicValue())
311 return atomicValue->stringValue();
312 else
313 return asNode().stringValue();
314 }
315
316 /**
317 * @short Returns the typed value of this item.
318 *
319 * Conceptually, this functions corresponds to the <tt>dm:typed-value</tt> accessor. Here are
320 * examples of what the typed value of an Item is:
321 *
322 * - The typed value of an atomic value is always the atomic value itself.
323 * - A comment node has always a typed value of type @c xs:string
324 * - For attribute and element nodes, the typed value can be arbitrary. For example, an
325 * element can have a sequence of @c xs:dateTime instances.
326 *
327 * @returns the typed value of this item
328 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-typed-value">XQuery 1.0 and
329 * XPath 2.0 Data Model, 5.15 typed-value Accessor</a>
330 */
331 Item::Iterator::Ptr sequencedTypedValue() const;
332
333 /**
334 * @short Determines whether this item is an atomic value, or a node.
335 *
336 * If this Item is @c null, @c false is returned.
337 *
338 * @see isNode()
339 * @returns @c true if it is an atomic value, otherwise @c false.
340 */
341 inline bool isAtomicValue() const
342 {
343 /* Setting node.model to ~0, signals that it's an atomic value. */
344 return node.model == reinterpret_cast<QAbstractXmlNodeModel *>(~0);
345 }
346
347 /**
348 * @short Determines whether this item is an atomic value, or a node.
349 *
350 * If this Item is @c null, false is returned.
351 *
352 * @see isAtomicValue()
353 * @returns @c true if this item is a node, otherwise @c false.
354 */
355 inline bool isNode() const
356 {
357 //return !isAtomicValue();
358 return node.model && node.model != reinterpret_cast<QAbstractXmlNodeModel *>(~0);
359 }
360
361 /**
362 * @short Returns the ItemType this Item is of.
363 *
364 * For example, if this Item is an XML node, more specifically a text node,
365 * <tt>text()</tt> is returned. That is, BuiltinTypes::text. However, if this
366 * Item is an atomic value of type <tt>xs:long</tt> that is what's returned,
367 * BuiltinTypes::xsLong.
368 *
369 * @returns the type of this Item.
370 */
371 inline QExplicitlySharedDataPointer<ItemType> type() const
372 {
373 if(isAtomicValue())
374 return atomicValue->type();
375 else
376 return asNode().type();
377 }
378
379 inline const AtomicValue *asAtomicValue() const
380 {
381 Q_ASSERT(isAtomicValue());
382 return atomicValue;
383 }
384
385 inline const QXmlNodeModelIndex &asNode() const
386 {
387 Q_ASSERT_X(isNode() || isNull(), Q_FUNC_INFO,
388 "This item isn't a valid QXmlNodeModelIndex.");
389 Q_ASSERT_X(sizeof(QXmlNodeModelIndex) == sizeof(QPatternist::NodeIndexStorage), Q_FUNC_INFO,
390 "If this doesn't hold, something is wrong.");
391
392 return reinterpret_cast<const QXmlNodeModelIndex &>(node);
393 }
394
395 inline operator bool() const
396 {
397 return node.model;
398 }
399
400 inline bool isNull() const
401 {
402 return !node.model;
403 }
404
405 inline void reset()
406 {
407 /* Note that this function should be equal to the default
408 * constructor. */
409 node.model = 0;
410 node.data = 0;
411 }
412
413 static inline Item fromPublic(const QXmlItem &i)
414 {
415 const Item it(i.m_node);
416 if(it.isAtomicValue())
417 it.asAtomicValue()->ref.ref();
418
419 return it;
420 }
421
422 static inline QXmlItem toPublic(const Item &i)
423 {
424 return QXmlItem(i);
425 }
426
427 private:
428 union
429 {
430 NodeIndexStorage node;
431 const AtomicValue *atomicValue;
432 };
433 };
434
435 template<typename T>
436 inline Item toItem(const QExplicitlySharedDataPointer<T> atomicValue)
437 {
438 return Item(atomicValue.data());
439 }
440
441 /**
442 * This is an overload, provided for convenience.
443 * @relates QXmlNodeModelIndex
444 */
445 static inline QString formatData(const QXmlNodeModelIndex node)
446 {
447 return node.stringValue(); // This can be improved a lot.
448 }
449}
450
451 inline QXmlName QXmlNodeModelIndex::name() const
452 {
453 return m_storage.model->name(*this);
454 }
455
456 inline QXmlNodeModelIndex QXmlNodeModelIndex::root() const
457 {
458 return m_storage.model->root(*this);
459 }
460
461 inline QXmlNodeModelIndex::Iterator::Ptr QXmlNodeModelIndex::iterate(const QXmlNodeModelIndex::Axis axis) const
462 {
463 return m_storage.model->iterate(*this, axis);
464 }
465
466 inline QUrl QXmlNodeModelIndex::documentUri() const
467 {
468 return m_storage.model->documentUri(*this);
469 }
470
471 inline QUrl QXmlNodeModelIndex::baseUri() const
472 {
473 return m_storage.model->baseUri(*this);
474 }
475
476 inline QXmlNodeModelIndex::NodeKind QXmlNodeModelIndex::kind() const
477 {
478 return m_storage.model->kind(*this);
479 }
480
481 inline bool QXmlNodeModelIndex::isDeepEqual(const QXmlNodeModelIndex &other) const
482 {
483 return m_storage.model->isDeepEqual(*this, other);
484 }
485
486 inline QXmlNodeModelIndex::DocumentOrder QXmlNodeModelIndex::compareOrder(const QXmlNodeModelIndex &other) const
487 {
488 Q_ASSERT_X(model() == other.model(), Q_FUNC_INFO, "The API docs guarantees the two nodes are from the same model");
489 return m_storage.model->compareOrder(*this, other);
490 }
491
492 inline bool QXmlNodeModelIndex::is(const QXmlNodeModelIndex &other) const
493 {
494 return m_storage.model == other.m_storage.model &&
495 m_storage.data == other.m_storage.data &&
496 m_storage.additionalData == other.m_storage.additionalData;
497 }
498
499 inline void QXmlNodeModelIndex::sendNamespaces(QAbstractXmlReceiver *const receiver) const
500 {
501 m_storage.model->sendNamespaces(*this, receiver);
502 }
503
504 inline QVector<QXmlName> QXmlNodeModelIndex::namespaceBindings() const
505 {
506 return m_storage.model->namespaceBindings(*this);
507 }
508
509 inline QXmlName::NamespaceCode QXmlNodeModelIndex::namespaceForPrefix(const QXmlName::PrefixCode prefix) const
510 {
511 return m_storage.model->namespaceForPrefix(*this, prefix);
512 }
513
514 inline QString QXmlNodeModelIndex::stringValue() const
515 {
516 return m_storage.model->stringValue(*this);
517 }
518
519 inline QPatternist::ItemType::Ptr QXmlNodeModelIndex::type() const
520 {
521 return m_storage.model->type(*this);
522 }
523
524 inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > QXmlNodeModelIndex::sequencedTypedValue() const
525 {
526 return m_storage.model->sequencedTypedValue(*this);
527 }
528
529 inline QXmlItem::QXmlItem(const QPatternist::Item &i) : m_node(i.node)
530 {
531 if(isAtomicValue())
532 m_atomicValue->ref.ref();
533 }
534
535Q_DECLARE_TYPEINFO(QPatternist::Item::Iterator::Ptr, Q_MOVABLE_TYPE);
536Q_DECLARE_TYPEINFO(QPatternist::AtomicValue, Q_MOVABLE_TYPE);
537
538QT_END_NAMESPACE
539
540QT_END_HEADER
541
542#endif
Note: See TracBrowser for help on using the repository browser.