summaryrefslogtreecommitdiff
path: root/storage/Variant.h
blob: 265abb02aede4f4750600eb967fa8ff0f5a34650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_storage_Variant_h__
#define mozilla_storage_Variant_h__

#include <utility>

#include "nsIVariant.h"
#include "nsString.h"
#include "nsTArray.h"

#define VARIANT_BASE_IID                                   \
{ /* 78888042-0fa3-4f7a-8b19-7996f99bf1aa */               \
  0x78888042, 0x0fa3, 0x4f7a,                              \
  { 0x8b, 0x19, 0x79, 0x96, 0xf9, 0x9b, 0xf1, 0xaa }       \
}

/**
 * This class is used by the storage module whenever an nsIVariant needs to be
 * returned.  We provide traits for the basic sqlite types to make use easier.
 * The following types map to the indicated sqlite type:
 * int64_t   -> INTEGER (use IntegerVariant)
 * double    -> FLOAT (use FloatVariant)
 * nsString  -> TEXT (use TextVariant)
 * nsCString -> TEXT (use UTF8TextVariant)
 * uint8_t[] -> BLOB (use BlobVariant)
 * nullptr   -> NULL (use NullVariant)
 */

namespace mozilla {
namespace storage {

////////////////////////////////////////////////////////////////////////////////
//// Base Class

class Variant_base : public nsIVariant
{
public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIVARIANT
  NS_DECLARE_STATIC_IID_ACCESSOR(VARIANT_BASE_IID)

protected:
  virtual ~Variant_base() { }
};

NS_DEFINE_STATIC_IID_ACCESSOR(Variant_base,
                              VARIANT_BASE_IID)

////////////////////////////////////////////////////////////////////////////////
//// Traits

/**
 * Generics
 */

template <typename DataType>
struct variant_traits
{
  static inline uint16_t type() { return nsIDataType::VTYPE_EMPTY; }
};

template <typename DataType, bool Adopting=false>
struct variant_storage_traits
{
  typedef DataType ConstructorType;
  typedef DataType StorageType;
  static inline void storage_conversion(const ConstructorType aData, StorageType* _storage)
  {
    *_storage = aData;
  }

