2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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"
38 class IDBKey : public RefCountedWillBeGarbageCollectedFinalized<IDBKey> {
40 typedef WillBeHeapVector<RefPtrWillBeMember<IDBKey> > KeyArray;
42 static PassRefPtrWillBeRawPtr<IDBKey> createInvalid()
44 return adoptRefWillBeNoop(new IDBKey());
47 static PassRefPtrWillBeRawPtr<IDBKey> createNumber(double number)
49 return adoptRefWillBeNoop(new IDBKey(NumberType, number));
52 static PassRefPtrWillBeRawPtr<IDBKey> createBinary(PassRefPtr<SharedBuffer> binary)
54 return adoptRefWillBeNoop(new IDBKey(binary));
57 static PassRefPtrWillBeRawPtr<IDBKey> createString(const String& string)
59 return adoptRefWillBeNoop(new IDBKey(string));
62 static PassRefPtrWillBeRawPtr<IDBKey> createDate(double date)
64 return adoptRefWillBeNoop(new IDBKey(DateType, date));
67 static PassRefPtrWillBeRawPtr<IDBKey> createMultiEntryArray(const KeyArray& array)
71 for (size_t i = 0; i < array.size(); i++) {
72 if (!array[i]->isValid())
76 for (size_t j = 0; j < result.size(); j++) {
77 if (array[i]->isEqual(result[j].get())) {
83 result.append(array[i]);
86 RefPtrWillBeRawPtr<IDBKey> idbKey = adoptRefWillBeNoop(new IDBKey(result));
87 ASSERT(idbKey->isValid());
88 return idbKey.release();
91 static PassRefPtrWillBeRawPtr<IDBKey> createArray(const KeyArray& array)
93 return adoptRefWillBeNoop(new IDBKey(array));
99 // In order of the least to the highest precedent in terms of sort order.
110 Type type() const { return m_type; }
111 bool isValid() const;
113 const KeyArray& array() const
115 ASSERT(m_type == ArrayType);
119 PassRefPtr<SharedBuffer> binary() const
121 ASSERT(m_type == BinaryType);
125 const String& string() const
127 ASSERT(m_type == StringType);
133 ASSERT(m_type == DateType);
137 double number() const
139 ASSERT(m_type == NumberType);
143 int compare(const IDBKey* other) const;
144 bool isLessThan(const IDBKey* other) const;
145 bool isEqual(const IDBKey* other) const;
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) { }
155 const KeyArray m_array;
156 RefPtr<SharedBuffer> m_binary;
157 const String m_string;
158 const double m_number;
161 } // namespace WebCore