• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "wtf/Forward.h"
31 #include "wtf/RefCounted.h"
32 #include "wtf/Vector.h"
33 #include "wtf/text/WTFString.h"
34 
35 namespace WebCore {
36 
37 class IDBKey : public RefCounted<IDBKey> {
38 public:
39     typedef Vector<RefPtr<IDBKey> > KeyArray;
40 
createInvalid()41     static PassRefPtr<IDBKey> createInvalid()
42     {
43         return adoptRef(new IDBKey());
44     }
45 
createNumber(double number)46     static PassRefPtr<IDBKey> createNumber(double number)
47     {
48         return adoptRef(new IDBKey(NumberType, number));
49     }
50 
createBinary(PassRefPtr<SharedBuffer> binary)51     static PassRefPtr<IDBKey> createBinary(PassRefPtr<SharedBuffer> binary)
52     {
53         return adoptRef(new IDBKey(binary));
54     }
55 
createString(const String & string)56     static PassRefPtr<IDBKey> createString(const String& string)
57     {
58         return adoptRef(new IDBKey(string));
59     }
60 
createDate(double date)61     static PassRefPtr<IDBKey> createDate(double date)
62     {
63         return adoptRef(new IDBKey(DateType, date));
64     }
65 
createMultiEntryArray(const KeyArray & array)66     static PassRefPtr<IDBKey> createMultiEntryArray(const KeyArray& array)
67     {
68         KeyArray result;
69 
70         for (size_t i = 0; i < array.size(); i++) {
71             if (!array[i]->isValid())
72                 continue;
73 
74             bool skip = false;
75             for (size_t j = 0; j < result.size(); j++) {
76                 if (array[i]->isEqual(result[j].get())) {
77                     skip = true;
78                     break;
79                 }
80             }
81             if (!skip) {
82                 result.append(array[i]);
83             }
84         }
85         RefPtr<IDBKey> idbKey = adoptRef(new IDBKey(result));
86         ASSERT(idbKey->isValid());
87         return idbKey.release();
88     }
89 
createArray(const KeyArray & array)90     static PassRefPtr<IDBKey> createArray(const KeyArray& array)
91     {
92         return adoptRef(new IDBKey(array));
93     }
94 
95     ~IDBKey();
96 
97     // In order of the least to the highest precedent in terms of sort order.
98     enum Type {
99         InvalidType = 0,
100         ArrayType,
101         BinaryType,
102         StringType,
103         DateType,
104         NumberType,
105         MinType
106     };
107 
type()108     Type type() const { return m_type; }
109     bool isValid() const;
110 
array()111     const KeyArray& array() const
112     {
113         ASSERT(m_type == ArrayType);
114         return m_array;
115     }
116 
binary()117     PassRefPtr<SharedBuffer> binary() const
118     {
119         ASSERT(m_type == BinaryType);
120         return m_binary;
121     }
122 
string()123     const String& string() const
124     {
125         ASSERT(m_type == StringType);
126         return m_string;
127     }
128 
date()129     double date() const
130     {
131         ASSERT(m_type == DateType);
132         return m_number;
133     }
134 
number()135     double number() const
136     {
137         ASSERT(m_type == NumberType);
138         return m_number;
139     }
140 
141     int compare(const IDBKey* other) const;
142     bool isLessThan(const IDBKey* other) const;
143     bool isEqual(const IDBKey* other) const;
144 
145     using RefCounted<IDBKey>::ref;
146     using RefCounted<IDBKey>::deref;
147 
148 private:
IDBKey()149     IDBKey() : m_type(InvalidType), m_number(0) { }
IDBKey(Type type,double number)150     IDBKey(Type type, double number) : m_type(type), m_number(number) { }
IDBKey(const String & value)151     explicit IDBKey(const String& value) : m_type(StringType), m_string(value), m_number(0) { }
IDBKey(PassRefPtr<SharedBuffer> value)152     explicit IDBKey(PassRefPtr<SharedBuffer> value) : m_type(BinaryType), m_binary(value), m_number(0) { }
IDBKey(const KeyArray & keyArray)153     explicit IDBKey(const KeyArray& keyArray) : m_type(ArrayType), m_array(keyArray), m_number(0) { }
154 
155     const Type m_type;
156     const KeyArray m_array;
157     RefPtr<SharedBuffer> m_binary;
158     const String m_string;
159     const double m_number;
160 };
161 
162 } // namespace WebCore
163 
164 #endif // IDBKey_h
165