Dillo v3.1.1-14-g8f67d6e0
Loading...
Searching...
No Matches
object.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21
22#include "object.hh"
23#include <stdio.h>
24#include <stdint.h>
25#include <config.h>
26
27namespace lout {
28
29namespace object {
30
31// ------------
32// Object
33// ------------
34
42
51{
53 return false;
54}
55
60{
61 fprintf (stderr, "Object::hashValue() should be implemented.\n");
62 return 0;
63}
64
69{
71 return NULL;
72}
73
81const char *Object::toString()
82{
86 char *s = strdup(sb.getChars());
87 return s;
88}
89
96{
97 sb->append("<not further specified object ");
98 sb->appendPointer(this);
99 sb->append(">");
100}
101
106{
107 fprintf (stderr, "Object::sizeOf() should be implemented.\n");
108 return sizeof(Object*);
109}
110
111// ----------------
112// Comparator
113// ----------------
114
116
127int Comparator::compareFun(const void *p1, const void *p2)
128{
129 return compareFunComparator->compare (*(Object**)p1, *(Object**)p2);
130}
131
132// ------------------------
133// StandardComparator
134// ------------------------
135
137{
138 if (o1 && o2)
139 return ((Comparable*)o1)->compareTo ((Comparable*)o2);
140 else if (o1)
141 return 1;
142 else if (o2)
143 return -1;
144 else
145 return 0;
146}
147
149
150// -------------
151// Pointer
152// -------------
153
155{
156 return value == ((Pointer*)other)->value;
157}
158
160{
161/* For some unknown reason, this doesn't compile on some 64bit platforms:
162 *
163 * if (sizeof (int) == sizeof (void*))
164 * return (int)value;
165 * else
166 * return ((int*)&value)[0] ^ ((int*)&value)[1];
167 */
168#if SIZEOF_VOID_P == 4
169 // Assuming that sizeof(void*) == sizeof(int); on 32 bit systems.
170 return (int)value;
171#else
172 // Assuming that sizeof(void*) == 2 * sizeof(int); on 64 bit
173 // systems (int is still 32 bit).
174 // Combine both parts of the pointer value *itself*, not what it
175 // points to, by first referencing it (operator "&"), then
176 // dereferencing it again (operator "[]").
177 return ((intptr_t)value >> 32) ^ ((intptr_t)value);
178#endif
179}
180
182{
183 char buf[64];
184 snprintf(buf, sizeof(buf), "%p", value);
185 sb->append(buf);
186}
187
188// -------------
189// Integer
190// -------------
191
193{
194 return value == ((Integer*)other)->value;
195}
196
198{
199 return (int)value;
200}
201
203{
204 char buf[64];
205 sprintf(buf, "%d", value);
206 sb->append(buf);
207}
208
210{
211 return value - ((Integer*)other)->value;
212}
213
214// -------------
215// Boolean
216// -------------
217
219{
220 bool value2 = ((Boolean*)other)->value;
221 // TODO Does "==" work?
222 return (value && value2) || (!value && value2);
223}
224
226{
227 return value ? 1 : 0;
228}
229
231{
232 sb->append(value ? "true" : "false");
233}
234
236{
237 return (value ? 1 : 0) - (((Boolean*)other)->value ? 1 : 0);
238}
239
240// -----------------
241// ConstString
242// -----------------
243
245{
246 ConstString *otherString = (ConstString*)other;
247 return
248 this == other ||
249 (str == NULL && otherString->str == NULL) ||
250 (str != NULL && otherString->str != NULL &&
251 strcmp(str, otherString->str) == 0);
252}
253
255{
256 return hashValue(str);
257}
258
259
261{
262 String *otherString = (String*)other;
263 if (str && otherString->str)
264 return strcmp(str, otherString->str);
265 else if (str)
266 return 1;
267 else if (otherString->str)
268 return -1;
269 else
270 return 0;
271}
272
273
274int ConstString::hashValue(const char *str)
275{
276 if (str) {
277 int h = 0;
278 for (int i = 0; str[i]; i++)
279 h = (h * 256 + str[i]);
280 return h;
281 } else
282 return 0;
283}
284
289
290// ------------
291// String
292// ------------
293
294String::String (const char *str): ConstString (str ? strdup(str) : NULL)
295{
296}
297
299{
300 if (str)
301 free((char *)str);
302}
303
304// ------------
305// Pair
306// ------------
307
309{
310 this->first = first;
311 this->second = second;
312}
313
315{
316 if (first)
317 delete first;
318 if (second)
319 delete second;
320}
321
323{
324 PairBase *otherPair = (PairBase*)other;
325
326 return
327 // Identical?
328 this == other || (
329 (// Both first parts are NULL, ...
330 (first == NULL && otherPair->first == NULL) ||
331 // ... or both first parts are not NULL and equal
332 (first != NULL && otherPair->first != NULL
333 && first->equals (otherPair->first))) &&
334 // Same with second part.
335 ((second == NULL && otherPair->second == NULL) ||
336 (second != NULL && otherPair->second != NULL
337 && second->equals (otherPair->second))));
338}
339
341{
342 int value = 0;
343
344 if (first)
345 value ^= first->hashValue();
346 if (second)
347 value ^= second->hashValue();
348
349 return value;
350}
351
353{
354 sb->append("<pair: ");
355
356 if (first)
358 else
359 sb->append("(nil)");
360
361 sb->append(",");
362
363 if (second)
365 else
366 sb->append("(nil)");
367
368 sb->append(">");
369}
370
372{
373 size_t size = 0;
374
375 if (first)
376 size += first->sizeOf();
377 if (second)
378 size += second->sizeOf();
379
380 return size;
381}
382
383} // namespace object
384
385} // namespace lout
A class for fast concatenation of a large number of strings.
Definition misc.hh:566
void appendPointer(void *p)
Definition misc.hh:592
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:589
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:92
An object::Object wrapper for bool's.
Definition object.hh:144
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:235
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:230
int hashValue()
Return a hash value for the object.
Definition object.cc:225
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:218
Instances of a sub class of may be compared (less, greater).
Definition object.hh:42
Used for other orders as the one defined by Comparable.
Definition object.hh:67
static int compareFun(const void *p1, const void *p2)
This static method may be used as compare function for qsort(3) and bsearch(3), for an array of Objec...
Definition object.cc:127
static Comparator * compareFunComparator
Definition object.hh:84
virtual int compare(Object *o1, Object *o2)=0
Compare two objects o1 and o2.
An object::Object wrapper for constant strings (char*).
Definition object.hh:163
int hashValue()
Return a hash value for the object.
Definition object.cc:254
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:285
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:244
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:260
An object::Object wrapper for int's.
Definition object.hh:127
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:202
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:209
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:192
int hashValue()
Return a hash value for the object.
Definition object.cc:197
This is the base class for many other classes, which defines very common virtual methods.
Definition object.hh:25
virtual Object * clone()
Return an exact copy of the object.
Definition object.cc:68
virtual size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition object.cc:105
virtual ~Object()
The destructor is defined as virtual (but not abstract), so that destruction of Object's works proper...
Definition object.cc:39
virtual void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:95
const char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition object.cc:81
virtual int hashValue()
Return a hash value for the object.
Definition object.cc:59
virtual bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:50
size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition object.cc:371
int hashValue()
Return a hash value for the object.
Definition object.cc:340
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:352
PairBase(Object *first, Object *second)
Definition object.cc:308
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:322
An object::Object wrapper for void pointers.
Definition object.hh:100
int hashValue()
Return a hash value for the object.
Definition object.cc:159
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:154
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:181
int compare(Object *o1, Object *o2)
Compare two objects o1 and o2.
Definition object.cc:136
An object::Object wrapper for strings (char*).
Definition object.hh:186
String(const char *str)
Definition object.cc:294
void assertNotReached()
Definition misc.hh:35
StandardComparator standardComparator
Definition object.cc:148