xref: /haiku/src/kits/translation/Translator.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 /*****************************************************************************/
2 //               File: Translator.cpp
3 //              Class: BTranslator
4 //   Reimplemented by: Michael Wilber, Translation Kit Team
5 //   Reimplementation: 2002-06-15
6 //
7 // Description: This file contains the BTranslator class, the base class for
8 //              all translators that don't use the BeOS R4/R4.5 add-on method.
9 //
10 //
11 // Copyright (c) 2002 OpenBeOS Project
12 //
13 // Original Version: Copyright 1998, Be Incorporated, All Rights Reserved.
14 //                   Copyright 1995-1997, Jon Watte
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining a
17 // copy of this software and associated documentation files (the "Software"),
18 // to deal in the Software without restriction, including without limitation
19 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 // and/or sell copies of the Software, and to permit persons to whom the
21 // Software is furnished to do so, subject to the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be included
24 // in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 // DEALINGS IN THE SOFTWARE.
33 /*****************************************************************************/
34 
35 #include <Translator.h>
36 
37 // Set refcount to 1
38 // ---------------------------------------------------------------
39 // Constructor
40 //
41 // Sets refcount to 1 and creates a semaphore for locking the
42 // object.
43 //
44 // Preconditions:
45 //
46 // Parameters:
47 //
48 // Postconditions:
49 //
50 // Returns:
51 // ---------------------------------------------------------------
52 BTranslator::BTranslator()
53 {
54 	fRefCount = 1;
55 	fSem = create_sem(1, "BTranslator Lock");
56 }
57 
58 // ---------------------------------------------------------------
59 // Destructor
60 //
61 // Deletes the semaphore and resets it.
62 //
63 // Preconditions:
64 //
65 // Parameters:
66 //
67 // Postconditions:
68 //
69 // Returns:
70 // ---------------------------------------------------------------
71 BTranslator::~BTranslator()
72 {
73 	delete_sem(fSem);
74 	fSem = 0;
75 }
76 
77 // ---------------------------------------------------------------
78 // Acquire
79 //
80 // Increments the refcount and returns a pointer to this object.
81 //
82 // Preconditions:
83 //
84 // Parameters:
85 //
86 // Postconditions:
87 //
88 // Returns: NULL, if failed to acquire the sempaphore
89 //          pointer to this object if successful
90 // ---------------------------------------------------------------
91 BTranslator *BTranslator::Acquire()
92 {
93 	if (fSem > 0 && acquire_sem(fSem) == B_OK) {
94 		fRefCount++;
95 		release_sem(fSem);
96 		return this;
97 	} else
98 		return NULL;
99 }
100 
101 // ---------------------------------------------------------------
102 // Release
103 //
104 // Decrements the refcount and returns a pointer to this object.
105 // When the refcount hits zero, the object is destroyed. This is
106 // so multiple objects can own the BTranslator and it won't get
107 // deleted until all of them are done with it.
108 //
109 // Preconditions:
110 //
111 // Parameters:
112 //
113 // Postconditions:
114 //
115 // Returns: NULL, if failed to acquire the sempaphore or the
116 //                object was just deleted
117 //          pointer to this object if successful
118 // ---------------------------------------------------------------
119 BTranslator *BTranslator::Release()
120 {
121 	if (fSem > 0 && acquire_sem(fSem) == B_OK) {
122 		fRefCount--;
123 		if (fRefCount > 0) {
124 			release_sem(fSem);
125 			return this;
126 		} else {
127 			delete this;
128 			return NULL;
129 		}
130 	} else
131 		return NULL;
132 }
133 
134 // ---------------------------------------------------------------
135 // ReferenceCount
136 //
137 // This function returns the current
138 // refcount. Notice that it is not thread safe.
139 // This function is only meant for fun/debugging.
140 //
141 // Preconditions:
142 //
143 // Parameters:
144 //
145 // Postconditions:
146 //
147 // Returns: the current refcount
148 // ---------------------------------------------------------------
149 int32 BTranslator::ReferenceCount()
150 {
151 	return fRefCount;
152 }
153 
154 // ---------------------------------------------------------------
155 // MakeConfigurationView
156 //
157 // This virtual function is for creating a configuration view
158 // for the translator so the user can change its settings. This
159 // base class version does nothing. Not all BTranslator derived
160 // object are required to support this function.
161 //
162 // Preconditions:
163 //
164 // Parameters: ioExtension, configuration data for the translator
165 //             outView, where the created view is stored
166 //             outText, the bounds of the view are stored here
167 //
168 // Postconditions:
169 //
170 // Returns: B_ERROR
171 // ---------------------------------------------------------------
172 status_t BTranslator::MakeConfigurationView(BMessage *ioExtension,
173 	BView **outView, BRect *outExtent)
174 {
175 	return B_ERROR;
176 }
177 
178 // ---------------------------------------------------------------
179 // GetConfigurationMessage
180 //
181 // Puts the current configuration for the translator into
182 // ioExtension. Not all translators are required to support
183 // this function
184 //
185 // Preconditions:
186 //
187 // Parameters: ioExtension, where the configuration data is stored
188 //
189 // Postconditions:
190 //
191 // Returns: B_ERROR
192 // ---------------------------------------------------------------
193 status_t BTranslator::GetConfigurationMessage(BMessage *ioExtension)
194 {
195 	return B_ERROR;
196 }
197 
198 // ---------------------------------------------------------------
199 // _Reserved_Translator_0
200 //
201 // It does nothing! :) Its only here for past/future binary
202 // compatiblity.
203 //
204 // Preconditions:
205 //
206 // Parameters: n, not used
207 //             p, not used
208 //
209 // Postconditions:
210 //
211 // Returns: B_ERROR
212 // ---------------------------------------------------------------
213 status_t BTranslator::_Reserved_Translator_0(int32 n, void *p)
214 {
215 	return B_ERROR;
216 }
217 
218 // ---------------------------------------------------------------
219 // _Reserved_Translator_1
220 //
221 // It does nothing! :) Its only here for past/future binary
222 // compatiblity.
223 //
224 // Preconditions:
225 //
226 // Parameters: n, not used
227 //             p, not used
228 //
229 // Postconditions:
230 //
231 // Returns: B_ERROR
232 // ---------------------------------------------------------------
233 status_t BTranslator::_Reserved_Translator_1(int32 n, void *p)
234 {
235 	return B_ERROR;
236 }
237 
238 // ---------------------------------------------------------------
239 // _Reserved_Translator_2
240 //
241 // It does nothing! :) Its only here for past/future binary
242 // compatiblity.
243 //
244 // Preconditions:
245 //
246 // Parameters: n, not used
247 //             p, not used
248 //
249 // Postconditions:
250 //
251 // Returns: B_ERROR
252 // ---------------------------------------------------------------
253 status_t BTranslator::_Reserved_Translator_2(int32 n, void *p)
254 {
255 	return B_ERROR;
256 }
257 
258 // ---------------------------------------------------------------
259 // _Reserved_Translator_3
260 //
261 // It does nothing! :) Its only here for past/future binary
262 // compatiblity.
263 //
264 // Preconditions:
265 //
266 // Parameters: n, not used
267 //             p, not used
268 //
269 // Postconditions:
270 //
271 // Returns: B_ERROR
272 // ---------------------------------------------------------------
273 status_t BTranslator::_Reserved_Translator_3(int32 n, void *p)
274 {
275 	return B_ERROR;
276 }
277 
278 // ---------------------------------------------------------------
279 // _Reserved_Translator_4
280 //
281 // It does nothing! :) Its only here for past/future binary
282 // compatiblity.
283 //
284 // Preconditions:
285 //
286 // Parameters: n, not used
287 //             p, not used
288 //
289 // Postconditions:
290 //
291 // Returns: B_ERROR
292 // ---------------------------------------------------------------
293 status_t BTranslator::_Reserved_Translator_4(int32 n, void *p)
294 {
295 	return B_ERROR;
296 }
297 
298 // ---------------------------------------------------------------
299 // _Reserved_Translator_5
300 //
301 // It does nothing! :) Its only here for past/future binary
302 // compatiblity.
303 //
304 // Preconditions:
305 //
306 // Parameters: n, not used
307 //             p, not used
308 //
309 // Postconditions:
310 //
311 // Returns: B_ERROR
312 // ---------------------------------------------------------------
313 status_t BTranslator::_Reserved_Translator_5(int32 n, void *p)
314 {
315 	return B_ERROR;
316 }
317 
318 // ---------------------------------------------------------------
319 // _Reserved_Translator_6
320 //
321 // It does nothing! :) Its only here for past/future binary
322 // compatiblity.
323 //
324 // Preconditions:
325 //
326 // Parameters: n, not used
327 //             p, not used
328 //
329 // Postconditions:
330 //
331 // Returns: B_ERROR
332 // ---------------------------------------------------------------
333 status_t BTranslator::_Reserved_Translator_6(int32 n, void *p)
334 {
335 	return B_ERROR;
336 }
337 
338 // ---------------------------------------------------------------
339 // _Reserved_Translator_7
340 //
341 // It does nothing! :) Its only here for past/future binary
342 // compatiblity.
343 //
344 // Preconditions:
345 //
346 // Parameters: n, not used
347 //             p, not used
348 //
349 // Postconditions:
350 //
351 // Returns: B_ERROR
352 // ---------------------------------------------------------------
353 status_t BTranslator::_Reserved_Translator_7(int32 n, void *p)
354 {
355 	return B_ERROR;
356 }
357