2 * Copyright 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006 Apple Computer, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "core/xml/XPathNodeSet.h"
31 #include "wtf/text/WTFString.h"
37 class ValueData : public RefCountedWillBeGarbageCollectedFinalized<ValueData> {
39 static PassRefPtrWillBeRawPtr<ValueData> create() { return adoptRefWillBeNoop(new ValueData); }
40 static PassRefPtrWillBeRawPtr<ValueData> create(const NodeSet& nodeSet) { return adoptRefWillBeNoop(new ValueData(nodeSet)); }
41 static PassRefPtrWillBeRawPtr<ValueData> create(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet) { return adoptRefWillBeNoop(new ValueData(nodeSet)); }
42 static PassRefPtrWillBeRawPtr<ValueData> create(const String& string) { return adoptRefWillBeNoop(new ValueData(string)); }
44 NodeSet& nodeSet() { return *m_nodeSet; }
49 ValueData() : m_nodeSet(NodeSet::create()) { }
50 explicit ValueData(const NodeSet& nodeSet) : m_nodeSet(NodeSet::create(nodeSet)) { }
51 explicit ValueData(PassOwnPtrWillBeRawPtr<NodeSet> nodeSet) : m_nodeSet(nodeSet) { }
52 explicit ValueData(const String& string) : m_string(string), m_nodeSet(NodeSet::create()) { }
54 OwnPtrWillBeMember<NodeSet> m_nodeSet;
57 // Copying Value objects makes their data partially shared, so care has to be taken when dealing with copies.
59 DISALLOW_ALLOCATION();
61 enum Type { NodeSetValue, BooleanValue, NumberValue, StringValue };
63 Value(unsigned value) : m_type(NumberValue), m_bool(false), m_number(value) { }
64 Value(unsigned long value) : m_type(NumberValue), m_bool(false), m_number(value) { }
65 Value(double value) : m_type(NumberValue), m_bool(false), m_number(value) { }
67 Value(const char* value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
68 Value(const String& value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
69 Value(const NodeSet& value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
70 Value(Node* value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create()) { m_data->nodeSet().append(value); }
73 // This is needed to safely implement constructing from bool - with normal
74 // function overloading, any pointer type would match.
75 template<typename T> Value(T);
77 static const struct AdoptTag { } adopt;
78 Value(PassOwnPtrWillBeRawPtr<NodeSet> value, const AdoptTag&) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) { }
80 Type type() const { return m_type; }
82 bool isNodeSet() const { return m_type == NodeSetValue; }
83 bool isBoolean() const { return m_type == BooleanValue; }
84 bool isNumber() const { return m_type == NumberValue; }
85 bool isString() const { return m_type == StringValue; }
87 const NodeSet& toNodeSet() const;
88 NodeSet& modifiableNodeSet();
89 bool toBoolean() const;
90 double toNumber() const;
91 String toString() const;
97 RefPtrWillBeMember<ValueData> m_data;
101 inline Value::Value(bool value)
102 : m_type(BooleanValue)
111 #endif // XPathValue_h