michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * michael@0: * A sample of XPConnect. This file contains an implementation nsSample michael@0: * of the interface nsISample. michael@0: * michael@0: */ michael@0: #include michael@0: michael@0: #include "nsSample.h" michael@0: #include "nsMemory.h" michael@0: michael@0: #include "nsEmbedString.h" michael@0: #include "nsIClassInfoImpl.h" michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: nsSampleImpl::nsSampleImpl() : mValue(nullptr) michael@0: { michael@0: mValue = (char*)nsMemory::Clone("initial value", 14); michael@0: } michael@0: michael@0: nsSampleImpl::~nsSampleImpl() michael@0: { michael@0: if (mValue) michael@0: nsMemory::Free(mValue); michael@0: } michael@0: michael@0: /** michael@0: * NS_IMPL_ISUPPORTS expands to a simple implementation of the nsISupports michael@0: * interface. This includes a proper implementation of AddRef, Release, michael@0: * and QueryInterface. If this class supported more interfaces than just michael@0: * nsISupports, michael@0: * you could use NS_IMPL_ADDREF() and NS_IMPL_RELEASE() to take care of the michael@0: * simple stuff, but you would have to create QueryInterface on your own. michael@0: * nsSampleFactory.cpp is an example of this approach. michael@0: * Notice that the second parameter to the macro is name of the interface, and michael@0: * NOT the #defined IID. michael@0: * michael@0: * The _CI variant adds support for nsIClassInfo, which permits introspection michael@0: * and interface flattening. michael@0: */ michael@0: NS_IMPL_CLASSINFO(nsSampleImpl, nullptr, 0, NS_SAMPLE_CID) michael@0: NS_IMPL_ISUPPORTS_CI(nsSampleImpl, nsISample) michael@0: /** michael@0: * Notice that in the protoype for this function, the NS_IMETHOD macro was michael@0: * used to declare the return type. For the implementation, the return michael@0: * type is declared by NS_IMETHODIMP michael@0: */ michael@0: NS_IMETHODIMP michael@0: nsSampleImpl::GetValue(char** aValue) michael@0: { michael@0: NS_PRECONDITION(aValue != nullptr, "null ptr"); michael@0: if (! aValue) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: if (mValue) { michael@0: /** michael@0: * GetValue's job is to return data known by an instance of michael@0: * nsSampleImpl to the outside world. If we were to simply return michael@0: * a pointer to data owned by this instance, and the client were to michael@0: * free it, bad things would surely follow. michael@0: * On the other hand, if we create a new copy of the data for our michael@0: * client, and it turns out that client is implemented in JavaScript, michael@0: * there would be no way to free the buffer. The solution to the michael@0: * buffer ownership problem is the nsMemory singleton. Any buffer michael@0: * returned by an XPCOM method should be allocated by the nsMemory. michael@0: * This convention lets things like JavaScript reflection do their michael@0: * job, and simplifies the way C++ clients deal with returned buffers. michael@0: */ michael@0: *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1); michael@0: if (! *aValue) michael@0: return NS_ERROR_NULL_POINTER; michael@0: } michael@0: else { michael@0: *aValue = nullptr; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSampleImpl::SetValue(const char* aValue) michael@0: { michael@0: NS_PRECONDITION(aValue != nullptr, "null ptr"); michael@0: if (! aValue) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: if (mValue) { michael@0: nsMemory::Free(mValue); michael@0: } michael@0: michael@0: /** michael@0: * Another buffer passing convention is that buffers passed INTO your michael@0: * object ARE NOT YOURS. Keep your hands off them, unless they are michael@0: * declared "inout". If you want to keep the value for posterity, michael@0: * you will have to make a copy of it. michael@0: */ michael@0: mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSampleImpl::Poke(const char* aValue) michael@0: { michael@0: return SetValue((char*) aValue); michael@0: } michael@0: michael@0: michael@0: static void GetStringValue(nsACString& aValue) michael@0: { michael@0: NS_CStringSetData(aValue, "GetValue"); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSampleImpl::WriteValue(const char* aPrefix) michael@0: { michael@0: NS_PRECONDITION(aPrefix != nullptr, "null ptr"); michael@0: if (! aPrefix) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: printf("%s %s\n", aPrefix, mValue); michael@0: michael@0: // This next part illustrates the nsEmbedString: michael@0: nsEmbedString foopy; michael@0: foopy.Append(char16_t('f')); michael@0: foopy.Append(char16_t('o')); michael@0: foopy.Append(char16_t('o')); michael@0: foopy.Append(char16_t('p')); michael@0: foopy.Append(char16_t('y')); michael@0: michael@0: const char16_t* f = foopy.get(); michael@0: uint32_t l = foopy.Length(); michael@0: printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l); michael@0: michael@0: nsEmbedCString foopy2; michael@0: GetStringValue(foopy2); michael@0: michael@0: //foopy2.AppendLiteral("foopy"); michael@0: const char* f2 = foopy2.get(); michael@0: uint32_t l2 = foopy2.Length(); michael@0: michael@0: printf("%s %d\n", f2, l2); michael@0: michael@0: return NS_OK; michael@0: }