Line data Source code
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/. */
6 :
7 : #ifndef nsVariant_h
8 : #define nsVariant_h
9 :
10 : #include "nsIVariant.h"
11 : #include "nsStringFwd.h"
12 : #include "mozilla/Attributes.h"
13 : #include "nsCycleCollectionParticipant.h"
14 :
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.
19 : */
20 : #define nsAUTF8String nsACString
21 : #define nsUTF8String nsCString
22 : #define PromiseFlatUTF8String PromiseFlatCString
23 :
24 : /**
25 : * nsDiscriminatedUnion is a class that nsIVariant implementors can use
26 : * to hold the underlying data.
27 : */
28 :
29 : class nsDiscriminatedUnion
30 : {
31 : public:
32 :
33 100 : nsDiscriminatedUnion() : mType(nsIDataType::VTYPE_EMPTY) {}
34 : nsDiscriminatedUnion(const nsDiscriminatedUnion&) = delete;
35 : nsDiscriminatedUnion(nsDiscriminatedUnion&&) = delete;
36 :
37 57 : ~nsDiscriminatedUnion() { Cleanup(); }
38 :
39 : nsDiscriminatedUnion& operator=(const nsDiscriminatedUnion&) = delete;
40 : nsDiscriminatedUnion& operator=(nsDiscriminatedUnion&&) = delete;
41 :
42 : void Cleanup();
43 :
44 82 : uint16_t GetType() const { return mType; }
45 :
46 : MOZ_MUST_USE nsresult ConvertToInt8(uint8_t* aResult) const;
47 : MOZ_MUST_USE nsresult ConvertToInt16(int16_t* aResult) const;
48 : MOZ_MUST_USE nsresult ConvertToInt32(int32_t* aResult) const;
49 : MOZ_MUST_USE nsresult ConvertToInt64(int64_t* aResult) const;
50 : MOZ_MUST_USE nsresult ConvertToUint8(uint8_t* aResult) const;
51 : MOZ_MUST_USE nsresult ConvertToUint16(uint16_t* aResult) const;
52 : MOZ_MUST_USE nsresult ConvertToUint32(uint32_t* aResult) const;
53 : MOZ_MUST_USE nsresult ConvertToUint64(uint64_t* aResult) const;
54 : MOZ_MUST_USE nsresult ConvertToFloat(float* aResult) const;
55 : MOZ_MUST_USE nsresult ConvertToDouble(double* aResult) const;
56 : MOZ_MUST_USE nsresult ConvertToBool(bool* aResult) const;
57 : MOZ_MUST_USE nsresult ConvertToChar(char* aResult) const;
58 : MOZ_MUST_USE nsresult ConvertToWChar(char16_t* aResult) const;
59 :
60 : MOZ_MUST_USE nsresult ConvertToID(nsID* aResult) const;
61 :
62 : MOZ_MUST_USE nsresult ConvertToAString(nsAString& aResult) const;
63 : MOZ_MUST_USE nsresult ConvertToAUTF8String(nsAUTF8String& aResult) const;
64 : MOZ_MUST_USE nsresult ConvertToACString(nsACString& aResult) const;
65 : MOZ_MUST_USE nsresult ConvertToString(char** aResult) const;
66 : MOZ_MUST_USE nsresult ConvertToWString(char16_t** aResult) const;
67 : MOZ_MUST_USE nsresult ConvertToStringWithSize(uint32_t* aSize, char** aStr) const;
68 : MOZ_MUST_USE nsresult ConvertToWStringWithSize(uint32_t* aSize, char16_t** aStr) const;
69 :
70 : MOZ_MUST_USE nsresult ConvertToISupports(nsISupports** aResult) const;
71 : MOZ_MUST_USE nsresult ConvertToInterface(nsIID** aIID, void** aInterface) const;
72 : MOZ_MUST_USE nsresult ConvertToArray(uint16_t* aType, nsIID* aIID,
73 : uint32_t* aCount, void** aPtr) const;
74 :
75 : MOZ_MUST_USE nsresult SetFromVariant(nsIVariant* aValue);
76 :
77 : void SetFromInt8(uint8_t aValue);
78 : void SetFromInt16(int16_t aValue);
79 : void SetFromInt32(int32_t aValue);
80 : void SetFromInt64(int64_t aValue);
81 : void SetFromUint8(uint8_t aValue);
82 : void SetFromUint16(uint16_t aValue);
83 : void SetFromUint32(uint32_t aValue);
84 : void SetFromUint64(uint64_t aValue);
85 : void SetFromFloat(float aValue);
86 : void SetFromDouble(double aValue);
87 : void SetFromBool(bool aValue);
88 : void SetFromChar(char aValue);
89 : void SetFromWChar(char16_t aValue);
90 : void SetFromID(const nsID& aValue);
91 : void SetFromAString(const nsAString& aValue);
92 : void SetFromDOMString(const nsAString& aValue);
93 : void SetFromAUTF8String(const nsAUTF8String& aValue);
94 : void SetFromACString(const nsACString& aValue);
95 : MOZ_MUST_USE nsresult SetFromString(const char* aValue);
96 : MOZ_MUST_USE nsresult SetFromWString(const char16_t* aValue);
97 : void SetFromISupports(nsISupports* aValue);
98 : void SetFromInterface(const nsIID& aIID, nsISupports* aValue);
99 : MOZ_MUST_USE nsresult SetFromArray(uint16_t aType, const nsIID* aIID,
100 : uint32_t aCount, void* aValue);
101 : MOZ_MUST_USE nsresult SetFromStringWithSize(uint32_t aSize,
102 : const char* aValue);
103 : MOZ_MUST_USE nsresult SetFromWStringWithSize(uint32_t aSize,
104 : const char16_t* aValue);
105 :
106 : // Like SetFromWStringWithSize, but leaves the string uninitialized. It does
107 : // does write the null-terminator.
108 : void AllocateWStringWithSize(uint32_t aSize);
109 :
110 : void SetToVoid();
111 : void SetToEmpty();
112 : void SetToEmptyArray();
113 :
114 : void Traverse(nsCycleCollectionTraversalCallback& aCb) const;
115 :
116 : private:
117 : MOZ_MUST_USE nsresult
118 : ToManageableNumber(nsDiscriminatedUnion* aOutData) const;
119 : void FreeArray();
120 : MOZ_MUST_USE bool String2ID(nsID* aPid) const;
121 : MOZ_MUST_USE nsresult ToString(nsACString& aOutString) const;
122 :
123 : public:
124 : union
125 : {
126 : int8_t mInt8Value;
127 : int16_t mInt16Value;
128 : int32_t mInt32Value;
129 : int64_t mInt64Value;
130 : uint8_t mUint8Value;
131 : uint16_t mUint16Value;
132 : uint32_t mUint32Value;
133 : uint64_t mUint64Value;
134 : float mFloatValue;
135 : double mDoubleValue;
136 : bool mBoolValue;
137 : char mCharValue;
138 : char16_t mWCharValue;
139 : nsIID mIDValue;
140 : nsAString* mAStringValue;
141 : nsAUTF8String* mUTF8StringValue;
142 : nsACString* mCStringValue;
143 : struct
144 : {
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 : {
153 : nsIID mArrayInterfaceID;
154 : void* mArrayValue;
155 : uint32_t mArrayCount;
156 : uint16_t mArrayType;
157 : } array;
158 : struct
159 : {
160 : char* mStringValue;
161 : uint32_t mStringLength;
162 : } str;
163 : struct
164 : {
165 : char16_t* mWStringValue;
166 : uint32_t mWStringLength;
167 : } wstr;
168 : } u;
169 : uint16_t mType;
170 : };
171 :
172 : /**
173 : * nsVariant implements the generic variant support. The xpcom module registers
174 : * a factory (see NS_VARIANT_CONTRACTID in nsIVariant.idl) that will create
175 : * these objects. They are created 'empty' and 'writable'.
176 : *
177 : * nsIVariant users won't usually need to see this class.
178 : */
179 : class nsVariantBase : public nsIWritableVariant
180 : {
181 : public:
182 : NS_DECL_NSIVARIANT
183 : NS_DECL_NSIWRITABLEVARIANT
184 :
185 : nsVariantBase();
186 :
187 : protected:
188 43 : ~nsVariantBase() {};
189 :
190 : nsDiscriminatedUnion mData;
191 : bool mWritable;
192 : };
193 :
194 : class nsVariant final : public nsVariantBase
195 : {
196 : public:
197 : NS_DECL_ISUPPORTS
198 :
199 86 : nsVariant() {};
200 :
201 : private:
202 43 : ~nsVariant() {};
203 : };
204 :
205 : class nsVariantCC final : public nsVariantBase
206 : {
207 : public:
208 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
209 0 : NS_DECL_CYCLE_COLLECTION_CLASS(nsVariantCC)
210 :
211 0 : nsVariantCC() {};
212 :
213 : private:
214 0 : ~nsVariantCC() {};
215 : };
216 :
217 : /**
218 : * Users of nsIVariant should be using the contractID and not this CID.
219 : * - see NS_VARIANT_CONTRACTID in nsIVariant.idl.
220 : */
221 :
222 : #define NS_VARIANT_CID \
223 : { /* 0D6EA1D0-879C-11d5-90EF-0010A4E73D9A */ \
224 : 0xd6ea1d0, \
225 : 0x879c, \
226 : 0x11d5, \
227 : {0x90, 0xef, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a}}
228 :
229 : #endif // nsVariant_h
|