Backed out changeset 5f819082a015 (bug 1936189) for causing reftest failures @ native...
[gecko.git] / xpcom / ds / nsVariant.h
blob8de1fe4618105b5cbeb9a304b5b2bf5020fcd279
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsVariant_h
8 #define nsVariant_h
10 #include "nsIVariant.h"
11 #include "nsStringFwd.h"
12 #include "mozilla/Attributes.h"
13 #include "nsCycleCollectionParticipant.h"
15 /**
16 * Map the nsAUTF8String, nsUTF8String classes to the nsACString and
17 * nsCString classes respectively for now. These defines need to be removed
18 * once Jag lands his nsUTF8String implementation.
20 #define nsAUTF8String nsACString
21 #define nsUTF8String nsCString
22 #define PromiseFlatUTF8String PromiseFlatCString
24 /**
25 * nsDiscriminatedUnion is a class that nsIVariant implementors can use
26 * to hold the underlying data.
29 class nsDiscriminatedUnion {
30 public:
31 nsDiscriminatedUnion() : mType(nsIDataType::VTYPE_EMPTY) {
32 u.mInt8Value = '\0';
34 nsDiscriminatedUnion(const nsDiscriminatedUnion&) = delete;
35 nsDiscriminatedUnion(nsDiscriminatedUnion&&) = delete;
37 ~nsDiscriminatedUnion() { Cleanup(); }
39 nsDiscriminatedUnion& operator=(const nsDiscriminatedUnion&) = delete;
40 nsDiscriminatedUnion& operator=(nsDiscriminatedUnion&&) = delete;
42 void Cleanup();
44 uint16_t GetType() const { return mType; }
46 [[nodiscard]] nsresult ConvertToInt8(uint8_t* aResult) const;
47 [[nodiscard]] nsresult ConvertToInt16(int16_t* aResult) const;
48 [[nodiscard]] nsresult ConvertToInt32(int32_t* aResult) const;
49 [[nodiscard]] nsresult ConvertToInt64(int64_t* aResult) const;
50 [[nodiscard]] nsresult ConvertToUint8(uint8_t* aResult) const;
51 [[nodiscard]] nsresult ConvertToUint16(uint16_t* aResult) const;
52 [[nodiscard]] nsresult ConvertToUint32(uint32_t* aResult) const;
53 [[nodiscard]] nsresult ConvertToUint64(uint64_t* aResult) const;
54 [[nodiscard]] nsresult ConvertToFloat(float* aResult) const;
55 [[nodiscard]] nsresult ConvertToDouble(double* aResult) const;
56 [[nodiscard]] nsresult ConvertToBool(bool* aResult) const;
57 [[nodiscard]] nsresult ConvertToChar(char* aResult) const;
58 [[nodiscard]] nsresult ConvertToWChar(char16_t* aResult) const;
60 [[nodiscard]] nsresult ConvertToID(nsID* aResult) const;
62 [[nodiscard]] nsresult ConvertToAString(nsAString& aResult) const;
63 [[nodiscard]] nsresult ConvertToAUTF8String(nsAUTF8String& aResult) const;
64 [[nodiscard]] nsresult ConvertToACString(nsACString& aResult) const;
65 [[nodiscard]] nsresult ConvertToString(char** aResult) const;
66 [[nodiscard]] nsresult ConvertToWString(char16_t** aResult) const;
67 [[nodiscard]] nsresult ConvertToStringWithSize(uint32_t* aSize,
68 char** aStr) const;
69 [[nodiscard]] nsresult ConvertToWStringWithSize(uint32_t* aSize,
70 char16_t** aStr) const;
72 [[nodiscard]] nsresult ConvertToISupports(nsISupports** aResult) const;
73 [[nodiscard]] nsresult ConvertToInterface(nsIID** aIID,
74 void** aInterface) const;
75 [[nodiscard]] nsresult ConvertToArray(uint16_t* aType, nsIID* aIID,
76 uint32_t* aCount, void** aPtr) const;
78 [[nodiscard]] nsresult SetFromVariant(nsIVariant* aValue);
80 void SetFromInt8(uint8_t aValue);
81 void SetFromInt16(int16_t aValue);
82 void SetFromInt32(int32_t aValue);
83 void SetFromInt64(int64_t aValue);
84 void SetFromUint8(uint8_t aValue);
85 void SetFromUint16(uint16_t aValue);
86 void SetFromUint32(uint32_t aValue);
87 void SetFromUint64(uint64_t aValue);
88 void SetFromFloat(float aValue);
89 void SetFromDouble(double aValue);
90 void SetFromBool(bool aValue);
91 void SetFromChar(char aValue);
92 void SetFromWChar(char16_t aValue);
93 void SetFromID(const nsID& aValue);
94 void SetFromAString(const nsAString& aValue);
95 void SetFromAUTF8String(const nsAUTF8String& aValue);
96 void SetFromACString(const nsACString& aValue);
97 [[nodiscard]] nsresult SetFromString(const char* aValue);
98 [[nodiscard]] nsresult SetFromWString(const char16_t* aValue);
99 void SetFromISupports(nsISupports* aValue);
100 void SetFromInterface(const nsIID& aIID, nsISupports* aValue);
101 [[nodiscard]] nsresult SetFromArray(uint16_t aType, const nsIID* aIID,
102 uint32_t aCount, void* aValue);
103 [[nodiscard]] nsresult SetFromStringWithSize(uint32_t aSize,
104 const char* aValue);
105 [[nodiscard]] nsresult SetFromWStringWithSize(uint32_t aSize,
106 const char16_t* aValue);
108 // Like SetFromWStringWithSize, but leaves the string uninitialized. It does
109 // does write the null-terminator.
110 void AllocateWStringWithSize(uint32_t aSize);
112 void SetToVoid();
113 void SetToEmpty();
114 void SetToEmptyArray();
116 void Traverse(nsCycleCollectionTraversalCallback& aCb) const;
118 private:
119 [[nodiscard]] nsresult ToManageableNumber(
120 nsDiscriminatedUnion* aOutData) const;
121 void FreeArray();
122 [[nodiscard]] bool String2ID(nsID* aPid) const;
123 [[nodiscard]] nsresult ToString(nsACString& aOutString) const;
125 public:
126 union {
127 int8_t mInt8Value;
128 int16_t mInt16Value;
129 int32_t mInt32Value;
130 int64_t mInt64Value;
131 uint8_t mUint8Value;
132 uint16_t mUint16Value;
133 uint32_t mUint32Value;
134 uint64_t mUint64Value;
135 float mFloatValue;
136 double mDoubleValue;
137 bool mBoolValue;
138 char mCharValue;
139 char16_t mWCharValue;
140 nsIID mIDValue;
141 nsAString* mAStringValue;
142 nsAUTF8String* mUTF8StringValue;
143 nsACString* mCStringValue;
144 struct {
145 // This is an owning reference that cannot be an nsCOMPtr because
146 // nsDiscriminatedUnion needs to be POD. AddRef/Release are manually
147 // called on this.
148 nsISupports* MOZ_OWNING_REF mInterfaceValue;
149 nsIID mInterfaceID;
150 } iface;
151 struct {
152 nsIID mArrayInterfaceID;
153 void* mArrayValue;
154 uint32_t mArrayCount;
155 uint16_t mArrayType;
156 } array;
157 struct {
158 char* mStringValue;
159 uint32_t mStringLength;
160 } str;
161 struct {
162 char16_t* mWStringValue;
163 uint32_t mWStringLength;
164 } wstr;
165 } u;
166 uint16_t mType;
170 * nsVariant implements the generic variant support. The xpcom module registers
171 * a factory (see NS_VARIANT_CONTRACTID in nsIVariant.idl) that will create
172 * these objects. They are created 'empty' and 'writable'.
174 * nsIVariant users won't usually need to see this class.
176 class nsVariantBase : public nsIWritableVariant {
177 public:
178 NS_DECL_NSIVARIANT
179 NS_DECL_NSIWRITABLEVARIANT
181 nsVariantBase();
183 protected:
184 ~nsVariantBase() = default;
186 nsDiscriminatedUnion mData;
187 bool mWritable;
190 class nsVariant final : public nsVariantBase {
191 public:
192 NS_DECL_ISUPPORTS
194 nsVariant() = default;
196 private:
197 ~nsVariant() = default;
200 class nsVariantCC final : public nsVariantBase {
201 public:
202 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
203 NS_DECL_CYCLE_COLLECTION_CLASS(nsVariantCC)
205 nsVariantCC() = default;
207 private:
208 ~nsVariantCC() = default;
212 * Users of nsIVariant should be using the contractID and not this CID.
213 * - see NS_VARIANT_CONTRACTID in nsIVariant.idl.
216 #define NS_VARIANT_CID \
217 { /* 0D6EA1D0-879C-11d5-90EF-0010A4E73D9A */ \
218 0xd6ea1d0, 0x879c, 0x11d5, { \
219 0x90, 0xef, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a \
223 #endif // nsVariant_h