VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/sample/nsSample.cpp@ 65902

最後變更 在這個檔案從65902是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.9 KB
 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <[email protected]>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39
40/**
41 *
42 * A sample of XPConnect. This file contains an implementation nsSample
43 * of the interface nsISample.
44 *
45 */
46#include <stdio.h>
47
48#include "nsSample.h"
49#include "nsMemory.h"
50
51#include "nsEmbedString.h"
52////////////////////////////////////////////////////////////////////////
53
54nsSampleImpl::nsSampleImpl() : mValue(nsnull)
55{
56 mValue = (char*)nsMemory::Clone("initial value", 14);
57}
58
59nsSampleImpl::~nsSampleImpl()
60{
61 if (mValue)
62 nsMemory::Free(mValue);
63}
64
65/**
66 * NS_IMPL_ISUPPORTS1 expands to a simple implementation of the nsISupports
67 * interface. This includes a proper implementation of AddRef, Release,
68 * and QueryInterface. If this class supported more interfaces than just
69 * nsISupports,
70 * you could use NS_IMPL_ADDREF() and NS_IMPL_RELEASE() to take care of the
71 * simple stuff, but you would have to create QueryInterface on your own.
72 * nsSampleFactory.cpp is an example of this approach.
73 * Notice that the second parameter to the macro is name of the interface, and
74 * NOT the #defined IID.
75 *
76 * The _CI variant adds support for nsIClassInfo, which permits introspection
77 * and interface flattening.
78 */
79NS_IMPL_ISUPPORTS1_CI(nsSampleImpl, nsISample)
80
81/**
82 * Notice that in the protoype for this function, the NS_IMETHOD macro was
83 * used to declare the return type. For the implementation, the return
84 * type is declared by NS_IMETHODIMP
85 */
86NS_IMETHODIMP
87nsSampleImpl::GetValue(char** aValue)
88{
89 NS_PRECONDITION(aValue != nsnull, "null ptr");
90 if (! aValue)
91 return NS_ERROR_NULL_POINTER;
92
93 if (mValue) {
94 /**
95 * GetValue's job is to return data known by an instance of
96 * nsSampleImpl to the outside world. If we were to simply return
97 * a pointer to data owned by this instance, and the client were to
98 * free it, bad things would surely follow.
99 * On the other hand, if we create a new copy of the data for our
100 * client, and it turns out that client is implemented in JavaScript,
101 * there would be no way to free the buffer. The solution to the
102 * buffer ownership problem is the nsMemory singleton. Any buffer
103 * returned by an XPCOM method should be allocated by the nsMemory.
104 * This convention lets things like JavaScript reflection do their
105 * job, and simplifies the way C++ clients deal with returned buffers.
106 */
107 *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1);
108 if (! *aValue)
109 return NS_ERROR_NULL_POINTER;
110 }
111 else {
112 *aValue = nsnull;
113 }
114 return NS_OK;
115}
116
117NS_IMETHODIMP
118nsSampleImpl::SetValue(const char* aValue)
119{
120 NS_PRECONDITION(aValue != nsnull, "null ptr");
121 if (! aValue)
122 return NS_ERROR_NULL_POINTER;
123
124 if (mValue) {
125 nsMemory::Free(mValue);
126 }
127
128 /**
129 * Another buffer passing convention is that buffers passed INTO your
130 * object ARE NOT YOURS. Keep your hands off them, unless they are
131 * declared "inout". If you want to keep the value for posterity,
132 * you will have to make a copy of it.
133 */
134 mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1);
135 return NS_OK;
136}
137
138NS_IMETHODIMP
139nsSampleImpl::Poke(const char* aValue)
140{
141 return SetValue((char*) aValue);
142}
143
144
145static void GetStringValue(nsACString& aValue)
146{
147 NS_CStringSetData(aValue, "GetValue");
148}
149
150NS_IMETHODIMP
151nsSampleImpl::WriteValue(const char* aPrefix)
152{
153 NS_PRECONDITION(aPrefix != nsnull, "null ptr");
154 if (! aPrefix)
155 return NS_ERROR_NULL_POINTER;
156
157 printf("%s %s\n", aPrefix, mValue);
158
159 // This next part illustrates the nsEmbedString:
160 nsEmbedString foopy;
161 foopy.Append(PRUnichar('f'));
162 foopy.Append(PRUnichar('o'));
163 foopy.Append(PRUnichar('o'));
164 foopy.Append(PRUnichar('p'));
165 foopy.Append(PRUnichar('y'));
166
167 const PRUnichar* f = foopy.get();
168 PRUint32 l = foopy.Length();
169 printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l);
170
171 nsEmbedCString foopy2;
172 GetStringValue(foopy2);
173
174 //foopy2.AppendLiteral("foopy");
175 const char* f2 = foopy2.get();
176 PRUint32 l2 = foopy2.Length();
177
178 printf("%s %d\n", f2, l2);
179
180 return NS_OK;
181}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette