1 /* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * John Bandhauer <jband@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* The long avoided variant support for xpcom. */
42 #include "nsIVariant.h"
43 #include "nsStringFwd.h"
44 #include "xpt_struct.h"
46 class nsCycleCollectionTraversalCallback
;
49 * Map the nsAUTF8String, nsUTF8String classes to the nsACString and
50 * nsCString classes respectively for now. These defines need to be removed
51 * once Jag lands his nsUTF8String implementation.
53 #define nsAUTF8String nsACString
54 #define nsUTF8String nsCString
55 #define PromiseFlatUTF8String PromiseFlatCString
58 * nsDiscriminatedUnion is a type that nsIVariant implementors *may* use
59 * to hold underlying data. It has no methods. So, its use requires no linkage
60 * to the xpcom module.
63 struct nsDiscriminatedUnion
71 PRUint16 mUint16Value
;
72 PRUint32 mUint32Value
;
73 PRUint64 mUint64Value
;
78 PRUnichar mWCharValue
;
80 nsAString
* mAStringValue
;
81 nsAUTF8String
* mUTF8StringValue
;
82 nsACString
* mCStringValue
;
84 nsISupports
* mInterfaceValue
;
88 nsIID mArrayInterfaceID
;
95 PRUint32 mStringLength
;
98 PRUnichar
* mWStringValue
;
99 PRUint32 mWStringLength
;
106 * nsVariant implements the generic variant support. The xpcom module registers
107 * a factory (see NS_VARIANT_CONTRACTID in nsIVariant.idl) that will create
108 * these objects. They are created 'empty' and 'writable'.
110 * nsIVariant users won't usually need to see this class.
112 * This class also has static helper methods that nsIVariant *implementors* can
113 * use to help them do all the 'standard' nsIVariant data conversions.
116 class NS_COM nsVariant
: public nsIWritableVariant
121 NS_DECL_NSIWRITABLEVARIANT
125 static nsresult
Initialize(nsDiscriminatedUnion
* data
);
126 static nsresult
Cleanup(nsDiscriminatedUnion
* data
);
128 static nsresult
ConvertToInt8(const nsDiscriminatedUnion
& data
, PRUint8
*_retval
);
129 static nsresult
ConvertToInt16(const nsDiscriminatedUnion
& data
, PRInt16
*_retval
);
130 static nsresult
ConvertToInt32(const nsDiscriminatedUnion
& data
, PRInt32
*_retval
);
131 static nsresult
ConvertToInt64(const nsDiscriminatedUnion
& data
, PRInt64
*_retval
);
132 static nsresult
ConvertToUint8(const nsDiscriminatedUnion
& data
, PRUint8
*_retval
);
133 static nsresult
ConvertToUint16(const nsDiscriminatedUnion
& data
, PRUint16
*_retval
);
134 static nsresult
ConvertToUint32(const nsDiscriminatedUnion
& data
, PRUint32
*_retval
);
135 static nsresult
ConvertToUint64(const nsDiscriminatedUnion
& data
, PRUint64
*_retval
);
136 static nsresult
ConvertToFloat(const nsDiscriminatedUnion
& data
, float *_retval
);
137 static nsresult
ConvertToDouble(const nsDiscriminatedUnion
& data
, double *_retval
);
138 static nsresult
ConvertToBool(const nsDiscriminatedUnion
& data
, PRBool
*_retval
);
139 static nsresult
ConvertToChar(const nsDiscriminatedUnion
& data
, char *_retval
);
140 static nsresult
ConvertToWChar(const nsDiscriminatedUnion
& data
, PRUnichar
*_retval
);
141 static nsresult
ConvertToID(const nsDiscriminatedUnion
& data
, nsID
* _retval
);
142 static nsresult
ConvertToAString(const nsDiscriminatedUnion
& data
, nsAString
& _retval
);
143 static nsresult
ConvertToAUTF8String(const nsDiscriminatedUnion
& data
, nsAUTF8String
& _retval
);
144 static nsresult
ConvertToACString(const nsDiscriminatedUnion
& data
, nsACString
& _retval
);
145 static nsresult
ConvertToString(const nsDiscriminatedUnion
& data
, char **_retval
);
146 static nsresult
ConvertToWString(const nsDiscriminatedUnion
& data
, PRUnichar
**_retval
);
147 static nsresult
ConvertToISupports(const nsDiscriminatedUnion
& data
, nsISupports
**_retval
);
148 static nsresult
ConvertToInterface(const nsDiscriminatedUnion
& data
, nsIID
* *iid
, void * *iface
);
149 static nsresult
ConvertToArray(const nsDiscriminatedUnion
& data
, PRUint16
*type
, nsIID
* iid
, PRUint32
*count
, void * *ptr
);
150 static nsresult
ConvertToStringWithSize(const nsDiscriminatedUnion
& data
, PRUint32
*size
, char **str
);
151 static nsresult
ConvertToWStringWithSize(const nsDiscriminatedUnion
& data
, PRUint32
*size
, PRUnichar
**str
);
153 static nsresult
SetFromVariant(nsDiscriminatedUnion
* data
, nsIVariant
* aValue
);
155 static nsresult
SetFromInt8(nsDiscriminatedUnion
* data
, PRUint8 aValue
);
156 static nsresult
SetFromInt16(nsDiscriminatedUnion
* data
, PRInt16 aValue
);
157 static nsresult
SetFromInt32(nsDiscriminatedUnion
* data
, PRInt32 aValue
);
158 static nsresult
SetFromInt64(nsDiscriminatedUnion
* data
, PRInt64 aValue
);
159 static nsresult
SetFromUint8(nsDiscriminatedUnion
* data
, PRUint8 aValue
);
160 static nsresult
SetFromUint16(nsDiscriminatedUnion
* data
, PRUint16 aValue
);
161 static nsresult
SetFromUint32(nsDiscriminatedUnion
* data
, PRUint32 aValue
);
162 static nsresult
SetFromUint64(nsDiscriminatedUnion
* data
, PRUint64 aValue
);
163 static nsresult
SetFromFloat(nsDiscriminatedUnion
* data
, float aValue
);
164 static nsresult
SetFromDouble(nsDiscriminatedUnion
* data
, double aValue
);
165 static nsresult
SetFromBool(nsDiscriminatedUnion
* data
, PRBool aValue
);
166 static nsresult
SetFromChar(nsDiscriminatedUnion
* data
, char aValue
);
167 static nsresult
SetFromWChar(nsDiscriminatedUnion
* data
, PRUnichar aValue
);
168 static nsresult
SetFromID(nsDiscriminatedUnion
* data
, const nsID
& aValue
);
169 static nsresult
SetFromAString(nsDiscriminatedUnion
* data
, const nsAString
& aValue
);
170 static nsresult
SetFromAUTF8String(nsDiscriminatedUnion
* data
, const nsAUTF8String
& aValue
);
171 static nsresult
SetFromACString(nsDiscriminatedUnion
* data
, const nsACString
& aValue
);
172 static nsresult
SetFromString(nsDiscriminatedUnion
* data
, const char *aValue
);
173 static nsresult
SetFromWString(nsDiscriminatedUnion
* data
, const PRUnichar
*aValue
);
174 static nsresult
SetFromISupports(nsDiscriminatedUnion
* data
, nsISupports
*aValue
);
175 static nsresult
SetFromInterface(nsDiscriminatedUnion
* data
, const nsIID
& iid
, nsISupports
*aValue
);
176 static nsresult
SetFromArray(nsDiscriminatedUnion
* data
, PRUint16 type
, const nsIID
* iid
, PRUint32 count
, void * aValue
);
177 static nsresult
SetFromStringWithSize(nsDiscriminatedUnion
* data
, PRUint32 size
, const char *aValue
);
178 static nsresult
SetFromWStringWithSize(nsDiscriminatedUnion
* data
, PRUint32 size
, const PRUnichar
*aValue
);
180 static nsresult
SetToVoid(nsDiscriminatedUnion
* data
);
181 static nsresult
SetToEmpty(nsDiscriminatedUnion
* data
);
182 static nsresult
SetToEmptyArray(nsDiscriminatedUnion
* data
);
184 static void Traverse(const nsDiscriminatedUnion
& data
,
185 nsCycleCollectionTraversalCallback
&cb
);
191 nsDiscriminatedUnion mData
;
196 * Users of nsIVariant should be using the contractID and not this CID.
197 * - see NS_VARIANT_CONTRACTID in nsIVariant.idl.
200 #define NS_VARIANT_CID \
201 { /* 0D6EA1D0-879C-11d5-90EF-0010A4E73D9A */ \
205 {0x90, 0xef, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a}}
207 #define NS_VARIANT_CLASSNAME "Variant"