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

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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