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 #include "nsSupportsPrimitives.h"
8 #include "mozilla/Assertions.h"
9 #include "mozilla/IntegerPrintfMacros.h"
10 #include "mozilla/Sprintf.h"
14 static char* DataToString(const char* aFormat
, T aData
) {
15 static const int size
= 32;
18 SprintfLiteral(buf
, aFormat
, aData
);
20 return moz_xstrdup(buf
);
23 /*****************************************************************************
25 *****************************************************************************/
27 NS_IMPL_ISUPPORTS(nsSupportsCString
, nsISupportsCString
, nsISupportsPrimitive
)
30 nsSupportsCString::GetType(uint16_t* aType
) {
31 NS_ASSERTION(aType
, "Bad pointer");
32 *aType
= TYPE_CSTRING
;
37 nsSupportsCString::GetData(nsACString
& aData
) {
43 nsSupportsCString::ToString(char** aResult
) {
44 *aResult
= ToNewCString(mData
, mozilla::fallible
);
46 return NS_ERROR_OUT_OF_MEMORY
;
53 nsSupportsCString::SetData(const nsACString
& aData
) {
54 bool ok
= mData
.Assign(aData
, mozilla::fallible
);
56 return NS_ERROR_OUT_OF_MEMORY
;
62 /*****************************************************************************
64 *****************************************************************************/
66 NS_IMPL_ISUPPORTS(nsSupportsString
, nsISupportsString
, nsISupportsPrimitive
)
69 nsSupportsString::GetType(uint16_t* aType
) {
70 NS_ASSERTION(aType
, "Bad pointer");
76 nsSupportsString::GetData(nsAString
& aData
) {
82 nsSupportsString::ToString(char16_t
** aResult
) {
83 *aResult
= ToNewUnicode(mData
, mozilla::fallible
);
85 return NS_ERROR_OUT_OF_MEMORY
;
92 nsSupportsString::SetData(const nsAString
& aData
) {
93 bool ok
= mData
.Assign(aData
, mozilla::fallible
);
95 return NS_ERROR_OUT_OF_MEMORY
;
101 /***************************************************************************/
103 NS_IMPL_ISUPPORTS(nsSupportsPRBool
, nsISupportsPRBool
, nsISupportsPrimitive
)
105 nsSupportsPRBool::nsSupportsPRBool() : mData(false) {}
108 nsSupportsPRBool::GetType(uint16_t* aType
) {
109 NS_ASSERTION(aType
, "Bad pointer");
110 *aType
= TYPE_PRBOOL
;
115 nsSupportsPRBool::GetData(bool* aData
) {
116 NS_ASSERTION(aData
, "Bad pointer");
122 nsSupportsPRBool::SetData(bool aData
) {
128 nsSupportsPRBool::ToString(char** aResult
) {
129 NS_ASSERTION(aResult
, "Bad pointer");
130 *aResult
= moz_xstrdup(mData
? "true" : "false");
134 /***************************************************************************/
136 NS_IMPL_ISUPPORTS(nsSupportsPRUint8
, nsISupportsPRUint8
, nsISupportsPrimitive
)
138 nsSupportsPRUint8::nsSupportsPRUint8() : mData(0) {}
141 nsSupportsPRUint8::GetType(uint16_t* aType
) {
142 NS_ASSERTION(aType
, "Bad pointer");
143 *aType
= TYPE_PRUINT8
;
148 nsSupportsPRUint8::GetData(uint8_t* aData
) {
149 NS_ASSERTION(aData
, "Bad pointer");
155 nsSupportsPRUint8::SetData(uint8_t aData
) {
161 nsSupportsPRUint8::ToString(char** aResult
) {
162 NS_ASSERTION(aResult
, "Bad pointer");
163 *aResult
= DataToString("%u", static_cast<unsigned int>(mData
));
167 /***************************************************************************/
169 NS_IMPL_ISUPPORTS(nsSupportsPRUint16
, nsISupportsPRUint16
, nsISupportsPrimitive
)
171 nsSupportsPRUint16::nsSupportsPRUint16() : mData(0) {}
174 nsSupportsPRUint16::GetType(uint16_t* aType
) {
175 NS_ASSERTION(aType
, "Bad pointer");
176 *aType
= TYPE_PRUINT16
;
181 nsSupportsPRUint16::GetData(uint16_t* aData
) {
182 NS_ASSERTION(aData
, "Bad pointer");
188 nsSupportsPRUint16::SetData(uint16_t aData
) {
194 nsSupportsPRUint16::ToString(char** aResult
) {
195 NS_ASSERTION(aResult
, "Bad pointer");
196 *aResult
= DataToString("%u", static_cast<unsigned int>(mData
));
200 /***************************************************************************/
202 NS_IMPL_ISUPPORTS(nsSupportsPRUint32
, nsISupportsPRUint32
, nsISupportsPrimitive
)
204 nsSupportsPRUint32::nsSupportsPRUint32() : mData(0) {}
207 nsSupportsPRUint32::GetType(uint16_t* aType
) {
208 NS_ASSERTION(aType
, "Bad pointer");
209 *aType
= TYPE_PRUINT32
;
214 nsSupportsPRUint32::GetData(uint32_t* aData
) {
215 NS_ASSERTION(aData
, "Bad pointer");
221 nsSupportsPRUint32::SetData(uint32_t aData
) {
227 nsSupportsPRUint32::ToString(char** aResult
) {
228 NS_ASSERTION(aResult
, "Bad pointer");
229 *aResult
= DataToString("%u", mData
);
233 /***************************************************************************/
235 NS_IMPL_ISUPPORTS(nsSupportsPRUint64
, nsISupportsPRUint64
, nsISupportsPrimitive
)
237 nsSupportsPRUint64::nsSupportsPRUint64() : mData(0) {}
240 nsSupportsPRUint64::GetType(uint16_t* aType
) {
241 NS_ASSERTION(aType
, "Bad pointer");
242 *aType
= TYPE_PRUINT64
;
247 nsSupportsPRUint64::GetData(uint64_t* aData
) {
248 NS_ASSERTION(aData
, "Bad pointer");
254 nsSupportsPRUint64::SetData(uint64_t aData
) {
260 nsSupportsPRUint64::ToString(char** aResult
) {
261 NS_ASSERTION(aResult
, "Bad pointer");
262 *aResult
= DataToString("%llu", mData
);
266 /***************************************************************************/
268 NS_IMPL_ISUPPORTS(nsSupportsPRTime
, nsISupportsPRTime
, nsISupportsPrimitive
)
270 nsSupportsPRTime::nsSupportsPRTime() : mData(0) {}
273 nsSupportsPRTime::GetType(uint16_t* aType
) {
274 NS_ASSERTION(aType
, "Bad pointer");
275 *aType
= TYPE_PRTIME
;
280 nsSupportsPRTime::GetData(PRTime
* aData
) {
281 NS_ASSERTION(aData
, "Bad pointer");
287 nsSupportsPRTime::SetData(PRTime aData
) {
293 nsSupportsPRTime::ToString(char** aResult
) {
294 NS_ASSERTION(aResult
, "Bad pointer");
295 *aResult
= DataToString("%" PRIu64
, mData
);
299 /***************************************************************************/
301 NS_IMPL_ISUPPORTS(nsSupportsChar
, nsISupportsChar
, nsISupportsPrimitive
)
303 nsSupportsChar::nsSupportsChar() : mData(0) {}
306 nsSupportsChar::GetType(uint16_t* aType
) {
307 NS_ASSERTION(aType
, "Bad pointer");
313 nsSupportsChar::GetData(char* aData
) {
314 NS_ASSERTION(aData
, "Bad pointer");
320 nsSupportsChar::SetData(char aData
) {
326 nsSupportsChar::ToString(char** aResult
) {
327 NS_ASSERTION(aResult
, "Bad pointer");
328 *aResult
= static_cast<char*>(moz_xmalloc(2 * sizeof(char)));
335 /***************************************************************************/
337 NS_IMPL_ISUPPORTS(nsSupportsPRInt16
, nsISupportsPRInt16
, nsISupportsPrimitive
)
339 nsSupportsPRInt16::nsSupportsPRInt16() : mData(0) {}
342 nsSupportsPRInt16::GetType(uint16_t* aType
) {
343 NS_ASSERTION(aType
, "Bad pointer");
344 *aType
= TYPE_PRINT16
;
349 nsSupportsPRInt16::GetData(int16_t* aData
) {
350 NS_ASSERTION(aData
, "Bad pointer");
356 nsSupportsPRInt16::SetData(int16_t aData
) {
362 nsSupportsPRInt16::ToString(char** aResult
) {
363 NS_ASSERTION(aResult
, "Bad pointer");
364 *aResult
= DataToString("%d", static_cast<int>(mData
));
368 /***************************************************************************/
370 NS_IMPL_ISUPPORTS(nsSupportsPRInt32
, nsISupportsPRInt32
, nsISupportsPrimitive
)
372 nsSupportsPRInt32::nsSupportsPRInt32() : mData(0) {}
375 nsSupportsPRInt32::GetType(uint16_t* aType
) {
376 NS_ASSERTION(aType
, "Bad pointer");
377 *aType
= TYPE_PRINT32
;
382 nsSupportsPRInt32::GetData(int32_t* aData
) {
383 NS_ASSERTION(aData
, "Bad pointer");
389 nsSupportsPRInt32::SetData(int32_t aData
) {
395 nsSupportsPRInt32::ToString(char** aResult
) {
396 NS_ASSERTION(aResult
, "Bad pointer");
397 *aResult
= DataToString("%d", mData
);
401 /***************************************************************************/
403 NS_IMPL_ISUPPORTS(nsSupportsPRInt64
, nsISupportsPRInt64
, nsISupportsPrimitive
)
405 nsSupportsPRInt64::nsSupportsPRInt64() : mData(0) {}
408 nsSupportsPRInt64::GetType(uint16_t* aType
) {
409 NS_ASSERTION(aType
, "Bad pointer");
410 *aType
= TYPE_PRINT64
;
415 nsSupportsPRInt64::GetData(int64_t* aData
) {
416 NS_ASSERTION(aData
, "Bad pointer");
422 nsSupportsPRInt64::SetData(int64_t aData
) {
428 nsSupportsPRInt64::ToString(char** aResult
) {
429 NS_ASSERTION(aResult
, "Bad pointer");
430 *aResult
= DataToString("%" PRId64
, mData
);
434 /***************************************************************************/
436 NS_IMPL_ISUPPORTS(nsSupportsFloat
, nsISupportsFloat
, nsISupportsPrimitive
)
438 nsSupportsFloat::nsSupportsFloat() : mData(float(0.0)) {}
441 nsSupportsFloat::GetType(uint16_t* aType
) {
442 NS_ASSERTION(aType
, "Bad pointer");
448 nsSupportsFloat::GetData(float* aData
) {
449 NS_ASSERTION(aData
, "Bad pointer");
455 nsSupportsFloat::SetData(float aData
) {
461 nsSupportsFloat::ToString(char** aResult
) {
462 NS_ASSERTION(aResult
, "Bad pointer");
463 *aResult
= DataToString("%f", static_cast<double>(mData
));
467 /***************************************************************************/
469 NS_IMPL_ISUPPORTS(nsSupportsDouble
, nsISupportsDouble
, nsISupportsPrimitive
)
471 nsSupportsDouble::nsSupportsDouble() : mData(double(0.0)) {}
474 nsSupportsDouble::GetType(uint16_t* aType
) {
475 NS_ASSERTION(aType
, "Bad pointer");
476 *aType
= TYPE_DOUBLE
;
481 nsSupportsDouble::GetData(double* aData
) {
482 NS_ASSERTION(aData
, "Bad pointer");
488 nsSupportsDouble::SetData(double aData
) {
494 nsSupportsDouble::ToString(char** aResult
) {
495 NS_ASSERTION(aResult
, "Bad pointer");
496 *aResult
= DataToString("%f", mData
);
500 /***************************************************************************/
502 NS_IMPL_ISUPPORTS(nsSupportsInterfacePointer
, nsISupportsInterfacePointer
,
503 nsISupportsPrimitive
)
505 nsSupportsInterfacePointer::nsSupportsInterfacePointer() : mIID(nullptr) {}
507 nsSupportsInterfacePointer::~nsSupportsInterfacePointer() {
514 nsSupportsInterfacePointer::GetType(uint16_t* aType
) {
515 NS_ASSERTION(aType
, "Bad pointer");
516 *aType
= TYPE_INTERFACE_POINTER
;
521 nsSupportsInterfacePointer::GetData(nsISupports
** aData
) {
522 NS_ASSERTION(aData
, "Bad pointer");
524 NS_IF_ADDREF(*aData
);
529 nsSupportsInterfacePointer::SetData(nsISupports
* aData
) {
535 nsSupportsInterfacePointer::GetDataIID(nsID
** aIID
) {
536 NS_ASSERTION(aIID
, "Bad pointer");
538 *aIID
= mIID
? mIID
->Clone() : nullptr;
543 nsSupportsInterfacePointer::SetDataIID(const nsID
* aIID
) {
548 mIID
= aIID
? aIID
->Clone() : nullptr;
553 nsSupportsInterfacePointer::ToString(char** aResult
) {
554 NS_ASSERTION(aResult
, "Bad pointer");
556 // jband sez: think about asking nsIInterfaceInfoManager whether
557 // the interface has a known human-readable name
558 *aResult
= moz_xstrdup("[interface pointer]");
562 /***************************************************************************/
564 NS_IMPL_ISUPPORTS(nsSupportsDependentCString
, nsISupportsCString
,
565 nsISupportsPrimitive
)
567 nsSupportsDependentCString::nsSupportsDependentCString(const char* aStr
)
571 nsSupportsDependentCString::GetType(uint16_t* aType
) {
572 if (NS_WARN_IF(!aType
)) {
573 return NS_ERROR_INVALID_ARG
;
576 *aType
= TYPE_CSTRING
;
581 nsSupportsDependentCString::GetData(nsACString
& aData
) {
587 nsSupportsDependentCString::ToString(char** aResult
) {
588 if (NS_WARN_IF(!aResult
)) {
589 return NS_ERROR_INVALID_ARG
;
592 *aResult
= ToNewCString(mData
, mozilla::fallible
);
594 return NS_ERROR_OUT_OF_MEMORY
;
601 nsSupportsDependentCString::SetData(const nsACString
& aData
) {
602 return NS_ERROR_NOT_IMPLEMENTED
;