• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2005 Frerich Raabe <raabe@kde.org>
3  * Copyright (C) 2006 Apple Computer, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef XPathValue_h
28 #define XPathValue_h
29 
30 #include "core/xml/XPathNodeSet.h"
31 #include "wtf/text/WTFString.h"
32 
33 namespace blink {
34 
35 namespace XPath {
36 
37 struct EvaluationContext;
38 
39 class ValueData : public RefCountedWillBeGarbageCollectedFinalized<ValueData> {
40 public:
create()41     static PassRefPtrWillBeRawPtr<ValueData> create() { return adoptRefWillBeNoop(new ValueData); }
create(const NodeSet & nodeSet)42     static PassRefPtrWillBeRawPtr<ValueData> create(const NodeSet& nodeSet) { return adoptRefWillBeNoop(new ValueData(nodeSet)); }
create(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet)43     static PassRefPtrWillBeRawPtr<ValueData> create(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet) { return adoptRefWillBeNoop(new ValueData(nodeSet)); }
create(const String & string)44     static PassRefPtrWillBeRawPtr<ValueData> create(const String& string) { return adoptRefWillBeNoop(new ValueData(string)); }
45     void trace(Visitor*);
nodeSet()46     NodeSet& nodeSet() { return *m_nodeSet; }
47 
48     String m_string;
49 
50 private:
ValueData()51     ValueData() : m_nodeSet(NodeSet::create()) { }
ValueData(const NodeSet & nodeSet)52     explicit ValueData(const NodeSet& nodeSet) : m_nodeSet(NodeSet::create(nodeSet)) { }
ValueData(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet)53     explicit ValueData(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet) : m_nodeSet(nodeSet) { }
ValueData(const String & string)54     explicit ValueData(const String& string) : m_string(string), m_nodeSet(NodeSet::create()) { }
55 
56     OwnPtrWillBeMember<NodeSet> m_nodeSet;
57 };
58 
59 // Copying Value objects makes their data partially shared, so care has to be taken when dealing with copies.
60 class Value {
61     DISALLOW_ALLOCATION();
62 public:
63     enum Type { NodeSetValue, BooleanValue, NumberValue, StringValue };
64 
Value(unsigned value)65     Value(unsigned value) : m_type(NumberValue), m_bool(false), m_number(value) { }
Value(unsigned long value)66     Value(unsigned long value) : m_type(NumberValue), m_bool(false), m_number(value) { }
Value(double value)67     Value(double value) : m_type(NumberValue), m_bool(false), m_number(value) { }
68 
Value(const char * value)69     Value(const char* value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
Value(const String & value)70     Value(const String& value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
Value(const NodeSet & value)71     Value(const NodeSet& value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
Value(Node * value)72     Value(Node* value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create()) { m_data->nodeSet().append(value); }
73     void trace(Visitor*);
74 
75     // This is needed to safely implement constructing from bool - with normal
76     // function overloading, any pointer type would match.
77     template<typename T> Value(T);
78 
79     static const struct AdoptTag { } adopt;
Value(PassOwnPtrWillBeRawPtr<NodeSet> value,const AdoptTag &)80     Value(PassOwnPtrWillBeRawPtr<NodeSet> value, const AdoptTag&) : m_type(NodeSetValue), m_bool(false), m_number(0),  m_data(ValueData::create(value)) { }
81 
type()82     Type type() const { return m_type; }
83 
isNodeSet()84     bool isNodeSet() const { return m_type == NodeSetValue; }
isBoolean()85     bool isBoolean() const { return m_type == BooleanValue; }
isNumber()86     bool isNumber() const { return m_type == NumberValue; }
isString()87     bool isString() const { return m_type == StringValue; }
88 
89     // If this is called during XPathExpression::evaluate(), EvaluationContext
90     // should be passed.
91     const NodeSet& toNodeSet(EvaluationContext*) const;
92     NodeSet& modifiableNodeSet(EvaluationContext&);
93     bool toBoolean() const;
94     double toNumber() const;
95     String toString() const;
96 
97 private:
98     Type m_type;
99     bool m_bool;
100     double m_number;
101     RefPtrWillBeMember<ValueData> m_data;
102 };
103 
104 template<>
Value(bool value)105 inline Value::Value(bool value)
106     : m_type(BooleanValue)
107     , m_bool(value)
108     , m_number(0)
109 {
110 }
111 
112 }
113 
114 }
115 #endif // XPathValue_h
116