020998c4b2fec3f7960874d26613db80ed6bcd00
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / data / qitem_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
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
75 QT_BEGIN_HEADER
76
77 QT_BEGIN_NAMESPACE
78
79 template<typename T> class QList;
80 template<typename T> class QVector;
81 template<typename T> class QAbstractXmlForwardIterator;
82
83 class QSourceLocation;
84 class QAbstractXmlReceiver;
85
86 namespace 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 <frans.englich@nokia.com>
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 <frans.englich@nokia.com>
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             reset();
211         }
212
213         inline Item(const QXmlNodeModelIndex &n) : node(n.m_storage)
214         {
215         }
216
217         inline Item(const Item &other) : node(other.node)
218         {
219             Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue), Q_FUNC_INFO,
220                        "Since we're only copying the node member, it must be the largest.");
221             if(isAtomicValue())
222                 atomicValue->ref.ref();
223         }
224
225         inline Item(const AtomicValue::Ptr &a)
226         {
227             reset();
228             if(a)
229             {
230                 atomicValue = a.data();
231                 atomicValue->ref.ref();
232
233                 /* Signal that we're housing an atomic value. */
234                 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
235             }
236         }
237
238         inline Item(const AtomicValue *const a)
239         {
240             /* Note, the implementation is a copy of the constructor above. */
241             reset();
242             if(a)
243             {
244                 atomicValue = a;
245                 atomicValue->ref.ref();
246
247                 /* Signal that we're housing an atomic value. */
248                 node.model = reinterpret_cast<const QAbstractXmlNodeModel *>(~0);
249             }
250         }
251
252         inline ~Item()
253         {
254             if(isAtomicValue() && !atomicValue->ref.deref())
255                 delete atomicValue;
256         }
257
258         inline Item &operator=(const Item &other)
259         {
260             Q_ASSERT_X(sizeof(QXmlNodeModelIndex) >= sizeof(AtomicValue *), Q_FUNC_INFO,
261                        "If this doesn't hold, we won't copy all data.");
262
263             if(other.isAtomicValue())
264                 other.atomicValue->ref.ref();
265
266             if(isAtomicValue())
267             {
268                 if(!atomicValue->ref.deref())
269                     delete atomicValue;
270             }
271
272             node = other.node;
273
274             return *this;
275         }
276
277         template<typename TCastTarget>
278         inline TCastTarget *as() const
279         {
280 #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
281 /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
282             Q_ASSERT_X(atomicValue == 0 || dynamic_cast<const TCastTarget *>(atomicValue),
283                        Q_FUNC_INFO,
284                        "The cast is invalid. This class does not inherit the cast target.");
285 #endif
286             return const_cast<TCastTarget *>(static_cast<const TCastTarget *>(atomicValue));
287         }
288
289         /**
290          * @short Returns the string value of this Item.
291          *
292          * In the case of a node, it is the node value corresponding to
293          * the particular node type. For atomic values, it is equivalent
294          * to the value cast as <tt>xs:string</tt>.
295          *
296          * Conceptually, this functions corresponds to the <tt>dm:string-value</tt> accessor.
297          *
298          * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-string-value">XQuery 1.0 and
299          * XPath 2.0 Data Model, 5.13 string-value Accessor</a>
300          * @returns the string value.
301          */
302         inline QString stringValue() const
303         {
304             if(isAtomicValue())
305                 return atomicValue->stringValue();
306             else
307                 return asNode().stringValue();
308         }
309
310         /**
311          * @short Returns the typed value of this item.
312          *
313          * Conceptually, this functions corresponds to the <tt>dm:typed-value</tt> accessor. Here are
314          * examples of what the typed value of an Item is:
315          *
316          * - The typed value of an atomic value is always the atomic value itself.
317          * - A comment node has always a typed value of type @c xs:string
318          * - For attribute and element nodes, the typed value can be arbitrary. For example, an
319          *   element can have a sequence of @c xs:dateTime instances.
320          *
321          * @returns the typed value of this item
322          * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dm-typed-value">XQuery 1.0 and
323          * XPath 2.0 Data Model, 5.15 typed-value Accessor</a>
324          */
325         Item::Iterator::Ptr sequencedTypedValue() const;
326
327         /**
328          * @short Determines whether this item is an atomic value, or a node.
329          *
330          * If this Item is @c null, @c false is returned.
331          *
332          * @see isNode()
333          * @returns @c true if it is an atomic value, otherwise @c false.
334          */
335         inline bool isAtomicValue() const
336         {
337             /* Setting node.model to ~0, signals that it's an atomic value. */
338             return node.model == reinterpret_cast<QAbstractXmlNodeModel *>(~0);
339         }
340
341         /**
342          * @short Determines whether this item is an atomic value, or a node.
343          *
344          * If this Item is @c null, false is returned.
345          *
346          * @see isAtomicValue()
347          * @returns @c true if this item is a node, otherwise @c false.
348          */
349         inline bool isNode() const
350         {
351             //return !isAtomicValue();
352             return node.model && node.model != reinterpret_cast<QAbstractXmlNodeModel *>(~0);
353         }
354
355         /**
356          * @short Returns the ItemType this Item is of.
357          *
358          * For example, if this Item is an XML node, more specifically a text node,
359          * <tt>text()</tt> is returned. That is, BuiltinTypes::text. However, if this
360          * Item is an atomic value of type <tt>xs:long</tt> that is what's returned,
361          * BuiltinTypes::xsLong.
362          *
363          * @returns the type of this Item.
364          */
365         inline QExplicitlySharedDataPointer<ItemType> type() const
366         {
367             if(isAtomicValue())
368                 return atomicValue->type();
369             else
370                 return asNode().type();
371         }
372
373         inline const AtomicValue *asAtomicValue() const
374         {
375             Q_ASSERT(isAtomicValue());
376             return atomicValue;
377         }
378
379         inline const QXmlNodeModelIndex &asNode() const
380         {
381             Q_ASSERT_X(isNode() || isNull(), Q_FUNC_INFO,
382                        "This item isn't a valid QXmlNodeModelIndex.");
383             Q_ASSERT_X(sizeof(QXmlNodeModelIndex) == sizeof(QPatternist::NodeIndexStorage), Q_FUNC_INFO,
384                        "If this doesn't hold, something is wrong.");
385
386             return reinterpret_cast<const QXmlNodeModelIndex &>(node);
387         }
388
389         inline operator bool() const
390         {
391             return node.model;
392         }
393
394         inline bool isNull() const
395         {
396             return !node.model;
397         }
398
399         inline void reset()
400         {
401             node.reset();
402         }
403
404         static inline Item fromPublic(const QXmlItem &i)
405         {
406             const Item it(i.m_node);
407             if(it.isAtomicValue())
408                 it.asAtomicValue()->ref.ref();
409
410             return it;
411         }
412
413         static inline QXmlItem toPublic(const Item &i)
414         {
415             return QXmlItem(i);
416         }
417
418     private:
419         union
420         {
421             NodeIndexStorage node;
422             const AtomicValue *atomicValue;
423         };
424     };
425
426     template<typename T>
427     inline Item toItem(const QExplicitlySharedDataPointer<T> atomicValue)
428     {
429         return Item(atomicValue.data());
430     }
431
432     /**
433      * This is an overload, provided for convenience.
434      * @relates QXmlNodeModelIndex
435      */
436     static inline QString formatData(const QXmlNodeModelIndex node)
437     {
438         return node.stringValue(); // This can be improved a lot.
439     }
440 }
441
442     inline QXmlName QXmlNodeModelIndex::name() const
443     {
444         return m_storage.model->name(*this);
445     }
446
447     inline QXmlNodeModelIndex QXmlNodeModelIndex::root() const
448     {
449         return m_storage.model->root(*this);
450     }
451
452     inline QXmlNodeModelIndex::Iterator::Ptr QXmlNodeModelIndex::iterate(const QXmlNodeModelIndex::Axis axis) const
453     {
454         return m_storage.model->iterate(*this, axis);
455     }
456
457     inline QUrl QXmlNodeModelIndex::documentUri() const
458     {
459         return m_storage.model->documentUri(*this);
460     }
461
462     inline QUrl QXmlNodeModelIndex::baseUri() const
463     {
464         return m_storage.model->baseUri(*this);
465     }
466
467     inline QXmlNodeModelIndex::NodeKind QXmlNodeModelIndex::kind() const
468     {
469         return m_storage.model->kind(*this);
470     }
471
472     inline bool QXmlNodeModelIndex::isDeepEqual(const QXmlNodeModelIndex &other) const
473     {
474         return m_storage.model->isDeepEqual(*this, other);
475     }
476
477     inline QXmlNodeModelIndex::DocumentOrder QXmlNodeModelIndex::compareOrder(const QXmlNodeModelIndex &other) const
478     {
479         Q_ASSERT_X(model() == other.model(), Q_FUNC_INFO, "The API docs guarantees the two nodes are from the same model");
480         return m_storage.model->compareOrder(*this, other);
481     }
482
483     inline bool QXmlNodeModelIndex::is(const QXmlNodeModelIndex &other) const
484     {
485         return m_storage.model == other.m_storage.model &&
486                m_storage.data == other.m_storage.data &&
487                m_storage.additionalData == other.m_storage.additionalData;
488     }
489
490     inline void QXmlNodeModelIndex::sendNamespaces(QAbstractXmlReceiver *const receiver) const
491     {
492         m_storage.model->sendNamespaces(*this, receiver);
493     }
494
495     inline QVector<QXmlName> QXmlNodeModelIndex::namespaceBindings() const
496     {
497         return m_storage.model->namespaceBindings(*this);
498     }
499
500     inline QXmlName::NamespaceCode QXmlNodeModelIndex::namespaceForPrefix(const QXmlName::PrefixCode prefix) const
501     {
502         return m_storage.model->namespaceForPrefix(*this, prefix);
503     }
504
505     inline QString QXmlNodeModelIndex::stringValue() const
506     {
507         return m_storage.model->stringValue(*this);
508     }
509
510     inline QPatternist::ItemType::Ptr QXmlNodeModelIndex::type() const
511     {
512         return m_storage.model->type(*this);
513     }
514
515     inline QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<QPatternist::Item> > QXmlNodeModelIndex::sequencedTypedValue() const
516     {
517         return m_storage.model->sequencedTypedValue(*this);
518     }
519
520     inline QXmlItem::QXmlItem(const QPatternist::Item &i) : m_node(i.node)
521     {
522         if(isAtomicValue())
523             m_atomicValue->ref.ref();
524     }
525
526 Q_DECLARE_TYPEINFO(QPatternist::Item::Iterator::Ptr, Q_MOVABLE_TYPE);
527 Q_DECLARE_TYPEINFO(QPatternist::AtomicValue, Q_MOVABLE_TYPE);
528
529 QT_END_NAMESPACE
530
531 QT_END_HEADER
532
533 #endif