DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (fddffdeab170)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 nsVariant_h
#define nsVariant_h

#include "nsIVariant.h"
#include "nsStringFwd.h"
#include "mozilla/Attributes.h"
#include "nsCycleCollectionParticipant.h"

/**
 * Map the nsAUTF8String, nsUTF8String classes to the nsACString and
 * nsCString classes respectively for now.  These defines need to be removed
 * once Jag lands his nsUTF8String implementation.
 */
#define nsAUTF8String nsACString
#define nsUTF8String nsCString
#define PromiseFlatUTF8String PromiseFlatCString

/**
 * nsDiscriminatedUnion is a class that nsIVariant implementors can use
 * to hold the underlying data.
 */

class nsDiscriminatedUnion
{
public:

  nsDiscriminatedUnion() : mType(nsIDataType::VTYPE_EMPTY) {}
  nsDiscriminatedUnion(const nsDiscriminatedUnion&) = delete;
  nsDiscriminatedUnion(nsDiscriminatedUnion&&) = delete;

  ~nsDiscriminatedUnion() { Cleanup(); }

  nsDiscriminatedUnion& operator=(const nsDiscriminatedUnion&) = delete;
  nsDiscriminatedUnion& operator=(nsDiscriminatedUnion&&) = delete;

  void Cleanup();

  uint16_t GetType() const { return mType; }

  nsresult ConvertToInt8(uint8_t* aResult) const;
  nsresult ConvertToInt16(int16_t* aResult) const;
  nsresult ConvertToInt32(int32_t* aResult) const;
  nsresult ConvertToInt64(int64_t* aResult) const;
  nsresult ConvertToUint8(uint8_t* aResult) const;
  nsresult ConvertToUint16(uint16_t* aResult) const;
  nsresult ConvertToUint32(uint32_t* aResult) const;
  nsresult ConvertToUint64(uint64_t* aResult) const;
  nsresult ConvertToFloat(float* aResult) const;
  nsresult ConvertToDouble(double* aResult) const;
  nsresult ConvertToBool(bool* aResult) const;
  nsresult ConvertToChar(char* aResult) const;
  nsresult ConvertToWChar(char16_t* aResult) const;

  nsresult ConvertToID(nsID* aResult) const;

  nsresult ConvertToAString(nsAString& aResult) const;
  nsresult ConvertToAUTF8String(nsAUTF8String& aResult) const;
  nsresult ConvertToACString(nsACString& aResult) const;
  nsresult ConvertToString(char** aResult) const;
  nsresult ConvertToWString(char16_t** aResult) const;
  nsresult ConvertToStringWithSize(uint32_t* aSize, char** aStr) const;
  nsresult ConvertToWStringWithSize(uint32_t* aSize, char16_t** aStr) const;

  nsresult ConvertToISupports(nsISupports** aResult) const;
  nsresult ConvertToInterface(nsIID** aIID, void** aInterface) const;
  nsresult ConvertToArray(uint16_t* aType, nsIID* aIID,
                          uint32_t* aCount, void** aPtr) const;

  nsresult SetFromVariant(nsIVariant* aValue);

  nsresult SetFromInt8(uint8_t aValue);
  nsresult SetFromInt16(int16_t aValue);
  nsresult SetFromInt32(int32_t aValue);
  nsresult SetFromInt64(int64_t aValue);
  nsresult SetFromUint8(uint8_t aValue);
  nsresult SetFromUint16(uint16_t aValue);
  nsresult SetFromUint32(uint32_t aValue);
  nsresult SetFromUint64(uint64_t aValue);
  nsresult SetFromFloat(float aValue);
  nsresult SetFromDouble(double aValue);
  nsresult SetFromBool(bool aValue);
  nsresult SetFromChar(char aValue);
  nsresult SetFromWChar(char16_t aValue);
  nsresult SetFromID(const nsID& aValue);
  nsresult SetFromAString(const nsAString& aValue);
  nsresult SetFromDOMString(const nsAString& aValue);
  nsresult SetFromAUTF8String(const nsAUTF8String& aValue);
  nsresult SetFromACString(const nsACString& aValue);
  nsresult SetFromString(const char* aValue);
  nsresult SetFromWString(const char16_t* aValue);
  nsresult SetFromISupports(nsISupports* aValue);
  nsresult SetFromInterface(const nsIID& aIID, nsISupports* aValue);
  nsresult SetFromArray(uint16_t aType, const nsIID* aIID, uint32_t aCount,
                        void* aValue);
  nsresult SetFromStringWithSize(uint32_t aSize, const char* aValue);
  nsresult SetFromWStringWithSize(uint32_t aSize, const char16_t* aValue);

