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 THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_VALUE_H_
28 #define THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_VALUE_H_
29
30 #include "third_party/blink/renderer/core/core_export.h"
31 #include "third_party/blink/renderer/core/xml/xpath_node_set.h"
32 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
33
34 namespace blink {
35
36 namespace xpath {
37
38 struct EvaluationContext;
39
40 class ValueData final : public GarbageCollected<ValueData> {
41 public:
ValueData()42 ValueData() : node_set_(NodeSet::Create()) {}
ValueData(const NodeSet & node_set)43 explicit ValueData(const NodeSet& node_set)
44 : node_set_(NodeSet::Create(node_set)) {}
ValueData(NodeSet * node_set)45 explicit ValueData(NodeSet* node_set) : node_set_(node_set) {}
ValueData(const String & string)46 explicit ValueData(const String& string)
47 : string_(string), node_set_(NodeSet::Create()) {}
48
49 void Trace(Visitor*);
GetNodeSet()50 NodeSet& GetNodeSet() { return *node_set_; }
51
52 String string_;
53
54 private:
55 Member<NodeSet> node_set_;
56 };
57
58 // Copying Value objects makes their data partially shared, so care has to be
59 // taken when dealing with copies.
60 class CORE_EXPORT Value {
61 DISALLOW_NEW();
62
63 public:
64 enum Type { kNodeSetValue, kBooleanValue, kNumberValue, kStringValue };
65
Value(unsigned value)66 Value(unsigned value) : type_(kNumberValue), bool_(false), number_(value) {}
Value(uint64_t value)67 Value(uint64_t value) : type_(kNumberValue), bool_(false), number_(value) {}
Value(double value)68 Value(double value) : type_(kNumberValue), bool_(false), number_(value) {}
69
Value(const char * value)70 Value(const char* value)
71 : type_(kStringValue),
72 bool_(false),
73 number_(0),
74 data_(MakeGarbageCollected<ValueData>(value)) {}
Value(const String & value)75 Value(const String& value)
76 : type_(kStringValue),
77 bool_(false),
78 number_(0),
79 data_(MakeGarbageCollected<ValueData>(value)) {}
Value(const NodeSet & value)80 Value(const NodeSet& value)
81 : type_(kNodeSetValue),
82 bool_(false),
83 number_(0),
84 data_(MakeGarbageCollected<ValueData>(value)) {}
Value(Node * value)85 Value(Node* value)
86 : type_(kNodeSetValue),
87 bool_(false),
88 number_(0),
89 data_(MakeGarbageCollected<ValueData>()) {
90 data_->GetNodeSet().Append(value);
91 }
92 void Trace(Visitor*);
93
94 // This is needed to safely implement constructing from bool - with normal
95 // function overloading, any pointer type would match.
96 template <typename T>
97 Value(T);
98
99 static const struct AdoptTag {
100 } kAdopt;
Value(NodeSet * value,const AdoptTag &)101 Value(NodeSet* value, const AdoptTag&)
102 : type_(kNodeSetValue),
103 bool_(false),
104 number_(0),
105 data_(MakeGarbageCollected<ValueData>(value)) {}
106
GetType()107 Type GetType() const { return type_; }
108
IsNodeSet()109 bool IsNodeSet() const { return type_ == kNodeSetValue; }
IsBoolean()110 bool IsBoolean() const { return type_ == kBooleanValue; }
IsNumber()111 bool IsNumber() const { return type_ == kNumberValue; }
IsString()112 bool IsString() const { return type_ == kStringValue; }
113
114 // If this is called during XPathExpression::evaluate(), EvaluationContext
115 // should be passed to record type conversion error.
116 const NodeSet& ToNodeSet(EvaluationContext*) const;
117 NodeSet& ModifiableNodeSet(EvaluationContext&);
118 bool ToBoolean() const;
119 double ToNumber() const;
120 String ToString() const;
121
122 private:
123 Type type_;
124 bool bool_;
125 double number_;
126 Member<ValueData> data_;
127 };
128
129 template <>
Value(bool value)130 inline Value::Value(bool value)
131 : type_(kBooleanValue), bool_(value), number_(0) {}
132
133 } // namespace xpath
134
135 } // namespace blink
136
137 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_VALUE_H_
138