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