dillo  3.0.5
About: dillo is a small, fast, extensible Web browser particularly suitable for older or smaller computers and embedded systems (but only limited or no support for frames, CSS, JavaScript, Java).
  Fossies Dox: dillo-3.0.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
27 namespace lout {
28 
29 namespace object {
30 
31 // ------------
32 // Object
33 // ------------
34 
40 {
41 }
42 
50 bool Object::equals(Object *other)
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 
81 const char *Object::toString()
82 {
85  intoStringBuffer(&sb);
86  char *s = strdup(sb.getChars());
87  return s;
88 }
89 
96 {
97  sb->append("<not further specified object>");
98 }
99 
104 {
105  fprintf (stderr, "Object::sizeOf() should be implemented.\n");
106  return sizeof(Object*);
107 }
108 
109 // ----------------
110 // Comparable
111 // ----------------
112 
118 int Comparable::compareFun(const void *p1, const void *p2)
119 {
120  Comparable *c1 = *(Comparable**)p1;
121  Comparable *c2 = *(Comparable**)p2;
122 
123  if (c1 && c2)
124  return ((c1)->compareTo(c2));
125  else if (c1)
126  return 1;
127  else if (c2)
128  return -1;
129  else
130  return 0;
131 }
132 
133 // -------------
134 // Pointer
135 // -------------
136 
138 {
139  return value == ((Pointer*)other)->value;
140 }
141 
143 {
144 /* For some unknown reason, this doesn't compile on some 64bit platforms:
145  *
146  * if (sizeof (int) == sizeof (void*))
147  * return (int)value;
148  * else
149  * return ((int*)&value)[0] ^ ((int*)&value)[1];
150  */
151 #if SIZEOF_VOID_P == 4
152  // Assuming that sizeof(void*) == sizeof(int); on 32 bit systems.
153  return (int)value;
154 #else
155  // Assuming that sizeof(void*) == 2 * sizeof(int); on 64 bit
156  // systems (int is still 32 bit).
157  // Combine both parts of the pointer value *itself*, not what it
158  // points to, by first referencing it (operator "&"), then
159  // dereferencing it again (operator "[]").
160  return ((intptr_t)value >> 32) ^ ((intptr_t)value);
161 #endif
162 }
163 
165 {
166  char buf[64];
167  snprintf(buf, sizeof(buf), "%p", value);
168  sb->append(buf);
169 }
170 
171 // -------------
172 // Integer
173 // -------------
174 
176 {
177  return value == ((Integer*)other)->value;
178 }
179 
181 {
182  return (int)value;
183 }
184 
186 {
187  char buf[64];
188  sprintf(buf, "%d", value);
189  sb->append(buf);
190 }
191 
193 {
194  return value - ((Integer*)other)->value;
195 }
196 
197 // -----------------
198 // ConstString
199 // -----------------
200 
202 {
203  ConstString *otherString = (ConstString*)other;
204  return
205  this == other ||
206  (str == NULL && otherString->str == NULL) ||
207  (str != NULL && otherString->str != NULL &&
208  strcmp(str, otherString->str) == 0);
209 }
210 
212 {
213  return hashValue(str);
214 }
215 
216 
218 {
219  String *otherString = (String*)other;
220  if (str && otherString->str)
221  return strcmp(str, otherString->str);
222  else if (str)
223  return 1;
224  else if (otherString->str)
225  return -1;
226  else
227  return 0;
228 }
229 
230 
231 int ConstString::hashValue(const char *str)
232 {
233  if (str) {
234  int h = 0;
235  for (int i = 0; str[i]; i++)
236  h = (h * 256 + str[i]);
237  return h;
238  } else
239  return 0;
240 }
241 
243 {
244  sb->append(str);
245 }
246 
247 // ------------
248 // String
249 // ------------
250 
251 String::String (const char *str): ConstString (str ? strdup(str) : NULL)
252 {
253 }
254 
256 {
257  if (str)
258  free((char *)str);
259 }
260 
261 // ------------
262 // Pair
263 // ------------
264 
266 {
267  this->first = first;
268  this->second = second;
269 }
270 
272 {
273  if (first)
274  delete first;
275  if (second)
276  delete second;
277 }
278 
280 {
281  PairBase *otherPair = (PairBase*)other;
282 
283  return
284  // Identical?
285  this == other || (
286  (// Both first parts are NULL, ...
287  (first == NULL && otherPair->first == NULL) ||
288  // ... or both first parts are not NULL and equal
289  (first != NULL && otherPair->first != NULL
290  && first->equals (otherPair->first))) &&
291  // Same with second part.
292  ((second == NULL && otherPair->second == NULL) ||
293  (second != NULL && otherPair->second != NULL
294  && second->equals (otherPair->second))));
295 }
296 
298 {
299  int value = 0;
300 
301  if (first)
302  value ^= first->hashValue();
303  if (second)
304  value ^= second->hashValue();
305 
306  return value;
307 }
308 
310 {
311  sb->append("<pair: ");
312 
313  if (first)
314  first->intoStringBuffer(sb);
315  else
316  sb->append("(nil)");
317 
318  sb->append(",");
319 
320  if (second)
322  else
323  sb->append("(nil)");
324 
325  sb->append(">");
326 }
327 
329 {
330  size_t size = 0;
331 
332  if (first)
333  size += first->sizeOf();
334  if (second)
335  size += second->sizeOf();
336 
337  return size;
338 }
339 
340 } // namespace object
341 
342 } // namespace lout
lout::object::ConstString::equals
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:201
lout::object::String::String
String(const char *str)
Definition: object.cc:251
lout::object::Integer
An object::Object wrapper for int's.
Definition: object.hh:92
lout::object::PairBase::first
Object * first
Definition: object.hh:147
lout::object::PairBase::PairBase
PairBase(Object *first, Object *second)
Definition: object.cc:265
lout::object::PairBase::sizeOf
size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition: object.cc:328
lout::object::PairBase::second
Object * second
Definition: object.hh:147
lout::object::ConstString
An object::Object wrapper for constant strings (char*).
Definition: object.hh:111
lout::object::Object::sizeOf
virtual size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition: object.cc:103
lout::object::Pointer::equals
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:137
lout::object::Integer::hashValue
int hashValue()
Return a hash value for the object.
Definition: object.cc:180
lout::misc::assertNotReached
void assertNotReached()
Definition: misc.hh:35
lout::object::Integer::intoStringBuffer
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:185
lout::object::PairBase::equals
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:279
lout::object::Pointer::intoStringBuffer
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:164
lout::object::Pointer::hashValue
int hashValue()
Return a hash value for the object.
Definition: object.cc:142
lout::object::Object::intoStringBuffer
virtual void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:95
lout::object::PairBase::intoStringBuffer
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:309
lout::object::PairBase
Definition: object.hh:144
lout::object::Integer::value
int value
Definition: object.hh:94
lout::object::String
An object::Object wrapper for strings (char*).
Definition: object.hh:134
lout::object::Object::clone
virtual Object * clone()
Return an exact copy of the object.
Definition: object.cc:68
lout::object::Object::~Object
virtual ~Object()
The destructor is defined as virtual (but not abstract), so that destruction of Object's works proper...
Definition: object.cc:39
lout::misc::StringBuffer
A class for fast concatenation of a large number of strings.
Definition: misc.hh:493
lout::object::Object
This is the base class for many other classes, which defines very common virtual methods.
Definition: object.hh:24
lout
Definition: container.cc:26
lout::object::Pointer::value
void * value
Definition: object.hh:68
lout::misc::StringBuffer::append
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition: misc.hh:517
lout::object::String::~String
~String()
Definition: object.cc:255
lout::object::ConstString::compareTo
int compareTo(Comparable *other)
Compare two objects c1 and c2.
Definition: object.cc:217
lout::object::Integer::compareTo
int compareTo(Comparable *other)
Compare two objects c1 and c2.
Definition: object.cc:192
lout::object::Pointer
An object::Object wrapper for void pointers.
Definition: object.hh:65
lout::object::Integer::equals
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:175
lout::object::ConstString::hashValue
int hashValue()
Return a hash value for the object.
Definition: object.cc:211
lout::object::Comparable
Instances of a sub class of may be compared (less, greater).
Definition: object.hh:41
lout::object::Object::equals
virtual bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:50
object.hh
lout::object::Object::hashValue
virtual int hashValue()
Return a hash value for the object.
Definition: object.cc:59
lout::object::Comparable::compareFun
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:118
lout::misc::StringBuffer::getChars
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition: misc.cc:92
lout::object::Object::toString
const char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition: object.cc:81
lout::object::ConstString::intoStringBuffer
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:242
lout::object::PairBase::hashValue
int hashValue()
Return a hash value for the object.
Definition: object.cc:297
lout::object::PairBase::~PairBase
~PairBase()
Definition: object.cc:271
lout::object::ConstString::str
const char * str
Definition: object.hh:114