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/. */
10 #include "nsIVariant.h"
11 #include "nsStringFwd.h"
12 #include "mozilla/Attributes.h"
13 #include "nsCycleCollectionParticipant.h"
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
25 * nsDiscriminatedUnion is a class that nsIVariant implementors can use
26 * to hold the underlying data.
29 class nsDiscriminatedUnion
{
31 nsDiscriminatedUnion() : mType(nsIDataType::VTYPE_EMPTY
) {
34 nsDiscriminatedUnion(const nsDiscriminatedUnion
&) = delete;
35 nsDiscriminatedUnion(nsDiscriminatedUnion
&&) = delete;
37 ~nsDiscriminatedUnion() { Cleanup(); }
39 nsDiscriminatedUnion
& operator=(const nsDiscriminatedUnion
&) = delete;
40 nsDiscriminatedUnion
& operator=(nsDiscriminatedUnion
&&) = delete;
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
,
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
,
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
);
114 void SetToEmptyArray();
116 void Traverse(nsCycleCollectionTraversalCallback
& aCb
) const;
119 [[nodiscard
]] nsresult
ToManageableNumber(
120 nsDiscriminatedUnion
* aOutData
) const;
122 [[nodiscard
]] bool String2ID(nsID
* aPid
) const;
123 [[nodiscard
]] nsresult
ToString(nsACString
& aOutString
) const;
132 uint16_t mUint16Value
;
133 uint32_t mUint32Value
;
134 uint64_t mUint64Value
;
139 char16_t mWCharValue
;
141 nsAString
* mAStringValue
;
142 nsAUTF8String
* mUTF8StringValue
;
143 nsACString
* mCStringValue
;
145 // This is an owning reference that cannot be an nsCOMPtr because
146 // nsDiscriminatedUnion needs to be POD. AddRef/Release are manually
148 nsISupports
* MOZ_OWNING_REF mInterfaceValue
;
152 nsIID mArrayInterfaceID
;
154 uint32_t mArrayCount
;
159 uint32_t mStringLength
;
162 char16_t
* mWStringValue
;
163 uint32_t mWStringLength
;
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
{
179 NS_DECL_NSIWRITABLEVARIANT
184 ~nsVariantBase() = default;
186 nsDiscriminatedUnion mData
;
190 class nsVariant final
: public nsVariantBase
{
194 nsVariant() = default;
197 ~nsVariant() = default;
200 class nsVariantCC final
: public nsVariantBase
{
202 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
203 NS_DECL_CYCLE_COLLECTION_CLASS(nsVariantCC
)
205 nsVariantCC() = default;
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