1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/sample/nsSample.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + * 1.12 + * A sample of XPConnect. This file contains an implementation nsSample 1.13 + * of the interface nsISample. 1.14 + * 1.15 + */ 1.16 +#include <stdio.h> 1.17 + 1.18 +#include "nsSample.h" 1.19 +#include "nsMemory.h" 1.20 + 1.21 +#include "nsEmbedString.h" 1.22 +#include "nsIClassInfoImpl.h" 1.23 +//////////////////////////////////////////////////////////////////////// 1.24 + 1.25 +nsSampleImpl::nsSampleImpl() : mValue(nullptr) 1.26 +{ 1.27 + mValue = (char*)nsMemory::Clone("initial value", 14); 1.28 +} 1.29 + 1.30 +nsSampleImpl::~nsSampleImpl() 1.31 +{ 1.32 + if (mValue) 1.33 + nsMemory::Free(mValue); 1.34 +} 1.35 + 1.36 +/** 1.37 + * NS_IMPL_ISUPPORTS expands to a simple implementation of the nsISupports 1.38 + * interface. This includes a proper implementation of AddRef, Release, 1.39 + * and QueryInterface. If this class supported more interfaces than just 1.40 + * nsISupports, 1.41 + * you could use NS_IMPL_ADDREF() and NS_IMPL_RELEASE() to take care of the 1.42 + * simple stuff, but you would have to create QueryInterface on your own. 1.43 + * nsSampleFactory.cpp is an example of this approach. 1.44 + * Notice that the second parameter to the macro is name of the interface, and 1.45 + * NOT the #defined IID. 1.46 + * 1.47 + * The _CI variant adds support for nsIClassInfo, which permits introspection 1.48 + * and interface flattening. 1.49 + */ 1.50 +NS_IMPL_CLASSINFO(nsSampleImpl, nullptr, 0, NS_SAMPLE_CID) 1.51 +NS_IMPL_ISUPPORTS_CI(nsSampleImpl, nsISample) 1.52 +/** 1.53 + * Notice that in the protoype for this function, the NS_IMETHOD macro was 1.54 + * used to declare the return type. For the implementation, the return 1.55 + * type is declared by NS_IMETHODIMP 1.56 + */ 1.57 +NS_IMETHODIMP 1.58 +nsSampleImpl::GetValue(char** aValue) 1.59 +{ 1.60 + NS_PRECONDITION(aValue != nullptr, "null ptr"); 1.61 + if (! aValue) 1.62 + return NS_ERROR_NULL_POINTER; 1.63 + 1.64 + if (mValue) { 1.65 + /** 1.66 + * GetValue's job is to return data known by an instance of 1.67 + * nsSampleImpl to the outside world. If we were to simply return 1.68 + * a pointer to data owned by this instance, and the client were to 1.69 + * free it, bad things would surely follow. 1.70 + * On the other hand, if we create a new copy of the data for our 1.71 + * client, and it turns out that client is implemented in JavaScript, 1.72 + * there would be no way to free the buffer. The solution to the 1.73 + * buffer ownership problem is the nsMemory singleton. Any buffer 1.74 + * returned by an XPCOM method should be allocated by the nsMemory. 1.75 + * This convention lets things like JavaScript reflection do their 1.76 + * job, and simplifies the way C++ clients deal with returned buffers. 1.77 + */ 1.78 + *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1); 1.79 + if (! *aValue) 1.80 + return NS_ERROR_NULL_POINTER; 1.81 + } 1.82 + else { 1.83 + *aValue = nullptr; 1.84 + } 1.85 + return NS_OK; 1.86 +} 1.87 + 1.88 +NS_IMETHODIMP 1.89 +nsSampleImpl::SetValue(const char* aValue) 1.90 +{ 1.91 + NS_PRECONDITION(aValue != nullptr, "null ptr"); 1.92 + if (! aValue) 1.93 + return NS_ERROR_NULL_POINTER; 1.94 + 1.95 + if (mValue) { 1.96 + nsMemory::Free(mValue); 1.97 + } 1.98 + 1.99 + /** 1.100 + * Another buffer passing convention is that buffers passed INTO your 1.101 + * object ARE NOT YOURS. Keep your hands off them, unless they are 1.102 + * declared "inout". If you want to keep the value for posterity, 1.103 + * you will have to make a copy of it. 1.104 + */ 1.105 + mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1); 1.106 + return NS_OK; 1.107 +} 1.108 + 1.109 +NS_IMETHODIMP 1.110 +nsSampleImpl::Poke(const char* aValue) 1.111 +{ 1.112 + return SetValue((char*) aValue); 1.113 +} 1.114 + 1.115 + 1.116 +static void GetStringValue(nsACString& aValue) 1.117 +{ 1.118 + NS_CStringSetData(aValue, "GetValue"); 1.119 +} 1.120 + 1.121 +NS_IMETHODIMP 1.122 +nsSampleImpl::WriteValue(const char* aPrefix) 1.123 +{ 1.124 + NS_PRECONDITION(aPrefix != nullptr, "null ptr"); 1.125 + if (! aPrefix) 1.126 + return NS_ERROR_NULL_POINTER; 1.127 + 1.128 + printf("%s %s\n", aPrefix, mValue); 1.129 + 1.130 + // This next part illustrates the nsEmbedString: 1.131 + nsEmbedString foopy; 1.132 + foopy.Append(char16_t('f')); 1.133 + foopy.Append(char16_t('o')); 1.134 + foopy.Append(char16_t('o')); 1.135 + foopy.Append(char16_t('p')); 1.136 + foopy.Append(char16_t('y')); 1.137 + 1.138 + const char16_t* f = foopy.get(); 1.139 + uint32_t l = foopy.Length(); 1.140 + printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l); 1.141 + 1.142 + nsEmbedCString foopy2; 1.143 + GetStringValue(foopy2); 1.144 + 1.145 + //foopy2.AppendLiteral("foopy"); 1.146 + const char* f2 = foopy2.get(); 1.147 + uint32_t l2 = foopy2.Length(); 1.148 + 1.149 + printf("%s %d\n", f2, l2); 1.150 + 1.151 + return NS_OK; 1.152 +}