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