Dillo v3.1.1-98-g318d1f14
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 "dlib/dlib.h"
24#include <stdio.h>
25#include <stdint.h>
26#include <config.h>
27
28namespace lout {
29
30namespace object {
31
32// ------------
33// Object
34// ------------
35
43
52{
54 return false;
55}
56
61{
62 fprintf (stderr, "Object::hashValue() should be implemented.\n");
63 return 0;
64}
65
70{
72 return NULL;
73}
74
82const char *Object::toString()
83{
87 char *s = dStrdup(sb.getChars());
88 return s;
89}
90
97{
98 sb->append("<not further specified object ");
99 sb->appendPointer(this);
100 sb->append(">");
101}
102
107{
108 fprintf (stderr, "Object::sizeOf() should be implemented.\n");
109 return sizeof(Object*);
110}
111
112// ----------------
113// Comparator
114// ----------------
115
117
128int Comparator::compareFun(const void *p1, const void *p2)
129{
130 return compareFunComparator->compare (*(Object**)p1, *(Object**)p2);
131}
132
133// ------------------------
134// StandardComparator
135// ------------------------
136
138{
139 if (o1 && o2)
140 return ((Comparable*)o1)->compareTo ((Comparable*)o2);
141 else if (o1)
142 return 1;
143 else if (o2)
144 return -1;
145 else
146 return 0;
147}
148
150
151// -------------
152// Pointer
153// -------------
154
156{
157 return value == ((Pointer*)other)->value;
158}
159
161{
162/* For some unknown reason, this doesn't compile on some 64bit platforms:
163 *
164 * if (sizeof (int) == sizeof (void*))
165 * return (int)value;
166 * else
167 * return ((int*)&value)[0] ^ ((int*)&value)[1];
168 */
169#if SIZEOF_VOID_P == 4
170 // Assuming that sizeof(void*) == sizeof(int); on 32 bit systems.
171 return (int)value;
172#else
173 // Assuming that sizeof(void*) == 2 * sizeof(int); on 64 bit
174 // systems (int is still 32 bit).
175 // Combine both parts of the pointer value *itself*, not what it
176 // points to, by first referencing it (operator "&"), then
177 // dereferencing it again (operator "[]").
178 return ((intptr_t)value >> 32) ^ ((intptr_t)value);
179#endif
180}
181
183{
184 char buf[64];
185 snprintf(buf, sizeof(buf), "%p", value);
186 sb->append(buf);
187}
188
189// -------------
190// Integer
191// -------------
192
194{
195 return value == ((Integer*)other)->value;
196}
197
199{
200 return (int)value;
201}
202
204{
205 char buf[64];
206 sprintf(buf, "%d", value);
207 sb->append(buf);
208}
209
211{
212 return value - ((Integer*)other)->value;
213}
214
215// -------------
216// Boolean
217// -------------
218
220{
221 bool value2 = ((Boolean*)other)->value;
222 // TODO Does "==" work?
223 return (value && value2) || (!value && value2);
224}
225
227{
228 return value ? 1 : 0;
229}
230
232{
233 sb->append(value ? "true" : "false");
234}
235
237{
238 return (value ? 1 : 0) - (((Boolean*)other)->value ? 1 : 0);
239}
240
241// -----------------
242// ConstString
243// -----------------
244
246{
247 ConstString *otherString = (ConstString*)other;
248 return
249 this == other ||
250 (str == NULL && otherString->str == NULL) ||
251 (str != NULL && otherString->str != NULL &&
252 strcmp(str, otherString->str) == 0);
253}
254
256{
257 return hashValue(str);
258}
259
260
262{
263 String *otherString = (String*)other;
264 if (str && otherString->str)
265 return strcmp(str, otherString->str);
266 else if (str)
267 return 1;
268 else if (otherString->str)
269 return -1;
270 else
271 return 0;
272}
273
274
275int ConstString::hashValue(const char *str)
276{
277 if (str) {
278 int h = 0;
279 for (int i = 0; str[i]; i++)
280 h = (h * 256 + str[i]);
281 return h;
282 } else
283 return 0;
284}
285
290
291// ------------
292// String
293// ------------
294
295String::String (const char *str): ConstString (str ? dStrdup(str) : NULL)
296{
297}
298
300{
301 if (str)
302 free((char *)str);
303}
304
305// ------------
306// Pair
307// ------------
308
310{
311 this->first = first;
312 this->second = second;
313}
314
316{
317 if (first)
318 delete first;
319 if (second)
320 delete second;
321}
322
324{
325 PairBase *otherPair = (PairBase*)other;
326
327 return
328 // Identical?
329 this == other || (
330 (// Both first parts are NULL, ...
331 (first == NULL && otherPair->first == NULL) ||
332 // ... or both first parts are not NULL and equal
333 (first != NULL && otherPair->first != NULL
334 && first->equals (otherPair->first))) &&
335 // Same with second part.
336 ((second == NULL && otherPair->second == NULL) ||
337 (second != NULL && otherPair->second != NULL
338 && second->equals (otherPair->second))));
339}
340
342{
343 int value = 0;
344
345 if (first)
346 value ^= first->hashValue();
347 if (second)
348 value ^= second->hashValue();
349
350 return value;
351}
352
354{
355 sb->append("<pair: ");
356
357 if (first)
359 else
360 sb->append("(nil)");
361
362 sb->append(",");
363
364 if (second)
366 else
367 sb->append("(nil)");
368
369 sb->append(">");
370}
371
373{
374 size_t size = 0;
375
376 if (first)
377 size += first->sizeOf();
378 if (second)
379 size += second->sizeOf();
380
381 return size;
382}
383
384} // namespace object
385
386} // namespace lout
A class for fast concatenation of a large number of strings.
Definition misc.hh:570
void appendPointer(void *p)
Definition misc.hh:596
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:593
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:86
An object::Object wrapper for bool's.
Definition object.hh:144
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:236
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:231
int hashValue()
Return a hash value for the object.
Definition object.cc:226
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:219
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:128
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:255
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:286
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:245
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:261
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:203
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition object.cc:210
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:193
int hashValue()
Return a hash value for the object.
Definition object.cc:198
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:69
virtual size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition object.cc:106
virtual ~Object()
The destructor is defined as virtual (but not abstract), so that destruction of Object's works proper...
Definition object.cc:40
virtual void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:96
const char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition object.cc:82
virtual int hashValue()
Return a hash value for the object.
Definition object.cc:60
virtual bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:51
size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition object.cc:372
int hashValue()
Return a hash value for the object.
Definition object.cc:341
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:353
PairBase(Object *first, Object *second)
Definition object.cc:309
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:323
An object::Object wrapper for void pointers.
Definition object.hh:100
int hashValue()
Return a hash value for the object.
Definition object.cc:160
bool equals(Object *other)
Returns, whether two objects are equal.
Definition object.cc:155
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition object.cc:182
int compare(Object *o1, Object *o2)
Compare two objects o1 and o2.
Definition object.cc:137
An object::Object wrapper for strings (char*).
Definition object.hh:186
String(const char *str)
Definition object.cc:295
char * dStrdup(const char *s)
Definition dlib.c:77
void assertNotReached()
Definition misc.hh:56
StandardComparator standardComparator
Definition object.cc:149