Bug 588735 - Mirror glass caption buttons for rtl windows. r=roc, a=blocking-betaN.
[mozilla-central.git] / storage / src / Variant.h
blobc1b06d2896bd3be891bfc81c7044e5556b45fcd2
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 * Drew Willcoxon <adw@mozilla.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef mozilla_storage_Variant_h__
42 #define mozilla_storage_Variant_h__
44 #include <utility>
46 #include "nsIVariant.h"
47 #include "nsString.h"
48 #include "nsTArray.h"
50 /**
51 * This class is used by the storage module whenever an nsIVariant needs to be
52 * returned. We provide traits for the basic sqlite types to make use easier.
53 * The following types map to the indicated sqlite type:
54 * PRInt64 -> INTEGER (use IntegerVariant)
55 * double -> FLOAT (use FloatVariant)
56 * nsString -> TEXT (use TextVariant)
57 * nsCString -> TEXT (use UTF8TextVariant)
58 * PRUint8[] -> BLOB (use BlobVariant)
59 * nsnull -> NULL (use NullVariant)
62 namespace mozilla {
63 namespace storage {
65 ////////////////////////////////////////////////////////////////////////////////
66 //// Base Class
68 class Variant_base : public nsIVariant
70 public:
71 NS_DECL_ISUPPORTS
72 NS_DECL_NSIVARIANT
74 protected:
75 virtual ~Variant_base() { }
78 ////////////////////////////////////////////////////////////////////////////////
79 //// Traits
81 /**
82 * Generics
85 template <typename DataType>
86 struct variant_traits
88 static inline PRUint16 type() { return nsIDataType::VTYPE_EMPTY; }
91 template <typename DataType>
92 struct variant_storage_traits
94 typedef DataType ConstructorType;
95 typedef DataType StorageType;
96 static inline StorageType storage_conversion(ConstructorType aData) { return aData; }
99 #define NO_CONVERSION return NS_ERROR_CANNOT_CONVERT_DATA;
101 template <typename DataType>
102 struct variant_integer_traits
104 typedef typename variant_storage_traits<DataType>::StorageType StorageType;
105 static inline nsresult asInt32(StorageType, PRInt32 *) { NO_CONVERSION }
106 static inline nsresult asInt64(StorageType, PRInt64 *) { NO_CONVERSION }
109 template <typename DataType>
110 struct variant_float_traits
112 typedef typename variant_storage_traits<DataType>::StorageType StorageType;
113 static inline nsresult asDouble(StorageType, double *) { NO_CONVERSION }
116 template <typename DataType>
117 struct variant_text_traits
119 typedef typename variant_storage_traits<DataType>::StorageType StorageType;
120 static inline nsresult asUTF8String(StorageType, nsACString &) { NO_CONVERSION }
121 static inline nsresult asString(StorageType, nsAString &) { NO_CONVERSION }
124 template <typename DataType>
125 struct variant_blob_traits
127 typedef typename variant_storage_traits<DataType>::StorageType StorageType;
128 static inline nsresult asArray(StorageType, PRUint16 *, PRUint32 *, void **)
129 { NO_CONVERSION }
132 #undef NO_CONVERSION
135 * INTEGER types
138 template < >
139 struct variant_traits<PRInt64>
141 static inline PRUint16 type() { return nsIDataType::VTYPE_INT64; }
143 template < >
144 struct variant_integer_traits<PRInt64>
146 static inline nsresult asInt32(PRInt64 aValue,
147 PRInt32 *_result)
149 if (aValue > PR_INT32_MAX || aValue < PR_INT32_MIN)
150 return NS_ERROR_CANNOT_CONVERT_DATA;
152 *_result = static_cast<PRInt32>(aValue);
153 return NS_OK;
155 static inline nsresult asInt64(PRInt64 aValue,
156 PRInt64 *_result)
158 *_result = aValue;
159 return NS_OK;
162 // xpcvariant just calls get double for integers...
163 template < >
164 struct variant_float_traits<PRInt64>
166 static inline nsresult asDouble(PRInt64 aValue,
167 double *_result)
169 *_result = double(aValue);
170 return NS_OK;
175 * FLOAT types
178 template < >
179 struct variant_traits<double>
181 static inline PRUint16 type() { return nsIDataType::VTYPE_DOUBLE; }
183 template < >
184 struct variant_float_traits<double>
186 static inline nsresult asDouble(double aValue,
187 double *_result)
189 *_result = aValue;
190 return NS_OK;
195 * TEXT types
198 template < >
199 struct variant_traits<nsString>
201 static inline PRUint16 type() { return nsIDataType::VTYPE_ASTRING; }
203 template < >
204 struct variant_storage_traits<nsString>
206 typedef const nsAString & ConstructorType;
207 typedef nsString StorageType;
208 static inline StorageType storage_conversion(ConstructorType aText)
210 return StorageType(aText);
213 template < >
214 struct variant_text_traits<nsString>
216 static inline nsresult asUTF8String(const nsString &aValue,
217 nsACString &_result)
219 CopyUTF16toUTF8(aValue, _result);
220 return NS_OK;
222 static inline nsresult asString(const nsString &aValue,
223 nsAString &_result)
225 _result = aValue;
226 return NS_OK;
230 template < >
231 struct variant_traits<nsCString>
233 static inline PRUint16 type() { return nsIDataType::VTYPE_UTF8STRING; }
235 template < >
236 struct variant_storage_traits<nsCString>
238 typedef const nsACString & ConstructorType;
239 typedef nsCString StorageType;
240 static inline StorageType storage_conversion(ConstructorType aText)
242 return StorageType(aText);
245 template < >
246 struct variant_text_traits<nsCString>
248 static inline nsresult asUTF8String(const nsCString &aValue,
249 nsACString &_result)
251 _result = aValue;
252 return NS_OK;
254 static inline nsresult asString(const nsCString &aValue,
255 nsAString &_result)
257 CopyUTF8toUTF16(aValue, _result);
258 return NS_OK;
263 * BLOB types
266 template < >
267 struct variant_traits<PRUint8[]>
269 static inline PRUint16 type() { return nsIDataType::VTYPE_ARRAY; }
271 template < >
272 struct variant_storage_traits<PRUint8[]>
274 typedef std::pair<const void *, int> ConstructorType;
275 typedef nsTArray<PRUint8> StorageType;
276 static inline StorageType storage_conversion(ConstructorType aBlob)
278 nsTArray<PRUint8> data(aBlob.second);
279 (void)data.AppendElements(static_cast<const PRUint8 *>(aBlob.first),
280 aBlob.second);
281 return data;
284 template < >
285 struct variant_blob_traits<PRUint8[]>
287 static inline nsresult asArray(nsTArray<PRUint8> &aData,
288 PRUint16 *_type,
289 PRUint32 *_size,
290 void **_result)
292 // For empty blobs, we return nsnull.
293 if (aData.Length() == 0) {
294 *_result = nsnull;
295 *_type = nsIDataType::VTYPE_UINT8;
296 *_size = 0;
297 return NS_OK;
300 // Otherwise, we copy the array.
301 *_result = nsMemory::Clone(aData.Elements(), aData.Length() * sizeof(PRUint8));
302 NS_ENSURE_TRUE(*_result, NS_ERROR_OUT_OF_MEMORY);
304 // Set type and size
305 *_type = nsIDataType::VTYPE_UINT8;
306 *_size = aData.Length();
307 return NS_OK;
312 * NULL type
315 class NullVariant : public Variant_base
317 public:
318 NS_IMETHOD GetDataType(PRUint16 *_type)
320 NS_ENSURE_ARG_POINTER(_type);
321 *_type = nsIDataType::VTYPE_EMPTY;
322 return NS_OK;
325 NS_IMETHOD GetAsAUTF8String(nsACString &_str)
327 // Return a void string.
328 _str.Truncate(0);
329 _str.SetIsVoid(PR_TRUE);
330 return NS_OK;
333 NS_IMETHOD GetAsAString(nsAString &_str)
335 // Return a void string.
336 _str.Truncate(0);
337 _str.SetIsVoid(PR_TRUE);
338 return NS_OK;
342 ////////////////////////////////////////////////////////////////////////////////
343 //// Template Implementation
345 template <typename DataType>
346 class Variant : public Variant_base
348 public:
349 Variant(typename variant_storage_traits<DataType>::ConstructorType aData)
350 : mData(variant_storage_traits<DataType>::storage_conversion(aData))
354 NS_IMETHOD GetDataType(PRUint16 *_type)
356 *_type = variant_traits<DataType>::type();
357 return NS_OK;
359 NS_IMETHOD GetAsInt32(PRInt32 *_integer)
361 return variant_integer_traits<DataType>::asInt32(mData, _integer);
364 NS_IMETHOD GetAsInt64(PRInt64 *_integer)
366 return variant_integer_traits<DataType>::asInt64(mData, _integer);
369 NS_IMETHOD GetAsDouble(double *_double)
371 return variant_float_traits<DataType>::asDouble(mData, _double);
374 NS_IMETHOD GetAsAUTF8String(nsACString &_str)
376 return variant_text_traits<DataType>::asUTF8String(mData, _str);
379 NS_IMETHOD GetAsAString(nsAString &_str)
381 return variant_text_traits<DataType>::asString(mData, _str);
384 NS_IMETHOD GetAsArray(PRUint16 *_type,
385 nsIID *,
386 PRUint32 *_size,
387 void **_data)
389 return variant_blob_traits<DataType>::asArray(mData, _type, _size, _data);
392 private:
393 typename variant_storage_traits<DataType>::StorageType mData;
396 ////////////////////////////////////////////////////////////////////////////////
397 //// Handy typedefs! Use these for the right mapping.
399 typedef Variant<PRInt64> IntegerVariant;
400 typedef Variant<double> FloatVariant;
401 typedef Variant<nsString> TextVariant;
402 typedef Variant<nsCString> UTF8TextVariant;
403 typedef Variant<PRUint8[]> BlobVariant;
405 } // namespace storage
406 } // namespace mozilla
408 #include "Variant_inl.h"
410 #endif // mozilla_storage_Variant_h__