tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / storage / 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 #if ENABLE(INDEXED_DATABASE)
30
31 #include "PlatformString.h"
32 #include <wtf/Forward.h>
33 #include <wtf/Threading.h>
34 #include <wtf/Vector.h>
35
36 namespace WebCore {
37
38 class IDBKey : public ThreadSafeRefCounted<IDBKey> {
39 public:
40     typedef Vector<RefPtr<IDBKey> > KeyArray;
41
42     static PassRefPtr<IDBKey> createInvalid()
43     {
44         RefPtr<IDBKey> idbKey(new IDBKey());
45         idbKey->m_type = InvalidType;
46         return idbKey.release();
47     }
48
49     static PassRefPtr<IDBKey> createNumber(double number)
50     {
51         RefPtr<IDBKey> idbKey(new IDBKey());
52         idbKey->m_type = NumberType;
53         idbKey->m_number = number;
54         return idbKey.release();
55     }
56
57     static PassRefPtr<IDBKey> createString(const String& string)
58     {
59         RefPtr<IDBKey> idbKey(new IDBKey());
60         idbKey->m_type = StringType;
61         idbKey->m_string = string;
62         return idbKey.release();
63     }
64
65     static PassRefPtr<IDBKey> createDate(double date)
66     {
67         RefPtr<IDBKey> idbKey(new IDBKey());
68         idbKey->m_type = DateType;
69         idbKey->m_date = date;
70         return idbKey.release();
71     }
72
73     static PassRefPtr<IDBKey> createArray(const KeyArray& array)
74     {
75         RefPtr<IDBKey> idbKey(new IDBKey());
76         idbKey->m_type = ArrayType;
77         idbKey->m_array = array;
78         return idbKey.release();
79     }
80
81     ~IDBKey();
82
83     // In order of the least to the highest precedent in terms of sort order.
84     enum Type {
85         InvalidType = 0,
86         ArrayType,
87         StringType,
88         DateType,
89         NumberType,
90         MinType
91     };
92
93     Type type() const { return m_type; }
94     bool valid() const { return m_type != InvalidType; }
95
96     const KeyArray& array() const
97     {
98         ASSERT(m_type == ArrayType);
99         return m_array;
100     }
101
102     const String& string() const
103     {
104         ASSERT(m_type == StringType);
105         return m_string;
106     }
107
108     double date() const
109     {
110         ASSERT(m_type == DateType);
111         return m_date;
112     }
113
114     double number() const
115     {
116         ASSERT(m_type == NumberType);
117         return m_number;
118     }
119
120     int compare(const IDBKey* other) const;
121     bool isLessThan(const IDBKey* other) const;
122     bool isEqual(const IDBKey* other) const;
123
124     static int compareTypes(Type a, Type b)
125     {
126         return b - a;
127     }
128
129     using ThreadSafeRefCounted<IDBKey>::ref;
130     using ThreadSafeRefCounted<IDBKey>::deref;
131
132 private:
133     IDBKey();
134
135     Type m_type;
136     KeyArray m_array;
137     String m_string;
138     double m_date;
139     double m_number;
140 };
141
142 }
143
144 #endif // ENABLE(INDEXED_DATABASE)
145
146 #endif // IDBKey_h