Bug 434602 - After being open a few minutes, large parts of the screen don't repaint...
[mozilla-1.9.git] / xpcom / sample / nsSample.cpp
blobf8c62b6731c59df97bb8fb6042b4e21b19fd7a28
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
13 * License.
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.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.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 ***** */
40 /**
42 * A sample of XPConnect. This file contains an implementation nsSample
43 * of the interface nsISample.
46 #include <stdio.h>
48 #include "nsSample.h"
49 #include "nsMemory.h"
51 #include "nsEmbedString.h"
52 #include "nsIClassInfoImpl.h"
53 ////////////////////////////////////////////////////////////////////////
55 nsSampleImpl::nsSampleImpl() : mValue(nsnull)
57 mValue = (char*)nsMemory::Clone("initial value", 14);
60 nsSampleImpl::~nsSampleImpl()
62 if (mValue)
63 nsMemory::Free(mValue);
66 /**
67 * NS_IMPL_ISUPPORTS1 expands to a simple implementation of the nsISupports
68 * interface. This includes a proper implementation of AddRef, Release,
69 * and QueryInterface. If this class supported more interfaces than just
70 * nsISupports,
71 * you could use NS_IMPL_ADDREF() and NS_IMPL_RELEASE() to take care of the
72 * simple stuff, but you would have to create QueryInterface on your own.
73 * nsSampleFactory.cpp is an example of this approach.
74 * Notice that the second parameter to the macro is name of the interface, and
75 * NOT the #defined IID.
77 * The _CI variant adds support for nsIClassInfo, which permits introspection
78 * and interface flattening.
80 NS_IMPL_ISUPPORTS1_CI(nsSampleImpl, nsISample)
82 /**
83 * Notice that in the protoype for this function, the NS_IMETHOD macro was
84 * used to declare the return type. For the implementation, the return
85 * type is declared by NS_IMETHODIMP
87 NS_IMETHODIMP
88 nsSampleImpl::GetValue(char** aValue)
90 NS_PRECONDITION(aValue != nsnull, "null ptr");
91 if (! aValue)
92 return NS_ERROR_NULL_POINTER;
94 if (mValue) {
95 /**
96 * GetValue's job is to return data known by an instance of
97 * nsSampleImpl to the outside world. If we were to simply return
98 * a pointer to data owned by this instance, and the client were to
99 * free it, bad things would surely follow.
100 * On the other hand, if we create a new copy of the data for our
101 * client, and it turns out that client is implemented in JavaScript,
102 * there would be no way to free the buffer. The solution to the
103 * buffer ownership problem is the nsMemory singleton. Any buffer
104 * returned by an XPCOM method should be allocated by the nsMemory.
105 * This convention lets things like JavaScript reflection do their
106 * job, and simplifies the way C++ clients deal with returned buffers.
108 *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1);
109 if (! *aValue)
110 return NS_ERROR_NULL_POINTER;
112 else {
113 *aValue = nsnull;
115 return NS_OK;
118 NS_IMETHODIMP
119 nsSampleImpl::SetValue(const char* aValue)
121 NS_PRECONDITION(aValue != nsnull, "null ptr");
122 if (! aValue)
123 return NS_ERROR_NULL_POINTER;
125 if (mValue) {
126 nsMemory::Free(mValue);
130 * Another buffer passing convention is that buffers passed INTO your
131 * object ARE NOT YOURS. Keep your hands off them, unless they are
132 * declared "inout". If you want to keep the value for posterity,
133 * you will have to make a copy of it.
135 mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1);
136 return NS_OK;
139 NS_IMETHODIMP
140 nsSampleImpl::Poke(const char* aValue)
142 return SetValue((char*) aValue);
146 static void GetStringValue(nsACString& aValue)
148 NS_CStringSetData(aValue, "GetValue");
151 NS_IMETHODIMP
152 nsSampleImpl::WriteValue(const char* aPrefix)
154 NS_PRECONDITION(aPrefix != nsnull, "null ptr");
155 if (! aPrefix)
156 return NS_ERROR_NULL_POINTER;
158 printf("%s %s\n", aPrefix, mValue);
160 // This next part illustrates the nsEmbedString:
161 nsEmbedString foopy;
162 foopy.Append(PRUnichar('f'));
163 foopy.Append(PRUnichar('o'));
164 foopy.Append(PRUnichar('o'));
165 foopy.Append(PRUnichar('p'));
166 foopy.Append(PRUnichar('y'));
168 const PRUnichar* f = foopy.get();
169 PRUint32 l = foopy.Length();
170 printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l);
172 nsEmbedCString foopy2;
173 GetStringValue(foopy2);
175 //foopy2.AppendLiteral("foopy");
176 const char* f2 = foopy2.get();
177 PRUint32 l2 = foopy2.Length();
179 printf("%s %d\n", f2, l2);
181 return NS_OK;