1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Dan Rosen <dr@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsSupportsPrimitives.h"
43 #include "nsIInterfaceInfoManager.h"
44 #include "nsDependentString.h"
45 #include "nsReadableUtils.h"
46 #include "nsPromiseFlatString.h"
48 /***************************************************************************/
50 NS_IMPL_ISUPPORTS2(nsSupportsIDImpl
, nsISupportsID
, nsISupportsPrimitive
)
52 nsSupportsIDImpl::nsSupportsIDImpl()
57 NS_IMETHODIMP
nsSupportsIDImpl::GetType(PRUint16
*aType
)
59 NS_ASSERTION(aType
, "Bad pointer");
65 NS_IMETHODIMP
nsSupportsIDImpl::GetData(nsID
**aData
)
67 NS_ASSERTION(aData
, "Bad pointer");
70 *aData
= (nsID
*) nsMemory::Clone(mData
, sizeof(nsID
));
71 return *aData
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
77 NS_IMETHODIMP
nsSupportsIDImpl::SetData(const nsID
*aData
)
80 nsMemory::Free(mData
);
82 mData
= (nsID
*) nsMemory::Clone(aData
, sizeof(nsID
));
88 NS_IMETHODIMP
nsSupportsIDImpl::ToString(char **_retval
)
91 NS_ASSERTION(_retval
, "Bad pointer");
94 result
= mData
->ToString();
98 static const char nullStr
[] = "null";
99 result
= (char*) nsMemory::Clone(nullStr
, sizeof(nullStr
));
103 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
106 /*****************************************************************************
107 * nsSupportsCStringImpl
108 *****************************************************************************/
110 NS_IMPL_ISUPPORTS2(nsSupportsCStringImpl
, nsISupportsCString
,
111 nsISupportsPrimitive
)
113 NS_IMETHODIMP
nsSupportsCStringImpl::GetType(PRUint16
*aType
)
115 NS_ASSERTION(aType
, "Bad pointer");
117 *aType
= TYPE_CSTRING
;
121 NS_IMETHODIMP
nsSupportsCStringImpl::GetData(nsACString
& aData
)
127 NS_IMETHODIMP
nsSupportsCStringImpl::ToString(char **_retval
)
129 *_retval
= ToNewCString(mData
);
132 return NS_ERROR_OUT_OF_MEMORY
;
137 NS_IMETHODIMP
nsSupportsCStringImpl::SetData(const nsACString
& aData
)
143 /*****************************************************************************
144 * nsSupportsStringImpl
145 *****************************************************************************/
147 NS_IMPL_ISUPPORTS2(nsSupportsStringImpl
, nsISupportsString
,
148 nsISupportsPrimitive
)
150 NS_IMETHODIMP
nsSupportsStringImpl::GetType(PRUint16
*aType
)
152 NS_ASSERTION(aType
, "Bad pointer");
154 *aType
= TYPE_STRING
;
158 NS_IMETHODIMP
nsSupportsStringImpl::GetData(nsAString
& aData
)
164 NS_IMETHODIMP
nsSupportsStringImpl::ToString(PRUnichar
**_retval
)
166 *_retval
= ToNewUnicode(mData
);
169 return NS_ERROR_OUT_OF_MEMORY
;
174 NS_IMETHODIMP
nsSupportsStringImpl::SetData(const nsAString
& aData
)
180 /***************************************************************************/
182 NS_IMPL_THREADSAFE_ISUPPORTS2(nsSupportsPRBoolImpl
, nsISupportsPRBool
,
183 nsISupportsPrimitive
)
185 nsSupportsPRBoolImpl::nsSupportsPRBoolImpl()
190 NS_IMETHODIMP
nsSupportsPRBoolImpl::GetType(PRUint16
*aType
)
192 NS_ASSERTION(aType
, "Bad pointer");
193 *aType
= TYPE_PRBOOL
;
198 NS_IMETHODIMP
nsSupportsPRBoolImpl::GetData(PRBool
*aData
)
200 NS_ASSERTION(aData
, "Bad pointer");
205 NS_IMETHODIMP
nsSupportsPRBoolImpl::SetData(PRBool aData
)
211 NS_IMETHODIMP
nsSupportsPRBoolImpl::ToString(char **_retval
)
213 NS_ASSERTION(_retval
, "Bad pointer");
214 const char * str
= mData
? "true" : "false";
215 char* result
= (char*) nsMemory::Clone(str
,
216 (strlen(str
)+1)*sizeof(char));
218 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
221 /***************************************************************************/
223 NS_IMPL_ISUPPORTS2(nsSupportsPRUint8Impl
, nsISupportsPRUint8
,
224 nsISupportsPrimitive
)
226 nsSupportsPRUint8Impl::nsSupportsPRUint8Impl()
231 NS_IMETHODIMP
nsSupportsPRUint8Impl::GetType(PRUint16
*aType
)
233 NS_ASSERTION(aType
, "Bad pointer");
234 *aType
= TYPE_PRUINT8
;
239 NS_IMETHODIMP
nsSupportsPRUint8Impl::GetData(PRUint8
*aData
)
241 NS_ASSERTION(aData
, "Bad pointer");
246 NS_IMETHODIMP
nsSupportsPRUint8Impl::SetData(PRUint8 aData
)
252 NS_IMETHODIMP
nsSupportsPRUint8Impl::ToString(char **_retval
)
254 NS_ASSERTION(_retval
, "Bad pointer");
255 static const int size
= 8;
258 PR_snprintf(buf
, size
, "%u", (PRUint16
) mData
);
260 char* result
= (char*) nsMemory::Clone(buf
,
261 (strlen(buf
)+1)*sizeof(char));
263 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
266 /***************************************************************************/
268 NS_IMPL_ISUPPORTS2(nsSupportsPRUint16Impl
, nsISupportsPRUint16
,
269 nsISupportsPrimitive
)
271 nsSupportsPRUint16Impl::nsSupportsPRUint16Impl()
276 NS_IMETHODIMP
nsSupportsPRUint16Impl::GetType(PRUint16
*aType
)
278 NS_ASSERTION(aType
, "Bad pointer");
279 *aType
= TYPE_PRUINT16
;
284 NS_IMETHODIMP
nsSupportsPRUint16Impl::GetData(PRUint16
*aData
)
286 NS_ASSERTION(aData
, "Bad pointer");
291 NS_IMETHODIMP
nsSupportsPRUint16Impl::SetData(PRUint16 aData
)
297 NS_IMETHODIMP
nsSupportsPRUint16Impl::ToString(char **_retval
)
299 NS_ASSERTION(_retval
, "Bad pointer");
300 static const int size
= 8;
303 PR_snprintf(buf
, size
, "%u", (int) mData
);
305 char* result
= (char*) nsMemory::Clone(buf
,
306 (strlen(buf
)+1)*sizeof(char));
308 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
311 /***************************************************************************/
313 NS_IMPL_ISUPPORTS2(nsSupportsPRUint32Impl
, nsISupportsPRUint32
,
314 nsISupportsPrimitive
)
316 nsSupportsPRUint32Impl::nsSupportsPRUint32Impl()
321 NS_IMETHODIMP
nsSupportsPRUint32Impl::GetType(PRUint16
*aType
)
323 NS_ASSERTION(aType
, "Bad pointer");
324 *aType
= TYPE_PRUINT32
;
329 NS_IMETHODIMP
nsSupportsPRUint32Impl::GetData(PRUint32
*aData
)
331 NS_ASSERTION(aData
, "Bad pointer");
336 NS_IMETHODIMP
nsSupportsPRUint32Impl::SetData(PRUint32 aData
)
342 NS_IMETHODIMP
nsSupportsPRUint32Impl::ToString(char **_retval
)
344 NS_ASSERTION(_retval
, "Bad pointer");
345 static const int size
= 16;
348 PR_snprintf(buf
, size
, "%lu", mData
);
350 char* result
= (char*) nsMemory::Clone(buf
,
351 (strlen(buf
)+1)*sizeof(char));
353 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
356 /***************************************************************************/
358 NS_IMPL_ISUPPORTS2(nsSupportsPRUint64Impl
, nsISupportsPRUint64
,
359 nsISupportsPrimitive
)
361 nsSupportsPRUint64Impl::nsSupportsPRUint64Impl()
366 NS_IMETHODIMP
nsSupportsPRUint64Impl::GetType(PRUint16
*aType
)
368 NS_ASSERTION(aType
, "Bad pointer");
369 *aType
= TYPE_PRUINT64
;
374 NS_IMETHODIMP
nsSupportsPRUint64Impl::GetData(PRUint64
*aData
)
376 NS_ASSERTION(aData
, "Bad pointer");
381 NS_IMETHODIMP
nsSupportsPRUint64Impl::SetData(PRUint64 aData
)
387 NS_IMETHODIMP
nsSupportsPRUint64Impl::ToString(char **_retval
)
389 NS_ASSERTION(_retval
, "Bad pointer");
390 static const int size
= 32;
393 PR_snprintf(buf
, size
, "%llu", mData
);
395 char* result
= (char*) nsMemory::Clone(buf
,
396 (strlen(buf
)+1)*sizeof(char));
398 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
401 /***************************************************************************/
403 NS_IMPL_ISUPPORTS2(nsSupportsPRTimeImpl
, nsISupportsPRTime
,
404 nsISupportsPrimitive
)
406 nsSupportsPRTimeImpl::nsSupportsPRTimeImpl()
411 NS_IMETHODIMP
nsSupportsPRTimeImpl::GetType(PRUint16
*aType
)
413 NS_ASSERTION(aType
, "Bad pointer");
414 *aType
= TYPE_PRTIME
;
419 NS_IMETHODIMP
nsSupportsPRTimeImpl::GetData(PRTime
*aData
)
421 NS_ASSERTION(aData
, "Bad pointer");
426 NS_IMETHODIMP
nsSupportsPRTimeImpl::SetData(PRTime aData
)
432 NS_IMETHODIMP
nsSupportsPRTimeImpl::ToString(char **_retval
)
434 NS_ASSERTION(_retval
, "Bad pointer");
435 static const int size
= 32;
438 PR_snprintf(buf
, size
, "%llu", mData
);
440 char* result
= (char*) nsMemory::Clone(buf
,
441 (strlen(buf
)+1)*sizeof(char));
443 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
446 /***************************************************************************/
448 NS_IMPL_ISUPPORTS2(nsSupportsCharImpl
, nsISupportsChar
,
449 nsISupportsPrimitive
)
451 nsSupportsCharImpl::nsSupportsCharImpl()
456 NS_IMETHODIMP
nsSupportsCharImpl::GetType(PRUint16
*aType
)
458 NS_ASSERTION(aType
, "Bad pointer");
464 NS_IMETHODIMP
nsSupportsCharImpl::GetData(char *aData
)
466 NS_ASSERTION(aData
, "Bad pointer");
471 NS_IMETHODIMP
nsSupportsCharImpl::SetData(char aData
)
477 NS_IMETHODIMP
nsSupportsCharImpl::ToString(char **_retval
)
480 NS_ASSERTION(_retval
, "Bad pointer");
482 if(nsnull
!= (result
= (char*) nsMemory::Alloc(2*sizeof(char))))
488 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
491 /***************************************************************************/
493 NS_IMPL_ISUPPORTS2(nsSupportsPRInt16Impl
, nsISupportsPRInt16
,
494 nsISupportsPrimitive
)
496 nsSupportsPRInt16Impl::nsSupportsPRInt16Impl()
501 NS_IMETHODIMP
nsSupportsPRInt16Impl::GetType(PRUint16
*aType
)
503 NS_ASSERTION(aType
, "Bad pointer");
504 *aType
= TYPE_PRINT16
;
509 NS_IMETHODIMP
nsSupportsPRInt16Impl::GetData(PRInt16
*aData
)
511 NS_ASSERTION(aData
, "Bad pointer");
516 NS_IMETHODIMP
nsSupportsPRInt16Impl::SetData(PRInt16 aData
)
522 NS_IMETHODIMP
nsSupportsPRInt16Impl::ToString(char **_retval
)
524 NS_ASSERTION(_retval
, "Bad pointer");
525 static const int size
= 8;
528 PR_snprintf(buf
, size
, "%d", mData
);
530 char* result
= (char*) nsMemory::Clone(buf
,
531 (strlen(buf
)+1)*sizeof(char));
533 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
536 /***************************************************************************/
538 NS_IMPL_ISUPPORTS2(nsSupportsPRInt32Impl
, nsISupportsPRInt32
,
539 nsISupportsPrimitive
)
541 nsSupportsPRInt32Impl::nsSupportsPRInt32Impl()
546 NS_IMETHODIMP
nsSupportsPRInt32Impl::GetType(PRUint16
*aType
)
548 NS_ASSERTION(aType
, "Bad pointer");
549 *aType
= TYPE_PRINT32
;
554 NS_IMETHODIMP
nsSupportsPRInt32Impl::GetData(PRInt32
*aData
)
556 NS_ASSERTION(aData
, "Bad pointer");
561 NS_IMETHODIMP
nsSupportsPRInt32Impl::SetData(PRInt32 aData
)
567 NS_IMETHODIMP
nsSupportsPRInt32Impl::ToString(char **_retval
)
569 NS_ASSERTION(_retval
, "Bad pointer");
570 static const int size
= 16;
573 PR_snprintf(buf
, size
, "%ld", mData
);
575 char* result
= (char*) nsMemory::Clone(buf
,
576 (strlen(buf
)+1)*sizeof(char));
578 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
581 /***************************************************************************/
583 NS_IMPL_ISUPPORTS2(nsSupportsPRInt64Impl
, nsISupportsPRInt64
,
584 nsISupportsPrimitive
)
586 nsSupportsPRInt64Impl::nsSupportsPRInt64Impl()
591 NS_IMETHODIMP
nsSupportsPRInt64Impl::GetType(PRUint16
*aType
)
593 NS_ASSERTION(aType
, "Bad pointer");
594 *aType
= TYPE_PRINT64
;
599 NS_IMETHODIMP
nsSupportsPRInt64Impl::GetData(PRInt64
*aData
)
601 NS_ASSERTION(aData
, "Bad pointer");
606 NS_IMETHODIMP
nsSupportsPRInt64Impl::SetData(PRInt64 aData
)
612 NS_IMETHODIMP
nsSupportsPRInt64Impl::ToString(char **_retval
)
614 NS_ASSERTION(_retval
, "Bad pointer");
615 static const int size
= 32;
618 PR_snprintf(buf
, size
, "%lld", mData
);
620 char* result
= (char*) nsMemory::Clone(buf
,
621 (strlen(buf
)+1)*sizeof(char));
623 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
626 /***************************************************************************/
628 NS_IMPL_ISUPPORTS2(nsSupportsFloatImpl
, nsISupportsFloat
,
629 nsISupportsPrimitive
)
631 nsSupportsFloatImpl::nsSupportsFloatImpl()
636 NS_IMETHODIMP
nsSupportsFloatImpl::GetType(PRUint16
*aType
)
638 NS_ASSERTION(aType
, "Bad pointer");
644 NS_IMETHODIMP
nsSupportsFloatImpl::GetData(float *aData
)
646 NS_ASSERTION(aData
, "Bad pointer");
651 NS_IMETHODIMP
nsSupportsFloatImpl::SetData(float aData
)
657 NS_IMETHODIMP
nsSupportsFloatImpl::ToString(char **_retval
)
659 NS_ASSERTION(_retval
, "Bad pointer");
660 static const int size
= 32;
663 PR_snprintf(buf
, size
, "%f", (double) mData
);
665 char* result
= (char*) nsMemory::Clone(buf
,
666 (strlen(buf
)+1)*sizeof(char));
668 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
671 /***************************************************************************/
673 NS_IMPL_ISUPPORTS2(nsSupportsDoubleImpl
, nsISupportsDouble
,
674 nsISupportsPrimitive
)
676 nsSupportsDoubleImpl::nsSupportsDoubleImpl()
681 NS_IMETHODIMP
nsSupportsDoubleImpl::GetType(PRUint16
*aType
)
683 NS_ASSERTION(aType
, "Bad pointer");
684 *aType
= TYPE_DOUBLE
;
689 NS_IMETHODIMP
nsSupportsDoubleImpl::GetData(double *aData
)
691 NS_ASSERTION(aData
, "Bad pointer");
696 NS_IMETHODIMP
nsSupportsDoubleImpl::SetData(double aData
)
702 NS_IMETHODIMP
nsSupportsDoubleImpl::ToString(char **_retval
)
704 NS_ASSERTION(_retval
, "Bad pointer");
705 static const int size
= 32;
708 PR_snprintf(buf
, size
, "%f", mData
);
710 char* result
= (char*) nsMemory::Clone(buf
,
711 (strlen(buf
)+1)*sizeof(char));
713 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
716 /***************************************************************************/
719 NS_IMPL_THREADSAFE_ISUPPORTS2(nsSupportsVoidImpl
, nsISupportsVoid
,
720 nsISupportsPrimitive
)
722 nsSupportsVoidImpl::nsSupportsVoidImpl()
727 NS_IMETHODIMP
nsSupportsVoidImpl::GetType(PRUint16
*aType
)
729 NS_ASSERTION(aType
, "Bad pointer");
735 NS_IMETHODIMP
nsSupportsVoidImpl::GetData(void * *aData
)
737 NS_ASSERTION(aData
, "Bad pointer");
742 NS_IMETHODIMP
nsSupportsVoidImpl::SetData(void * aData
)
748 NS_IMETHODIMP
nsSupportsVoidImpl::ToString(char **_retval
)
750 NS_ASSERTION(_retval
, "Bad pointer");
752 static const char str
[] = "[raw data]";
753 char* result
= (char*) nsMemory::Clone(str
, sizeof(str
));
755 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
758 /***************************************************************************/
761 NS_IMPL_THREADSAFE_ISUPPORTS2(nsSupportsInterfacePointerImpl
,
762 nsISupportsInterfacePointer
,
763 nsISupportsPrimitive
)
765 nsSupportsInterfacePointerImpl::nsSupportsInterfacePointerImpl()
770 nsSupportsInterfacePointerImpl::~nsSupportsInterfacePointerImpl()
773 nsMemory::Free(mIID
);
777 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::GetType(PRUint16
*aType
)
779 NS_ASSERTION(aType
, "Bad pointer");
780 *aType
= TYPE_INTERFACE_POINTER
;
785 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::GetData(nsISupports
**aData
)
787 NS_ASSERTION(aData
,"Bad pointer");
790 NS_IF_ADDREF(*aData
);
795 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::SetData(nsISupports
* aData
)
802 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::GetDataIID(nsID
**aIID
)
804 NS_ASSERTION(aIID
,"Bad pointer");
808 *aIID
= (nsID
*) nsMemory::Clone(mIID
, sizeof(nsID
));
809 return *aIID
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
815 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::SetDataIID(const nsID
*aIID
)
818 nsMemory::Free(mIID
);
820 mIID
= (nsID
*) nsMemory::Clone(aIID
, sizeof(nsID
));
827 NS_IMETHODIMP
nsSupportsInterfacePointerImpl::ToString(char **_retval
)
829 NS_ASSERTION(_retval
, "Bad pointer");
831 static const char str
[] = "[interface pointer]";
833 // jband sez: think about asking nsIInterfaceInfoManager whether
834 // the interface has a known human-readable name
835 char* result
= (char*) nsMemory::Clone(str
, sizeof(str
));
837 return result
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
840 /***************************************************************************/
842 NS_IMPL_ISUPPORTS2(nsSupportsDependentCString
,nsISupportsCString
,nsISupportsPrimitive
)
844 nsSupportsDependentCString::nsSupportsDependentCString(const char* aStr
)
849 nsSupportsDependentCString::GetType(PRUint16
*aType
)
851 NS_ENSURE_ARG_POINTER(aType
);
853 *aType
= TYPE_CSTRING
;
858 nsSupportsDependentCString::GetData(nsACString
& aData
)
865 nsSupportsDependentCString::ToString(char **_retval
)
867 NS_ENSURE_ARG_POINTER(_retval
);
869 *_retval
= ToNewCString(mData
);
871 return NS_ERROR_OUT_OF_MEMORY
;
877 nsSupportsDependentCString::SetData(const nsACString
& aData
)
879 return NS_ERROR_NOT_IMPLEMENTED
;