  static inline void destroy(const StorageType& _storage)
  { }
};

#define NO_CONVERSION return NS_ERROR_CANNOT_CONVERT_DATA;

template <typename DataType, bool Adopting=false>
struct variant_integer_traits
{
  typedef typename variant_storage_traits<DataType, Adopting>::StorageType StorageType;
  static inline nsresult asInt32(const StorageType &, int32_t *) { NO_CONVERSION }
  static inline nsresult asInt64(const StorageType &, int64_t *) { NO_CONVERSION }
};

template <typename DataType, bool Adopting=false>
struct variant_float_traits
{
  typedef typename variant_storage_traits<DataType, Adopting>::StorageType StorageType;
  static inline nsresult asDouble(const StorageType &, double *) { NO_CONVERSION }
};

template <typename DataType, bool Adopting=false>
struct variant_text_traits
{
  typedef typename variant_storage_traits<DataType, Adopting>::StorageType StorageType;
  static inline nsresult asUTF8String(const StorageType &, nsACString &) { NO_CONVERSION }
  static inline nsresult asString(const StorageType &, nsAString &) { NO_CONVERSION }
};

template <typename DataType, bool Adopting=false>
struct variant_blob_traits
{
  typedef typename variant_storage_traits<DataType, Adopting>::StorageType StorageType;
  static inline nsresult asArray(const StorageType &, uint16_t *, uint32_t *, void **)
  { NO_CONVERSION }
};

#undef NO_CONVERSION

/**
 * INTEGER types
 */

template < >
struct variant_traits<int64_t>
{
  static inline uint16_t type() { return nsIDataType::VTYPE_INT64; }
};
template < >
struct variant_integer_traits<int64_t>
{
  static inline nsresult asInt32(int64_t aValue,
                                 int32_t *_result)
  {
    if (aValue > INT32_MAX || aValue < INT32_MIN)
      return NS_ERROR_CANNOT_CONVERT_DATA;

    *_result = static_cast<int32_t>(aValue);
    return NS_OK;
  }
  static inline nsresult asInt64(int64_t aValue,
                                 int64_t *_result)
  {
    *_result = aValue;
    return NS_OK;
  }
};
// xpcvariant just calls get double for integers...
template < >
struct variant_float_traits<int64_t>
{
  static inline nsresult asDouble(int64_t aValue,
                                  double *_result)
  {
    *_result = double(aValue);
    return NS_OK;
  }
};

/**
 * FLOAT types
 */

template < >
struct variant_traits<double>
{
  static inline uint16_t type() { return nsIDataType::VTYPE_DOUBLE; }
};
template < >
struct variant_float_traits<double>
{
  static inline nsresult asDouble(double aValue,
                                  double *_result)
  {
    *_result = aValue;
    return NS_OK;
  }
};

/**
 * TEXT types
 */

template < >
struct variant_traits<nsString>
{
  static inline uint16_t type() { return nsIDataType::VTYPE_ASTRING; }
};
template < >
struct variant_storage_traits<nsString>
{
  typedef const nsAString & ConstructorType;
  typedef nsString StorageType;
  static inline void storage_conversion(ConstructorType aText, StorageType* _outData)
  {
    *_outData = aText;
  }
  static inline void destroy(const StorageType& _outData)
  { }
};
template < >
struct variant_text_traits<nsString>
{
  static inline nsresult asUTF8String(const nsString &aValue,
                                      nsACString &_result)
  {
    CopyUTF16toUTF8(aValue, _result);
    return NS_OK;
  }
  static inline nsresult asString(const nsString &aValue,
                                  nsAString &_result)
  {
    _result = aValue;
    return NS_OK;
  }
};

template < >
struct variant_traits<nsCString>
{
  static inline uint16_t type() { return nsIDataType::VTYPE_UTF8STRING; }
};
template < >
struct variant_storage_traits<nsCString>
{
  typedef const nsACString & ConstructorType;
  typedef nsCString StorageType;
  static inline void storage_conversion(ConstructorType aText, StorageType* _outData)
  {
    *_outData = aText;
  }
  static inline void destroy(const StorageType &aData)
  { }
};
template < >
struct variant_text_traits<nsCString>
{
  static inline nsresult asUTF8String(const nsCString &aValue,
                                      nsACString &_result)
  {
    _result = aValue;
    return NS_OK;
  }
  static inline nsresult asString(const nsCString &aValue,
                                  nsAString &_result)
  {
    CopyUTF8toUTF16(aValue, _result);
    return NS_OK;
  }
};

/**
 * BLOB types
 */

template < >
struct variant_traits<uint8_t[]>
{
  static inline uint16_t type() { return nsIDataType::VTYPE_ARRAY; }
};
template < >
struct variant_storage_traits<uint8_t[], false>
{
  typedef std::pair<const void *, int> ConstructorType;
  typedef FallibleTArray<uint8_t> StorageType;
  static inline void storage_conversion(ConstructorType aBlob, StorageType* _outData)
  {
    _outData->Clear();
    (void)_outData->AppendElements(static_cast<const uint8_t *>(aBlob.first),
                                   aBlob.second, fallible);
  }
  static inline void destroy(const StorageType& _outData)
  { }
};
template < >
struct variant_storage_traits<uint8_t[], true>
{
  typedef std::pair<uint8_t *, int> ConstructorType;
  typedef std::pair<uint8_t *, int> StorageType;
  static inline void storage_conversion(ConstructorType aBlob, StorageType* _outData)
  {
    *_outData = aBlob;
  }
  static inline void destroy(StorageType &aData)
  {
    if (aData.first) {
      free(aData.first);
      aData.first = nullptr;
    }
  }
};
template < >
struct variant_blob_traits<uint8_t[], false>
{
  static inline nsresult asArray(FallibleTArray<uint8_t> &aData,
                                 uint16_t *_type,
                                 uint32_t *_size,
                                 void **_result)
  {
    // For empty blobs, we return nullptr.
    if (aData.Length() == 0) {
      *_result = nullptr;
      *_type = nsIDataType::VTYPE_UINT8;
      *_size = 0;
      return NS_OK;
    }

    // Otherwise, we copy the array.
    *_result = nsMemory::Clone(aData.Elements(), aData.Length() * sizeof(uint8_t));
    NS_ENSURE_TRUE(*_result, NS_ERROR_OUT_OF_MEMORY);

    // Set type and size
    *_type = nsIDataType::VTYPE_UINT8;
    *_size = aData.Length();
    return NS_OK;
  }
};

template < >
struct variant_blob_traits<uint8_t[], true>
{
  static inline nsresult asArray(std::pair<uint8_t *, int> &aData,
                                 uint16_t *_type,
                                 uint32_t *_size,
                                 void **_result)
  {
    // For empty blobs, we return nullptr.
    if (aData.second == 0) {
      *_result = nullptr;
      *_type = nsIDataType::VTYPE_UINT8;
      *_size = 0;
      return NS_OK;
    }

    // Otherwise, transfer the data out.
    *_result = aData.first;
    aData.first = nullptr;
    MOZ_ASSERT(*_result); // We asked for it twice, better not use adopting!

    // Set type and size
    *_type = nsIDataType::VTYPE_UINT8;
    *_size = aData.second;
    return NS_OK;
  }
};

/**
 * nullptr type
 */

class NullVariant : public Variant_base
{
public:
  NS_IMETHOD GetDataType(uint16_t *_type)
  {
    NS_ENSURE_ARG_POINTER(_type);
    *_type = nsIDataType::VTYPE_EMPTY;
    return NS_OK;
  }

