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 #if ENABLE(XPATH)
31 
32 #include "PlatformString.h"
33 #include "XPathNodeSet.h"
34 
35 namespace WebCore {
36 
37     namespace XPath {
38 
39         class ValueData : public RefCounted<ValueData> {
40         public:
create()41             static PassRefPtr<ValueData> create() { return adoptRef(new ValueData); }
create(const NodeSet & nodeSet)42             static PassRefPtr<ValueData> create(const NodeSet& nodeSet) { return adoptRef(new ValueData(nodeSet)); }
create(const String & string)43             static PassRefPtr<ValueData> create(const String& string) { return adoptRef(new ValueData(string)); }
44 
45             NodeSet m_nodeSet;
46             String m_string;
47 
48         private:
ValueData()49             ValueData() { }
ValueData(const NodeSet & nodeSet)50             ValueData(const NodeSet& nodeSet) : m_nodeSet(nodeSet) { }
ValueData(const String & string)51             ValueData(const String& string) : m_string(string) { }
52         };
53 
54         // Copying Value objects makes their data partially shared, so care has to be taken when dealing with copies.
55         class Value {
56         public:
57             enum Type { NodeSetValue, BooleanValue, NumberValue, StringValue };
58 
Value(unsigned value)59             Value(unsigned value) : m_type(NumberValue), m_bool(false), m_number(value) {}
Value(unsigned long value)60             Value(unsigned long value) : m_type(NumberValue), m_bool(false), m_number(value) {}
Value(double value)61             Value(double value) : m_type(NumberValue), m_bool(false), m_number(value) {}
62 
Value(const char * value)63             Value(const char* value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) {}
Value(const String & value)64             Value(const String& value) : m_type(StringValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) {}
Value(const NodeSet & value)65             Value(const NodeSet& value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create(value)) {}
Value(Node * value)66             Value(Node* value) : m_type(NodeSetValue), m_bool(false), m_number(0), m_data(ValueData::create()) { m_data->m_nodeSet.append(value); }
67 
68             // This is needed to safely implement constructing from bool - with normal function overloading, any pointer type would match.
69             template<typename T> Value(T);
70 
71             static const struct AdoptTag {} adopt;
Value(NodeSet & value,const AdoptTag &)72             Value(NodeSet& value, const AdoptTag&) : m_type(NodeSetValue), m_bool(false), m_number(0),  m_data(ValueData::create()) { value.swap(m_data->m_nodeSet); }
73 
type()74             Type type() const { return m_type; }
75 
isNodeSet()76             bool isNodeSet() const { return m_type == NodeSetValue; }
isBoolean()77             bool isBoolean() const { return m_type == BooleanValue; }
isNumber()78             bool isNumber() const { return m_type == NumberValue; }
isString()79             bool isString() const { return m_type == StringValue; }
80 
81             const NodeSet& toNodeSet() const;
82             NodeSet& modifiableNodeSet();
83             bool toBoolean() const;
84             double toNumber() const;
85             String toString() const;
86 
87         private:
88             Type m_type;
89             bool m_bool;
90             double m_number;
91             RefPtr<ValueData> m_data;
92         };
93 
94         template<>
Value(bool value)95         inline Value::Value(bool value)
96             : m_type(BooleanValue)
97             , m_bool(value)
98             , m_number(0)
99         {
100         }
101     }
102 }
103 
104 #endif // ENABLE(XPATH)
105 
106 #endif // XPath_Value_H
107