  // Like SetFromWStringWithSize, but leaves the string uninitialized. It does
  // does write the null-terminator.
  nsresult AllocateWStringWithSize(uint32_t aSize);

  nsresult SetToVoid();
  nsresult SetToEmpty();
  nsresult SetToEmptyArray();

  void Traverse(nsCycleCollectionTraversalCallback& aCb) const;

private:
  nsresult ToManageableNumber(nsDiscriminatedUnion* aOutData) const;
  void FreeArray();
  bool String2ID(nsID* aPid) const;
  nsresult ToString(nsACString& aOutString) const;

public:
  union
  {
    int8_t         mInt8Value;
    int16_t        mInt16Value;
    int32_t        mInt32Value;
    int64_t        mInt64Value;
    uint8_t        mUint8Value;
    uint16_t       mUint16Value;
    uint32_t       mUint32Value;
    uint64_t       mUint64Value;
    float          mFloatValue;
    double         mDoubleValue;
    bool           mBoolValue;
    char           mCharValue;
    char16_t       mWCharValue;
    nsIID          mIDValue;
    nsAString*     mAStringValue;
    nsAUTF8String* mUTF8StringValue;
    nsACString*    mCStringValue;
    struct
    {
      // This is an owning reference that cannot be an nsCOMPtr because
      // nsDiscriminatedUnion needs to be POD.  AddRef/Release are manually
      // called on this.
      nsISupports* MOZ_OWNING_REF mInterfaceValue;
      nsIID        mInterfaceID;
    } iface;
    struct
    {
      nsIID        mArrayInterfaceID;
      void*        mArrayValue;
      uint32_t     mArrayCount;
      uint16_t     mArrayType;
    } array;
    struct
    {
      char*        mStringValue;
      uint32_t     mStringLength;
    } str;
    struct
    {
      char16_t*    mWStringValue;
      uint32_t     mWStringLength;
    } wstr;
  } u;
  uint16_t       mType;
};

/**
 * nsVariant implements the generic variant support. The xpcom module registers
 * a factory (see NS_VARIANT_CONTRACTID in nsIVariant.idl) that will create
 * these objects. They are created 'empty' and 'writable'.
 *
 * nsIVariant users won't usually need to see this class.
 */
class nsVariantBase : public nsIWritableVariant
{
public:
  NS_DECL_NSIVARIANT
  NS_DECL_NSIWRITABLEVARIANT

  nsVariantBase();

protected:
  ~nsVariantBase() {};

  nsDiscriminatedUnion mData;
  bool mWritable;
};

class nsVariant final : public nsVariantBase
{
public:
  NS_DECL_ISUPPORTS

  nsVariant() {};

private:
  ~nsVariant() {};
};

class nsVariantCC final : public nsVariantBase
{
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_CLASS(nsVariantCC)

  nsVariantCC() {};

private:
  ~nsVariantCC() {};
};

/**
 * Users of nsIVariant should be using the contractID and not this CID.
 *  - see NS_VARIANT_CONTRACTID in nsIVariant.idl.
 */

#define NS_VARIANT_CID \
{ /* 0D6EA1D0-879C-11d5-90EF-0010A4E73D9A */ \
    0xd6ea1d0,                               \
    0x879c,                                  \
    0x11d5,                                  \
    {0x90, 0xef, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a}}

#endif // nsVariant_h