  NS_IMETHOD GetAsAUTF8String(nsACString &_str)
  {
    // Return a void string.
    _str.SetIsVoid(true);
    return NS_OK;
  }

  NS_IMETHOD GetAsAString(nsAString &_str)
  {
    // Return a void string.
    _str.SetIsVoid(true);
    return NS_OK;
  }
};

////////////////////////////////////////////////////////////////////////////////
//// Template Implementation

template <typename DataType, bool Adopting=false>
class Variant final : public Variant_base
{
  ~Variant()
  {
    variant_storage_traits<DataType, Adopting>::destroy(mData);
  }

public:
  explicit Variant(const typename variant_storage_traits<DataType, Adopting>::ConstructorType aData)
  {
    variant_storage_traits<DataType, Adopting>::storage_conversion(aData, &mData);
  }

  NS_IMETHOD GetDataType(uint16_t *_type)
  {
    *_type = variant_traits<DataType>::type();
    return NS_OK;
  }
  NS_IMETHOD GetAsInt32(int32_t *_integer)
  {
    return variant_integer_traits<DataType, Adopting>::asInt32(mData, _integer);
  }

  NS_IMETHOD GetAsInt64(int64_t *_integer)
  {
    return variant_integer_traits<DataType, Adopting>::asInt64(mData, _integer);
  }

  NS_IMETHOD GetAsDouble(double *_double)
  {
    return variant_float_traits<DataType, Adopting>::asDouble(mData, _double);
  }

  NS_IMETHOD GetAsAUTF8String(nsACString &_str)
  {
    return variant_text_traits<DataType, Adopting>::asUTF8String(mData, _str);
  }

  NS_IMETHOD GetAsAString(nsAString &_str)
  {
    return variant_text_traits<DataType, Adopting>::asString(mData, _str);
  }

  NS_IMETHOD GetAsArray(uint16_t *_type,
                        nsIID *,
                        uint32_t *_size,
                        void **_data)
  {
    return variant_blob_traits<DataType, Adopting>::asArray(mData, _type, _size, _data);
  }

private:
  typename variant_storage_traits<DataType, Adopting>::StorageType mData;
};

////////////////////////////////////////////////////////////////////////////////
//// Handy typedefs!  Use these for the right mapping.

typedef Variant<int64_t> IntegerVariant;
typedef Variant<double> FloatVariant;
typedef Variant<nsString> TextVariant;
typedef Variant<nsCString> UTF8TextVariant;
typedef Variant<uint8_t[], false> BlobVariant;
typedef Variant<uint8_t[], true> AdoptedBlobVariant;

} // namespace storage
} // namespace mozilla

#include "Variant_inl.h"

#endif // mozilla_storage_Variant_h__