no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / ds / nsSupportsPrimitives.cpp
blob1d0db73187ce5723a0300513d26499077b400bd3
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"
11 #include <algorithm>
13 template <typename T>
14 static char* DataToString(const char* aFormat, T aData) {
15 static const int size = 32;
16 char buf[size];
18 SprintfLiteral(buf, aFormat, aData);
20 return moz_xstrdup(buf);
23 /*****************************************************************************
24 * nsSupportsCString
25 *****************************************************************************/
27 NS_IMPL_ISUPPORTS(nsSupportsCString, nsISupportsCString, nsISupportsPrimitive)
29 NS_IMETHODIMP
30 nsSupportsCString::GetType(uint16_t* aType) {
31 NS_ASSERTION(aType, "Bad pointer");
32 *aType = TYPE_CSTRING;
33 return NS_OK;
36 NS_IMETHODIMP
37 nsSupportsCString::GetData(nsACString& aData) {
38 aData = mData;
39 return NS_OK;
42 NS_IMETHODIMP
43 nsSupportsCString::ToString(char** aResult) {
44 *aResult = ToNewCString(mData, mozilla::fallible);
45 if (!*aResult) {
46 return NS_ERROR_OUT_OF_MEMORY;
49 return NS_OK;
52 NS_IMETHODIMP
53 nsSupportsCString::SetData(const nsACString& aData) {
54 bool ok = mData.Assign(aData, mozilla::fallible);
55 if (!ok) {
56 return NS_ERROR_OUT_OF_MEMORY;
59 return NS_OK;
62 /*****************************************************************************
63 * nsSupportsString
64 *****************************************************************************/
66 NS_IMPL_ISUPPORTS(nsSupportsString, nsISupportsString, nsISupportsPrimitive)
68 NS_IMETHODIMP
69 nsSupportsString::GetType(uint16_t* aType) {
70 NS_ASSERTION(aType, "Bad pointer");
71 *aType = TYPE_STRING;
72 return NS_OK;
75 NS_IMETHODIMP
76 nsSupportsString::GetData(nsAString& aData) {
77 aData = mData;
78 return NS_OK;
81 NS_IMETHODIMP
82 nsSupportsString::ToString(char16_t** aResult) {
83 *aResult = ToNewUnicode(mData, mozilla::fallible);
84 if (!*aResult) {
85 return NS_ERROR_OUT_OF_MEMORY;
88 return NS_OK;
91 NS_IMETHODIMP
92 nsSupportsString::SetData(const nsAString& aData) {
93 bool ok = mData.Assign(aData, mozilla::fallible);
94 if (!ok) {
95 return NS_ERROR_OUT_OF_MEMORY;
98 return NS_OK;
101 /***************************************************************************/
103 NS_IMPL_ISUPPORTS(nsSupportsPRBool, nsISupportsPRBool, nsISupportsPrimitive)
105 nsSupportsPRBool::nsSupportsPRBool() : mData(false) {}
107 NS_IMETHODIMP
108 nsSupportsPRBool::GetType(uint16_t* aType) {
109 NS_ASSERTION(aType, "Bad pointer");
110 *aType = TYPE_PRBOOL;
111 return NS_OK;
114 NS_IMETHODIMP
115 nsSupportsPRBool::GetData(bool* aData) {
116 NS_ASSERTION(aData, "Bad pointer");
117 *aData = mData;
118 return NS_OK;
121 NS_IMETHODIMP
122 nsSupportsPRBool::SetData(bool aData) {
123 mData = aData;
124 return NS_OK;
127 NS_IMETHODIMP
128 nsSupportsPRBool::ToString(char** aResult) {
129 NS_ASSERTION(aResult, "Bad pointer");
130 *aResult = moz_xstrdup(mData ? "true" : "false");
131 return NS_OK;
134 /***************************************************************************/
136 NS_IMPL_ISUPPORTS(nsSupportsPRUint8, nsISupportsPRUint8, nsISupportsPrimitive)
138 nsSupportsPRUint8::nsSupportsPRUint8() : mData(0) {}
140 NS_IMETHODIMP
141 nsSupportsPRUint8::GetType(uint16_t* aType) {
142 NS_ASSERTION(aType, "Bad pointer");
143 *aType = TYPE_PRUINT8;
144 return NS_OK;
147 NS_IMETHODIMP
148 nsSupportsPRUint8::GetData(uint8_t* aData) {
149 NS_ASSERTION(aData, "Bad pointer");
150 *aData = mData;
151 return NS_OK;
154 NS_IMETHODIMP
155 nsSupportsPRUint8::SetData(uint8_t aData) {
156 mData = aData;
157 return NS_OK;
160 NS_IMETHODIMP
161 nsSupportsPRUint8::ToString(char** aResult) {
162 NS_ASSERTION(aResult, "Bad pointer");
163 *aResult = DataToString("%u", static_cast<unsigned int>(mData));
164 return NS_OK;
167 /***************************************************************************/
169 NS_IMPL_ISUPPORTS(nsSupportsPRUint16, nsISupportsPRUint16, nsISupportsPrimitive)
171 nsSupportsPRUint16::nsSupportsPRUint16() : mData(0) {}
173 NS_IMETHODIMP
174 nsSupportsPRUint16::GetType(uint16_t* aType) {
175 NS_ASSERTION(aType, "Bad pointer");
176 *aType = TYPE_PRUINT16;
177 return NS_OK;
180 NS_IMETHODIMP
181 nsSupportsPRUint16::GetData(uint16_t* aData) {
182 NS_ASSERTION(aData, "Bad pointer");
183 *aData = mData;
184 return NS_OK;
187 NS_IMETHODIMP
188 nsSupportsPRUint16::SetData(uint16_t aData) {
189 mData = aData;
190 return NS_OK;
193 NS_IMETHODIMP
194 nsSupportsPRUint16::ToString(char** aResult) {
195 NS_ASSERTION(aResult, "Bad pointer");
196 *aResult = DataToString("%u", static_cast<unsigned int>(mData));
197 return NS_OK;
200 /***************************************************************************/
202 NS_IMPL_ISUPPORTS(nsSupportsPRUint32, nsISupportsPRUint32, nsISupportsPrimitive)
204 nsSupportsPRUint32::nsSupportsPRUint32() : mData(0) {}
206 NS_IMETHODIMP
207 nsSupportsPRUint32::GetType(uint16_t* aType) {
208 NS_ASSERTION(aType, "Bad pointer");
209 *aType = TYPE_PRUINT32;
210 return NS_OK;
213 NS_IMETHODIMP
214 nsSupportsPRUint32::GetData(uint32_t* aData) {
215 NS_ASSERTION(aData, "Bad pointer");
216 *aData = mData;
217 return NS_OK;
220 NS_IMETHODIMP
221 nsSupportsPRUint32::SetData(uint32_t aData) {
222 mData = aData;
223 return NS_OK;
226 NS_IMETHODIMP
227 nsSupportsPRUint32::ToString(char** aResult) {
228 NS_ASSERTION(aResult, "Bad pointer");
229 *aResult = DataToString("%u", mData);
230 return NS_OK;
233 /***************************************************************************/
235 NS_IMPL_ISUPPORTS(nsSupportsPRUint64, nsISupportsPRUint64, nsISupportsPrimitive)
237 nsSupportsPRUint64::nsSupportsPRUint64() : mData(0) {}
239 NS_IMETHODIMP
240 nsSupportsPRUint64::GetType(uint16_t* aType) {
241 NS_ASSERTION(aType, "Bad pointer");
242 *aType = TYPE_PRUINT64;
243 return NS_OK;
246 NS_IMETHODIMP
247 nsSupportsPRUint64::GetData(uint64_t* aData) {
248 NS_ASSERTION(aData, "Bad pointer");
249 *aData = mData;
250 return NS_OK;
253 NS_IMETHODIMP
254 nsSupportsPRUint64::SetData(uint64_t aData) {
255 mData = aData;
256 return NS_OK;
259 NS_IMETHODIMP
260 nsSupportsPRUint64::ToString(char** aResult) {
261 NS_ASSERTION(aResult, "Bad pointer");
262 *aResult = DataToString("%llu", mData);
263 return NS_OK;
266 /***************************************************************************/
268 NS_IMPL_ISUPPORTS(nsSupportsPRTime, nsISupportsPRTime, nsISupportsPrimitive)
270 nsSupportsPRTime::nsSupportsPRTime() : mData(0) {}
272 NS_IMETHODIMP
273 nsSupportsPRTime::GetType(uint16_t* aType) {
274 NS_ASSERTION(aType, "Bad pointer");
275 *aType = TYPE_PRTIME;
276 return NS_OK;
279 NS_IMETHODIMP
280 nsSupportsPRTime::GetData(PRTime* aData) {
281 NS_ASSERTION(aData, "Bad pointer");
282 *aData = mData;
283 return NS_OK;
286 NS_IMETHODIMP
287 nsSupportsPRTime::SetData(PRTime aData) {
288 mData = aData;
289 return NS_OK;
292 NS_IMETHODIMP
293 nsSupportsPRTime::ToString(char** aResult) {
294 NS_ASSERTION(aResult, "Bad pointer");
295 *aResult = DataToString("%" PRIu64, mData);
296 return NS_OK;
299 /***************************************************************************/
301 NS_IMPL_ISUPPORTS(nsSupportsChar, nsISupportsChar, nsISupportsPrimitive)
303 nsSupportsChar::nsSupportsChar() : mData(0) {}
305 NS_IMETHODIMP
306 nsSupportsChar::GetType(uint16_t* aType) {
307 NS_ASSERTION(aType, "Bad pointer");
308 *aType = TYPE_CHAR;
309 return NS_OK;
312 NS_IMETHODIMP
313 nsSupportsChar::GetData(char* aData) {
314 NS_ASSERTION(aData, "Bad pointer");
315 *aData = mData;
316 return NS_OK;
319 NS_IMETHODIMP
320 nsSupportsChar::SetData(char aData) {
321 mData = aData;
322 return NS_OK;
325 NS_IMETHODIMP
326 nsSupportsChar::ToString(char** aResult) {
327 NS_ASSERTION(aResult, "Bad pointer");
328 *aResult = static_cast<char*>(moz_xmalloc(2 * sizeof(char)));
329 *aResult[0] = mData;
330 *aResult[1] = '\0';
332 return NS_OK;
335 /***************************************************************************/
337 NS_IMPL_ISUPPORTS(nsSupportsPRInt16, nsISupportsPRInt16, nsISupportsPrimitive)
339 nsSupportsPRInt16::nsSupportsPRInt16() : mData(0) {}
341 NS_IMETHODIMP
342 nsSupportsPRInt16::GetType(uint16_t* aType) {
343 NS_ASSERTION(aType, "Bad pointer");
344 *aType = TYPE_PRINT16;
345 return NS_OK;
348 NS_IMETHODIMP
349 nsSupportsPRInt16::GetData(int16_t* aData) {
350 NS_ASSERTION(aData, "Bad pointer");
351 *aData = mData;
352 return NS_OK;
355 NS_IMETHODIMP
356 nsSupportsPRInt16::SetData(int16_t aData) {
357 mData = aData;
358 return NS_OK;
361 NS_IMETHODIMP
362 nsSupportsPRInt16::ToString(char** aResult) {
363 NS_ASSERTION(aResult, "Bad pointer");
364 *aResult = DataToString("%d", static_cast<int>(mData));
365 return NS_OK;
368 /***************************************************************************/
370 NS_IMPL_ISUPPORTS(nsSupportsPRInt32, nsISupportsPRInt32, nsISupportsPrimitive)
372 nsSupportsPRInt32::nsSupportsPRInt32() : mData(0) {}
374 NS_IMETHODIMP
375 nsSupportsPRInt32::GetType(uint16_t* aType) {
376 NS_ASSERTION(aType, "Bad pointer");
377 *aType = TYPE_PRINT32;
378 return NS_OK;
381 NS_IMETHODIMP
382 nsSupportsPRInt32::GetData(int32_t* aData) {
383 NS_ASSERTION(aData, "Bad pointer");
384 *aData = mData;
385 return NS_OK;
388 NS_IMETHODIMP
389 nsSupportsPRInt32::SetData(int32_t aData) {
390 mData = aData;
391 return NS_OK;
394 NS_IMETHODIMP
395 nsSupportsPRInt32::ToString(char** aResult) {
396 NS_ASSERTION(aResult, "Bad pointer");
397 *aResult = DataToString("%d", mData);
398 return NS_OK;
401 /***************************************************************************/
403 NS_IMPL_ISUPPORTS(nsSupportsPRInt64, nsISupportsPRInt64, nsISupportsPrimitive)
405 nsSupportsPRInt64::nsSupportsPRInt64() : mData(0) {}
407 NS_IMETHODIMP
408 nsSupportsPRInt64::GetType(uint16_t* aType) {
409 NS_ASSERTION(aType, "Bad pointer");
410 *aType = TYPE_PRINT64;
411 return NS_OK;
414 NS_IMETHODIMP
415 nsSupportsPRInt64::GetData(int64_t* aData) {
416 NS_ASSERTION(aData, "Bad pointer");
417 *aData = mData;
418 return NS_OK;
421 NS_IMETHODIMP
422 nsSupportsPRInt64::SetData(int64_t aData) {
423 mData = aData;
424 return NS_OK;
427 NS_IMETHODIMP
428 nsSupportsPRInt64::ToString(char** aResult) {
429 NS_ASSERTION(aResult, "Bad pointer");
430 *aResult = DataToString("%" PRId64, mData);
431 return NS_OK;
434 /***************************************************************************/
436 NS_IMPL_ISUPPORTS(nsSupportsFloat, nsISupportsFloat, nsISupportsPrimitive)
438 nsSupportsFloat::nsSupportsFloat() : mData(float(0.0)) {}
440 NS_IMETHODIMP
441 nsSupportsFloat::GetType(uint16_t* aType) {
442 NS_ASSERTION(aType, "Bad pointer");
443 *aType = TYPE_FLOAT;
444 return NS_OK;
447 NS_IMETHODIMP
448 nsSupportsFloat::GetData(float* aData) {
449 NS_ASSERTION(aData, "Bad pointer");
450 *aData = mData;
451 return NS_OK;
454 NS_IMETHODIMP
455 nsSupportsFloat::SetData(float aData) {
456 mData = aData;
457 return NS_OK;
460 NS_IMETHODIMP
461 nsSupportsFloat::ToString(char** aResult) {
462 NS_ASSERTION(aResult, "Bad pointer");
463 *aResult = DataToString("%f", static_cast<double>(mData));
464 return NS_OK;
467 /***************************************************************************/
469 NS_IMPL_ISUPPORTS(nsSupportsDouble, nsISupportsDouble, nsISupportsPrimitive)
471 nsSupportsDouble::nsSupportsDouble() : mData(double(0.0)) {}
473 NS_IMETHODIMP
474 nsSupportsDouble::GetType(uint16_t* aType) {
475 NS_ASSERTION(aType, "Bad pointer");
476 *aType = TYPE_DOUBLE;
477 return NS_OK;
480 NS_IMETHODIMP
481 nsSupportsDouble::GetData(double* aData) {
482 NS_ASSERTION(aData, "Bad pointer");
483 *aData = mData;
484 return NS_OK;
487 NS_IMETHODIMP
488 nsSupportsDouble::SetData(double aData) {
489 mData = aData;
490 return NS_OK;
493 NS_IMETHODIMP
494 nsSupportsDouble::ToString(char** aResult) {
495 NS_ASSERTION(aResult, "Bad pointer");
496 *aResult = DataToString("%f", mData);
497 return NS_OK;
500 /***************************************************************************/
502 NS_IMPL_ISUPPORTS(nsSupportsInterfacePointer, nsISupportsInterfacePointer,
503 nsISupportsPrimitive)
505 nsSupportsInterfacePointer::nsSupportsInterfacePointer() : mIID(nullptr) {}
507 nsSupportsInterfacePointer::~nsSupportsInterfacePointer() {
508 if (mIID) {
509 free(mIID);
513 NS_IMETHODIMP
514 nsSupportsInterfacePointer::GetType(uint16_t* aType) {
515 NS_ASSERTION(aType, "Bad pointer");
516 *aType = TYPE_INTERFACE_POINTER;
517 return NS_OK;
520 NS_IMETHODIMP
521 nsSupportsInterfacePointer::GetData(nsISupports** aData) {
522 NS_ASSERTION(aData, "Bad pointer");
523 *aData = mData;
524 NS_IF_ADDREF(*aData);
525 return NS_OK;
528 NS_IMETHODIMP
529 nsSupportsInterfacePointer::SetData(nsISupports* aData) {
530 mData = aData;
531 return NS_OK;
534 NS_IMETHODIMP
535 nsSupportsInterfacePointer::GetDataIID(nsID** aIID) {
536 NS_ASSERTION(aIID, "Bad pointer");
538 *aIID = mIID ? mIID->Clone() : nullptr;
539 return NS_OK;
542 NS_IMETHODIMP
543 nsSupportsInterfacePointer::SetDataIID(const nsID* aIID) {
544 if (mIID) {
545 free(mIID);
548 mIID = aIID ? aIID->Clone() : nullptr;
549 return NS_OK;
552 NS_IMETHODIMP
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]");
559 return NS_OK;
562 /***************************************************************************/
564 NS_IMPL_ISUPPORTS(nsSupportsDependentCString, nsISupportsCString,
565 nsISupportsPrimitive)
567 nsSupportsDependentCString::nsSupportsDependentCString(const char* aStr)
568 : mData(aStr) {}
570 NS_IMETHODIMP
571 nsSupportsDependentCString::GetType(uint16_t* aType) {
572 if (NS_WARN_IF(!aType)) {
573 return NS_ERROR_INVALID_ARG;
576 *aType = TYPE_CSTRING;
577 return NS_OK;
580 NS_IMETHODIMP
581 nsSupportsDependentCString::GetData(nsACString& aData) {
582 aData = mData;
583 return NS_OK;
586 NS_IMETHODIMP
587 nsSupportsDependentCString::ToString(char** aResult) {
588 if (NS_WARN_IF(!aResult)) {
589 return NS_ERROR_INVALID_ARG;
592 *aResult = ToNewCString(mData, mozilla::fallible);
593 if (!*aResult) {
594 return NS_ERROR_OUT_OF_MEMORY;
597 return NS_OK;
600 NS_IMETHODIMP
601 nsSupportsDependentCString::SetData(const nsACString& aData) {
602 return NS_ERROR_NOT_IMPLEMENTED;