• 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 "platform/heap/Handle.h"
31 #include "wtf/Forward.h"
32 #include "wtf/Vector.h"
33 #include "wtf/text/WTFString.h"
34 
35 namespace WebCore {
36 
37 class IDBKey : public GarbageCollectedFinalized<IDBKey> {
38 public:
39     typedef HeapVector<Member<IDBKey> > KeyArray;
40 
createInvalid()41     static IDBKey* createInvalid()
42     {
43         return new IDBKey();
44     }
45 
createNumber(double number)46     static IDBKey* createNumber(double number)
47     {
48         return new IDBKey(NumberType, number);
49     }
50 
createBinary(PassRefPtr<SharedBuffer> binary)51     static IDBKey* createBinary(PassRefPtr<SharedBuffer> binary)
52     {
53         return new IDBKey(binary);
54     }
55 
createString(const String & string)56     static IDBKey* createString(const String& string)
57     {
58         return new IDBKey(string);
59     }
60 
createDate(double date)61     static IDBKey* createDate(double date)
62     {
63         return new IDBKey(DateType, date);
64     }
65 
createMultiEntryArray(const KeyArray & array)66     static 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         IDBKey* idbKey = new IDBKey(result);
86         ASSERT(idbKey->isValid());
87         return idbKey;
88     }
89 
createArray(const KeyArray & array)90     static IDBKey* createArray(const KeyArray& array)
91     {
92         return new IDBKey(array);
93     }
94 
95     ~IDBKey();
96     void trace(Visitor*);
97 
98     // In order of the least to the highest precedent in terms of sort order.
99     enum Type {
100         InvalidType = 0,
101         ArrayType,
102         BinaryType,
103         StringType,
104         DateType,
105         NumberType,
106         MinType
107     };
108 
type()109     Type type() const { return m_type; }
110     bool isValid() const;
111 
array()112     const KeyArray& array() const
113     {
114         ASSERT(m_type == ArrayType);
115         return m_array;
116     }
117 
binary()118     PassRefPtr<SharedBuffer> binary() const
119     {
120         ASSERT(m_type == BinaryType);
121         return m_binary;
122     }
123 
string()124     const String& string() const
125     {
126         ASSERT(m_type == StringType);
127         return m_string;
128     }
129 
date()130     double date() const
131     {
132         ASSERT(m_type == DateType);
133         return m_number;
134     }
135 
number()136     double number() const
137     {
138         ASSERT(m_type == NumberType);
139         return m_number;
140     }
141 
142     int compare(const IDBKey* other) const;
143     bool isLessThan(const IDBKey* other) const;
144     bool isEqual(const IDBKey* other) const;
145 
146 private:
IDBKey()147     IDBKey() : m_type(InvalidType), m_number(0) { }
IDBKey(Type type,double number)148     IDBKey(Type type, double number) : m_type(type), m_number(number) { }
IDBKey(const String & value)149     explicit IDBKey(const String& value) : m_type(StringType), m_string(value), m_number(0) { }
IDBKey(PassRefPtr<SharedBuffer> value)150     explicit IDBKey(PassRefPtr<SharedBuffer> value) : m_type(BinaryType), m_binary(value), m_number(0) { }
IDBKey(const KeyArray & keyArray)151     explicit IDBKey(const KeyArray& keyArray) : m_type(ArrayType), m_array(keyArray), m_number(0) { }
152 
153     const Type m_type;
154     const KeyArray m_array;
155     RefPtr<SharedBuffer> m_binary;
156     const String m_string;
157     const double m_number;
158 };
159 
160 } // namespace WebCore
161 
162 #endif // IDBKey_h
163