Home | History | Annotate | Download | only in xml
      1 /*
      2  * Copyright 2005 Frerich Raabe <raabe (at) 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 WebCore {
     34 
     35 namespace XPath {
     36 
     37 class ValueData : public RefCountedWillBeGarbageCollectedFinalized<ValueData> {
     38 public:
     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)); }
     43     void trace(Visitor*);
     44     NodeSet& nodeSet() { return *m_nodeSet; }
     45 
     46     String m_string;
     47 
     48 private:
     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()) { }
     53 
     54     OwnPtrWillBeMember<NodeSet> m_nodeSet;
     55 };
     56 
     57 // Copying Value objects makes their data partially shared, so care has to be taken when dealing with copies.
     58 class Value {
     59     DISALLOW_ALLOCATION();
     60 public:
     61     enum Type { NodeSetValue, BooleanValue, NumberValue, StringValue };
     62 
     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) { }
     66 
     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); }
     71     void trace(Visitor*);
     72 
     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);
     76 
     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)) { }
     79 
     80     Type type() const { return m_type; }
     81 
     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; }
     86 
     87     const NodeSet& toNodeSet() const;
     88     NodeSet& modifiableNodeSet();
     89     bool toBoolean() const;
     90     double toNumber() const;
     91     String toString() const;
     92 
     93 private:
     94     Type m_type;
     95     bool m_bool;
     96     double m_number;
     97     RefPtrWillBeMember<ValueData> m_data;
     98 };
     99 
    100 template<>
    101 inline Value::Value(bool value)
    102     : m_type(BooleanValue)
    103     , m_bool(value)
    104     , m_number(0)
    105 {
    106 }
    107 
    108 }
    109 
    110 }
    111 #endif // XPathValue_h
    112