Mercurial > dillo_port1.3
annotate lout/object.cc @ 2104:3e7e5395f0bc
non-ASCII keybindings
Alexander Voigt has kindly done some testing, and it seems that this
makes bindings to most keys on a German keyboard possible -- except
those that need AltGr don't work yet.
author | corvid <corvid@lavabit.com> |
---|---|
date | Thu, 23 Jun 2011 19:24:11 +0000 |
parents | b519799e360d |
children |
rev | line source |
---|---|
347 | 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 | |
972
d7dbd3dcfa38
Updated the GPL copyright note in the source files
Detlef Riekenberg <wine.dev@web.de>
parents:
930
diff
changeset
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
347 | 18 */ |
19 | |
20 | |
21 | |
22 #include "object.hh" | |
23 #include <stdio.h> | |
1861
14c4f613de26
fix Pointer::hashValue() by including config.h
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents:
972
diff
changeset
|
24 #include <config.h> |
347 | 25 |
26 namespace lout { | |
27 | |
28 namespace object { | |
29 | |
30 // ------------ | |
31 // Object | |
32 // ------------ | |
33 | |
34 /** | |
35 * \brief The destructor is defined as virtual (but not abstract), so that | |
36 * destruction of Object's works properly. | |
37 */ | |
38 Object::~Object() | |
39 { | |
40 } | |
41 | |
42 /** | |
43 * \brief Returns, whether two objects are equal. | |
44 * | |
45 * The caller should ensure, that this and the object have the same class; | |
46 * this makes casting of "other" safe. Typically, an implementation should | |
47 * check this == other first, the caller can assume a fast implementation. | |
48 */ | |
49 bool Object::equals(Object *other) | |
50 { | |
51 misc::assertNotReached (); | |
52 return false; | |
53 } | |
54 | |
55 /** | |
56 * \brief Return a hash value for the object. | |
57 */ | |
58 int Object::hashValue() | |
59 { | |
60 fprintf (stderr, "Object::hashValue() should be implemented.\n"); | |
61 return 0; | |
62 } | |
63 | |
64 /** | |
65 * \brief Return an exact copy of the object. | |
66 */ | |
67 Object *Object::clone() | |
68 { | |
69 misc::assertNotReached (); | |
70 return NULL; | |
71 } | |
72 | |
73 /** | |
74 * \brief Use object::Object::intoStringBuffer to return a textual | |
75 * representation of the object. | |
76 * | |
77 * The caller does not have to free the memory, object::Object is responsible | |
78 * for this. | |
79 */ | |
80 const char *Object::toString() | |
81 { | |
82 /** \todo garbage! */ | |
83 misc::StringBuffer sb; | |
84 intoStringBuffer(&sb); | |
85 char *s = strdup(sb.getChars()); | |
86 return s; | |
87 } | |
88 | |
89 /** | |
90 * \brief Store a textual representation of the object in a misc::StringBuffer. | |
91 * | |
92 * This is used by object::Object::toString. | |
93 */ | |
94 void Object::intoStringBuffer(misc::StringBuffer *sb) | |
95 { | |
96 sb->append("<not further specified object>"); | |
97 } | |
98 | |
99 /** | |
100 * \brief Return the number of bytes, this object totally uses. | |
101 */ | |
102 size_t Object::sizeOf() | |
103 { | |
104 fprintf (stderr, "Object::sizeOf() should be implemented.\n"); | |
105 return sizeof(Object*); | |
106 } | |
107 | |
108 // ------------- | |
109 // Pointer | |
110 // ------------- | |
111 | |
112 bool Pointer::equals(Object *other) | |
113 { | |
114 return value == ((Pointer*)other)->value; | |
115 } | |
116 | |
117 int Pointer::hashValue() | |
118 { | |
119 /* For some unknown reason, this doesn't compile on some 64bit platforms: | |
120 * | |
121 * if (sizeof (int) == sizeof (void*)) | |
122 * return (int)value; | |
123 * else | |
124 * return ((int*)value)[0] ^ ((int*)value)[1]; | |
125 */ | |
126 #if SIZEOF_VOID_P == 4 | |
127 return (int)value; | |
128 #else | |
129 return ((int*)value)[0] ^ ((int*)value)[1]; | |
130 #endif | |
131 } | |
132 | |
133 void Pointer::intoStringBuffer(misc::StringBuffer *sb) | |
134 { | |
135 char buf[64]; | |
136 snprintf(buf, sizeof(buf), "0x%p", value); | |
137 sb->append(buf); | |
138 } | |
139 | |
140 // ------------- | |
141 // Integer | |
142 // ------------- | |
143 | |
144 bool Integer::equals(Object *other) | |
145 { | |
146 return value == ((Integer*)other)->value; | |
147 } | |
148 | |
149 int Integer::hashValue() | |
150 { | |
151 return (int)value; | |
152 } | |
153 | |
154 void Integer::intoStringBuffer(misc::StringBuffer *sb) | |
155 { | |
156 char buf[64]; | |
157 sprintf(buf, "%d", value); | |
158 sb->append(buf); | |
159 } | |
160 | |
161 int Integer::compareTo(Comparable *other) | |
162 { | |
163 return value - ((Integer*)other)->value; | |
164 } | |
165 | |
166 // ----------------- | |
167 // ConstString | |
168 // ----------------- | |
169 | |
170 bool ConstString::equals(Object *other) | |
171 { | |
172 ConstString *otherString = (ConstString*)other; | |
173 return | |
174 this == other || | |
175 (str == NULL && otherString->str == NULL) || | |
176 (str != NULL && otherString->str != NULL && | |
177 strcmp(str, otherString->str) == 0); | |
178 } | |
179 | |
180 int ConstString::hashValue() | |
181 { | |
182 return hashValue(str); | |
183 } | |
184 | |
185 | |
186 int ConstString::compareTo(Comparable *other) | |
187 { | |
188 String *otherString = (String*)other; | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
189 if (str && otherString->str) |
347 | 190 return strcmp(str, otherString->str); |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
191 else if (str) |
347 | 192 return 1; |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
193 else if (otherString->str) |
347 | 194 return -1; |
195 else | |
196 return 0; | |
197 } | |
198 | |
199 | |
200 int ConstString::hashValue(const char *str) | |
201 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
202 if (str) { |
347 | 203 int h = 0; |
204 for (int i = 0; str[i]; i++) | |
205 h = (h * 256 + str[i]); | |
206 return h; | |
207 } else | |
208 return 0; | |
209 } | |
210 | |
211 void ConstString::intoStringBuffer(misc::StringBuffer *sb) | |
212 { | |
213 sb->append(str); | |
214 } | |
215 | |
216 // ------------ | |
217 // String | |
218 // ------------ | |
219 | |
220 String::String (const char *str): ConstString (str ? strdup(str) : NULL) | |
221 { | |
222 } | |
223 | |
224 String::~String () | |
225 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
226 if (str) |
2005
b519799e360d
straighten out free/delete/delete[]
corvid <corvid@lavabit.com>
parents:
1861
diff
changeset
|
227 free((char *)str); |
347 | 228 } |
229 | |
230 // ------------ | |
231 // Pair | |
232 // ------------ | |
233 | |
234 PairBase::PairBase(Object *first, Object *second) | |
235 { | |
236 this->first = first; | |
237 this->second = second; | |
238 } | |
239 | |
240 PairBase::~PairBase() | |
241 { | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
242 if (first) |
347 | 243 delete first; |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
244 if (second) |
347 | 245 delete second; |
246 } | |
247 | |
248 bool PairBase::equals(Object *other) | |
249 { | |
250 PairBase *otherPair = (PairBase*)other; | |
251 | |
252 return | |
253 // Identical? | |
442 | 254 this == other || ( |
347 | 255 (// Both first parts are NULL, ... |
256 (first == NULL && otherPair->first == NULL) || | |
257 // ... or both first parts are not NULL and equal | |
258 (first != NULL && otherPair->first != NULL | |
259 && first->equals (otherPair->first))) && | |
260 // Same with second part. | |
261 ((second == NULL && otherPair->second == NULL) || | |
262 (second != NULL && otherPair->second != NULL | |
442 | 263 && second->equals (otherPair->second)))); |
347 | 264 } |
265 | |
266 int PairBase::hashValue() | |
267 { | |
268 int value = 0; | |
269 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
270 if (first) |
347 | 271 value ^= first->hashValue(); |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
272 if (second) |
347 | 273 value ^= second->hashValue(); |
930
b277eed3119c
whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents:
928
diff
changeset
|
274 |
347 | 275 return value; |
276 } | |
277 | |
278 void PairBase::intoStringBuffer(misc::StringBuffer *sb) | |
279 { | |
280 sb->append("<pair: "); | |
281 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
282 if (first) |
347 | 283 first->intoStringBuffer(sb); |
284 else | |
285 sb->append("(nil)"); | |
286 | |
287 sb->append(","); | |
288 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
289 if (second) |
347 | 290 second->intoStringBuffer(sb); |
291 else | |
292 sb->append("(nil)"); | |
293 | |
294 sb->append(">"); | |
295 } | |
296 | |
297 size_t PairBase::sizeOf() | |
298 { | |
299 size_t size = 0; | |
300 | |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
301 if (first) |
347 | 302 size += first->sizeOf(); |
928
7771cf243ca6
's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents:
442
diff
changeset
|
303 if (second) |
347 | 304 size += second->sizeOf(); |
305 | |
306 return size; | |
307 } | |
308 | |
309 } // namespace object | |
310 | |
311 } // namespace lout |