Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBKey.h
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IDBKey_h
27 #define IDBKey_h
28
29 #include "platform/SharedBuffer.h"
30 #include "platform/heap/Handle.h"
31 #include "wtf/Forward.h"
32 #include "wtf/RefCounted.h"
33 #include "wtf/Vector.h"
34 #include "wtf/text/WTFString.h"
35
36 namespace WebCore {
37
38 class IDBKey : public RefCountedWillBeGarbageCollectedFinalized<IDBKey> {
39 public:
40     typedef WillBeHeapVector<RefPtrWillBeMember<IDBKey> > KeyArray;
41
42     static PassRefPtrWillBeRawPtr<IDBKey> createInvalid()
43     {
44         return adoptRefWillBeNoop(new IDBKey());
45     }
46
47     static PassRefPtrWillBeRawPtr<IDBKey> createNumber(double number)
48     {
49         return adoptRefWillBeNoop(new IDBKey(NumberType, number));
50     }
51
52     static PassRefPtrWillBeRawPtr<IDBKey> createBinary(PassRefPtr<SharedBuffer> binary)
53     {
54         return adoptRefWillBeNoop(new IDBKey(binary));
55     }
56
57     static PassRefPtrWillBeRawPtr<IDBKey> createString(const String& string)
58     {
59         return adoptRefWillBeNoop(new IDBKey(string));
60     }
61
62     static PassRefPtrWillBeRawPtr<IDBKey> createDate(double date)
63     {
64         return adoptRefWillBeNoop(new IDBKey(DateType, date));
65     }
66
67     static PassRefPtrWillBeRawPtr<IDBKey> createMultiEntryArray(const KeyArray& array)
68     {
69         KeyArray result;
70
71         for (size_t i = 0; i < array.size(); i++) {
72             if (!array[i]->isValid())
73                 continue;
74
75             bool skip = false;
76             for (size_t j = 0; j < result.size(); j++) {
77                 if (array[i]->isEqual(result[j].get())) {
78                     skip = true;
79                     break;
80                 }
81             }
82             if (!skip) {
83                 result.append(array[i]);
84             }
85         }
86         RefPtrWillBeRawPtr<IDBKey> idbKey = adoptRefWillBeNoop(new IDBKey(result));
87         ASSERT(idbKey->isValid());
88         return idbKey.release();
89     }
90
91     static PassRefPtrWillBeRawPtr<IDBKey> createArray(const KeyArray& array)
92     {
93         return adoptRefWillBeNoop(new IDBKey(array));
94     }
95
96     ~IDBKey();
97     void trace(Visitor*);
98
99     // In order of the least to the highest precedent in terms of sort order.
100     enum Type {
101         InvalidType = 0,
102         ArrayType,
103         BinaryType,
104         StringType,
105         DateType,
106         NumberType,
107         MinType
108     };
109
110     Type type() const { return m_type; }
111     bool isValid() const;
112
113     const KeyArray& array() const
114     {
115         ASSERT(m_type == ArrayType);
116         return m_array;
117     }
118
119     PassRefPtr<SharedBuffer> binary() const
120     {
121         ASSERT(m_type == BinaryType);
122         return m_binary;
123     }
124
125     const String& string() const
126     {
127         ASSERT(m_type == StringType);
128         return m_string;
129     }
130
131     double date() const
132     {
133         ASSERT(m_type == DateType);
134         return m_number;
135     }
136
137     double number() const
138     {
139         ASSERT(m_type == NumberType);
140         return m_number;
141     }
142
143     int compare(const IDBKey* other) const;
144     bool isLessThan(const IDBKey* other) const;
145     bool isEqual(const IDBKey* other) const;
146
147 private:
148     IDBKey() : m_type(InvalidType), m_number(0) { }
149     IDBKey(Type type, double number) : m_type(type), m_number(number) { }
150     explicit IDBKey(const String& value) : m_type(StringType), m_string(value), m_number(0) { }
151     explicit IDBKey(PassRefPtr<SharedBuffer> value) : m_type(BinaryType), m_binary(value), m_number(0) { }
152     explicit IDBKey(const KeyArray& keyArray) : m_type(ArrayType), m_array(keyArray), m_number(0) { }
153
154     const Type m_type;
155     const KeyArray m_array;
156     RefPtr<SharedBuffer> m_binary;
157     const String m_string;
158     const double m_number;
159 };
160
161 } // namespace WebCore
162
163 #endif // IDBKey_h