Mercurial > projects > ldc
annotate lphobos/internal/objectimpl.d @ 42:0b9b286b67b6 trunk
[svn r46] fix for shift operations
added a simple opengl binding in demos
author | lindquist |
---|---|
date | Fri, 19 Oct 2007 15:16:11 +0200 |
parents | 3cfcb944304e |
children | 0c77619e803b |
rev | line source |
---|---|
1 | 1 |
2 /** | |
3 * Part of the D programming language runtime library. | |
4 * Forms the symbols available to all D programs. Includes | |
5 * Object, which is the root of the class object hierarchy. | |
6 * | |
7 * This module is implicitly imported. | |
8 * Macros: | |
9 * WIKI = Phobos/Object | |
10 */ | |
11 | |
12 /* | |
13 * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com | |
14 * Written by Walter Bright | |
15 * | |
16 * This software is provided 'as-is', without any express or implied | |
17 * warranty. In no event will the authors be held liable for any damages | |
18 * arising from the use of this software. | |
19 * | |
20 * Permission is granted to anyone to use this software for any purpose, | |
21 * including commercial applications, and to alter it and redistribute it | |
22 * freely, in both source and binary form, subject to the following | |
23 * restrictions: | |
24 * | |
25 * o The origin of this software must not be misrepresented; you must not | |
26 * claim that you wrote the original software. If you use this software | |
27 * in a product, an acknowledgment in the product documentation would be | |
28 * appreciated but is not required. | |
29 * o Altered source versions must be plainly marked as such, and must not | |
30 * be misrepresented as being the original software. | |
31 * o This notice may not be removed or altered from any source | |
32 * distribution. | |
33 */ | |
34 | |
35 | |
36 module object; | |
37 | |
38 //import std.outofmemory; | |
39 | |
40 extern (C) | |
41 { /// C's printf function. | |
42 int printf(char *, ...); | |
43 | |
44 int memcmp(void *, void *, size_t); | |
45 void* memcpy(void *, void *, size_t); | |
46 void* calloc(size_t, size_t); | |
47 void* realloc(void*, size_t); | |
48 void free(void*); | |
49 | |
50 //Object _d_newclass(ClassInfo ci); | |
51 } | |
52 | |
53 /// Standard boolean type. | |
54 alias bool bit; | |
55 | |
56 version (LLVM64) | |
57 { | |
58 /** | |
59 * An unsigned integral type large enough to span the memory space. Use for | |
60 * array indices and pointer offsets for maximal portability to | |
61 * architectures that have different memory address ranges. This is | |
62 * analogous to C's size_t. | |
63 */ | |
64 alias ulong size_t; | |
65 | |
66 /** | |
67 * A signed integral type large enough to span the memory space. Use for | |
68 * pointer differences and for size_t differences for maximal portability to | |
69 * architectures that have different memory address ranges. This is | |
70 * analogous to C's ptrdiff_t. | |
71 */ | |
72 alias long ptrdiff_t; | |
73 | |
74 alias ulong hash_t; | |
75 } | |
76 else | |
77 { | |
78 alias uint size_t; | |
79 alias int ptrdiff_t; | |
80 alias uint hash_t; | |
81 } | |
82 | |
83 /+ | |
84 /* ************************* | |
85 * Internal struct pointed to by the hidden .monitor member. | |
86 */ | |
87 struct Monitor | |
88 { | |
89 void delegate(Object)[] delegates; | |
90 | |
91 /* More stuff goes here defined by internal/monitor.c */ | |
92 } | |
93 +/ | |
94 | |
95 /****************** | |
96 * All D class objects inherit from Object. | |
97 */ | |
98 class Object | |
99 { | |
100 void print() | |
101 { | |
102 char[] s = toString(); | |
103 printf("%.*s\n", s.length, s.ptr); | |
104 } | |
105 | |
106 /** | |
107 * Convert Object to a human readable string. | |
108 */ | |
109 char[] toString() | |
110 { | |
111 //return this.classinfo.name; | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
1
diff
changeset
|
112 return "Object.toString: classinfo not yet implemented"; |
1 | 113 } |
114 | |
115 /** | |
116 * Compute hash function for Object. | |
117 */ | |
118 hash_t toHash() | |
119 { | |
120 // BUG: this prevents a compacting GC from working, needs to be fixed | |
121 return cast(uint)cast(void *)this; | |
122 } | |
123 | |
124 /** | |
125 * Compare with another Object obj. | |
126 * Returns: | |
127 * $(TABLE | |
128 * $(TR $(TD this < obj) $(TD < 0)) | |
129 * $(TR $(TD this == obj) $(TD 0)) | |
130 * $(TR $(TD this > obj) $(TD > 0)) | |
131 * ) | |
132 */ | |
133 int opCmp(Object o) | |
134 { | |
135 // BUG: this prevents a compacting GC from working, needs to be fixed | |
136 //return cast(int)cast(void *)this - cast(int)cast(void *)o; | |
137 | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
1
diff
changeset
|
138 assert(0, "need opCmp for class <no classinfo yet>"); |
1 | 139 //throw new Error("need opCmp for class " ~ this.classinfo.name); |
140 } | |
141 | |
142 /** | |
143 * Returns !=0 if this object does have the same contents as obj. | |
144 */ | |
145 int opEquals(Object o) | |
146 { | |
147 return cast(int)(this is o); | |
148 } | |
149 | |
150 /* ** | |
151 * Call delegate dg, passing this to it, when this object gets destroyed. | |
152 * Use extreme caution, as the list of delegates is stored in a place | |
153 * not known to the gc. Thus, if any objects pointed to by one of these | |
154 * delegates gets freed by the gc, calling the delegate will cause a | |
155 * crash. | |
156 * This is only for use by library developers, as it will need to be | |
157 * redone if weak pointers are added or a moving gc is developed. | |
158 */ | |
159 final void notifyRegister(void delegate(Object) dg) | |
160 { | |
161 //printf("notifyRegister(dg = %llx, o = %p)\n", dg, this); | |
162 /+synchronized (this) | |
163 { | |
164 Monitor* m = cast(Monitor*)(cast(void**)this)[1]; | |
165 foreach (inout x; m.delegates) | |
166 { | |
167 if (!x || x == dg) | |
168 { x = dg; | |
169 return; | |
170 } | |
171 } | |
172 | |
173 // Increase size of delegates[] | |
174 auto len = m.delegates.length; | |
175 auto startlen = len; | |
176 if (len == 0) | |
177 { | |
178 len = 4; | |
179 auto p = calloc((void delegate(Object)).sizeof, len); | |
180 if (!p) | |
181 _d_OutOfMemory(); | |
182 m.delegates = (cast(void delegate(Object)*)p)[0 .. len]; | |
183 } | |
184 else | |
185 { | |
186 len += len + 4; | |
187 auto p = realloc(m.delegates.ptr, (void delegate(Object)).sizeof * len); | |
188 if (!p) | |
189 _d_OutOfMemory(); | |
190 m.delegates = (cast(void delegate(Object)*)p)[0 .. len]; | |
191 m.delegates[startlen .. len] = null; | |
192 } | |
193 m.delegates[startlen] = dg; | |
194 }+/ | |
195 } | |
196 | |
197 /* ** | |
198 * Remove delegate dg from the notify list. | |
199 * This is only for use by library developers, as it will need to be | |
200 * redone if weak pointers are added or a moving gc is developed. | |
201 */ | |
202 final void notifyUnRegister(void delegate(Object) dg) | |
203 { | |
204 /+synchronized (this) | |
205 { | |
206 Monitor* m = cast(Monitor*)(cast(void**)this)[1]; | |
207 foreach (inout x; m.delegates) | |
208 { | |
209 if (x == dg) | |
210 x = null; | |
211 } | |
212 }+/ | |
213 } | |
214 | |
215 /****** | |
216 * Create instance of class specified by classname. | |
217 * The class must either have no constructors or have | |
218 * a default constructor. | |
219 * Returns: | |
220 * null if failed | |
221 */ | |
42 | 222 /+static Object factory(char[] classname) |
1 | 223 { |
42 | 224 auto ci = ClassInfo.find(classname); |
1 | 225 if (ci) |
226 { | |
227 return ci.create(); | |
42 | 228 } |
1 | 229 return null; |
42 | 230 }+/ |
1 | 231 } |
232 | |
233 /+ | |
234 extern (C) void _d_notify_release(Object o) | |
235 { | |
236 //printf("_d_notify_release(o = %p)\n", o); | |
237 Monitor* m = cast(Monitor*)(cast(void**)o)[1]; | |
238 if (m.delegates.length) | |
239 { | |
240 auto dgs = m.delegates; | |
241 synchronized (o) | |
242 { | |
243 dgs = m.delegates; | |
244 m.delegates = null; | |
245 } | |
246 | |
247 foreach (dg; dgs) | |
248 { | |
249 if (dg) | |
250 { //printf("calling dg = %llx (%p)\n", dg, o); | |
251 dg(o); | |
252 } | |
253 } | |
254 | |
255 free(dgs.ptr); | |
256 } | |
257 } | |
258 | |
259 /** | |
260 * Information about an interface. | |
261 * A pointer to this appears as the first entry in the interface's vtbl[]. | |
262 */ | |
263 struct Interface | |
264 { | |
265 ClassInfo classinfo; /// .classinfo for this interface (not for containing class) | |
266 void *[] vtbl; | |
267 int offset; /// offset to Interface 'this' from Object 'this' | |
268 } | |
269 | |
270 import std.moduleinit; | |
271 /** | |
272 * Runtime type information about a class. Can be retrieved for any class type | |
273 * or instance by using the .classinfo property. | |
274 * A pointer to this appears as the first entry in the class's vtbl[]. | |
275 */ | |
276 class ClassInfo : Object | |
277 { | |
278 byte[] init; /** class static initializer | |
279 * (init.length gives size in bytes of class) | |
280 */ | |
281 char[] name; /// class name | |
282 void *[] vtbl; /// virtual function pointer table | |
283 Interface[] interfaces; /// interfaces this class implements | |
284 ClassInfo base; /// base class | |
285 void *destructor; | |
286 void (*classInvariant)(Object); | |
287 uint flags; | |
288 // 1: // IUnknown | |
289 // 2: // has no possible pointers into GC memory | |
290 // 4: // has offTi[] member | |
291 // 8: // has constructors | |
292 void *deallocator; | |
293 OffsetTypeInfo[] offTi; | |
294 void function(Object) defaultConstructor; // default Constructor | |
295 | |
296 /************* | |
297 * Search all modules for ClassInfo corresponding to classname. | |
298 * Returns: null if not found | |
299 */ | |
300 static ClassInfo find(char[] classname) | |
301 { | |
302 foreach (m; ModuleInfo.modules()) | |
303 { | |
304 //writefln("module %s, %d", m.name, m.localClasses.length); | |
305 foreach (c; m.localClasses) | |
306 { | |
307 //writefln("\tclass %s", c.name); | |
308 if (c.name == classname) | |
309 return c; | |
310 } | |
311 } | |
312 return null; | |
313 } | |
314 | |
315 /******************** | |
316 * Create instance of Object represented by 'this'. | |
317 */ | |
318 Object create() | |
319 { | |
320 if (flags & 8 && !defaultConstructor) | |
321 return null; | |
322 Object o = _d_newclass(this); | |
323 if (flags & 8 && defaultConstructor) | |
324 { | |
325 defaultConstructor(o); | |
326 } | |
327 return o; | |
328 } | |
329 } | |
330 | |
331 private import std.string; | |
332 | |
333 /** | |
334 * Array of pairs giving the offset and type information for each | |
335 * member in an aggregate. | |
336 */ | |
337 struct OffsetTypeInfo | |
338 { | |
339 size_t offset; /// Offset of member from start of object | |
340 TypeInfo ti; /// TypeInfo for this member | |
341 } | |
342 | |
343 /** | |
344 * Runtime type information about a type. | |
345 * Can be retrieved for any type using a | |
346 * <a href="../expression.html#typeidexpression">TypeidExpression</a>. | |
347 */ | |
348 class TypeInfo | |
349 { | |
350 hash_t toHash() | |
351 { hash_t hash; | |
352 | |
353 foreach (char c; this.toString()) | |
354 hash = hash * 9 + c; | |
355 return hash; | |
356 } | |
357 | |
358 int opCmp(Object o) | |
359 { | |
360 if (this is o) | |
361 return 0; | |
362 TypeInfo ti = cast(TypeInfo)o; | |
363 if (ti is null) | |
364 return 1; | |
365 return std.string.cmp(this.toString(), ti.toString()); | |
366 } | |
367 | |
368 int opEquals(Object o) | |
369 { | |
370 /* TypeInfo instances are singletons, but duplicates can exist | |
371 * across DLL's. Therefore, comparing for a name match is | |
372 * sufficient. | |
373 */ | |
374 if (this is o) | |
375 return 1; | |
376 TypeInfo ti = cast(TypeInfo)o; | |
377 return cast(int)(ti && this.toString() == ti.toString()); | |
378 } | |
379 | |
380 /// Returns a hash of the instance of a type. | |
381 hash_t getHash(void *p) { return cast(uint)p; } | |
382 | |
383 /// Compares two instances for equality. | |
384 int equals(void *p1, void *p2) { return cast(int)(p1 == p2); } | |
385 | |
386 /// Compares two instances for <, ==, or >. | |
387 int compare(void *p1, void *p2) { return 0; } | |
388 | |
389 /// Returns size of the type. | |
390 size_t tsize() { return 0; } | |
391 | |
392 /// Swaps two instances of the type. | |
393 void swap(void *p1, void *p2) | |
394 { | |
395 size_t n = tsize(); | |
396 for (size_t i = 0; i < n; i++) | |
397 { byte t; | |
398 | |
399 t = (cast(byte *)p1)[i]; | |
400 (cast(byte *)p1)[i] = (cast(byte *)p2)[i]; | |
401 (cast(byte *)p2)[i] = t; | |
402 } | |
403 } | |
404 | |
405 /// Get TypeInfo for 'next' type, as defined by what kind of type this is, | |
406 /// null if none. | |
407 TypeInfo next() { return null; } | |
408 | |
409 /// Return default initializer, null if default initialize to 0 | |
410 void[] init() { return null; } | |
411 | |
412 /// Get flags for type: 1 means GC should scan for pointers | |
413 uint flags() { return 0; } | |
414 | |
415 /// Get type information on the contents of the type; null if not available | |
416 OffsetTypeInfo[] offTi() { return null; } | |
417 } | |
418 | |
419 class TypeInfo_Typedef : TypeInfo | |
420 { | |
421 char[] toString() { return name; } | |
422 | |
423 int opEquals(Object o) | |
424 { TypeInfo_Typedef c; | |
425 | |
426 return cast(int) | |
427 (this is o || | |
428 ((c = cast(TypeInfo_Typedef)o) !is null && | |
429 this.name == c.name && | |
430 this.base == c.base)); | |
431 } | |
432 | |
433 hash_t getHash(void *p) { return base.getHash(p); } | |
434 int equals(void *p1, void *p2) { return base.equals(p1, p2); } | |
435 int compare(void *p1, void *p2) { return base.compare(p1, p2); } | |
436 size_t tsize() { return base.tsize(); } | |
437 void swap(void *p1, void *p2) { return base.swap(p1, p2); } | |
438 | |
439 TypeInfo next() { return base.next(); } | |
440 uint flags() { return base.flags(); } | |
441 void[] init() { return m_init.length ? m_init : base.init(); } | |
442 | |
443 TypeInfo base; | |
444 char[] name; | |
445 void[] m_init; | |
446 } | |
447 | |
448 class TypeInfo_Enum : TypeInfo_Typedef | |
449 { | |
450 } | |
451 | |
452 class TypeInfo_Pointer : TypeInfo | |
453 { | |
454 char[] toString() { return m_next.toString() ~ "*"; } | |
455 | |
456 int opEquals(Object o) | |
457 { TypeInfo_Pointer c; | |
458 | |
459 return this is o || | |
460 ((c = cast(TypeInfo_Pointer)o) !is null && | |
461 this.m_next == c.m_next); | |
462 } | |
463 | |
464 hash_t getHash(void *p) | |
465 { | |
466 return cast(uint)*cast(void* *)p; | |
467 } | |
468 | |
469 int equals(void *p1, void *p2) | |
470 { | |
471 return cast(int)(*cast(void* *)p1 == *cast(void* *)p2); | |
472 } | |
473 | |
474 int compare(void *p1, void *p2) | |
475 { | |
476 if (*cast(void* *)p1 < *cast(void* *)p2) | |
477 return -1; | |
478 else if (*cast(void* *)p1 > *cast(void* *)p2) | |
479 return 1; | |
480 else | |
481 return 0; | |
482 } | |
483 | |
484 size_t tsize() | |
485 { | |
486 return (void*).sizeof; | |
487 } | |
488 | |
489 void swap(void *p1, void *p2) | |
490 { void* tmp; | |
491 tmp = *cast(void**)p1; | |
492 *cast(void**)p1 = *cast(void**)p2; | |
493 *cast(void**)p2 = tmp; | |
494 } | |
495 | |
496 TypeInfo next() { return m_next; } | |
497 uint flags() { return 1; } | |
498 | |
499 TypeInfo m_next; | |
500 } | |
501 | |
502 class TypeInfo_Array : TypeInfo | |
503 { | |
504 char[] toString() { return value.toString() ~ "[]"; } | |
505 | |
506 int opEquals(Object o) | |
507 { TypeInfo_Array c; | |
508 | |
509 return cast(int) | |
510 (this is o || | |
511 ((c = cast(TypeInfo_Array)o) !is null && | |
512 this.value == c.value)); | |
513 } | |
514 | |
515 hash_t getHash(void *p) | |
516 { size_t sz = value.tsize(); | |
517 hash_t hash = 0; | |
518 void[] a = *cast(void[]*)p; | |
519 for (size_t i = 0; i < a.length; i++) | |
520 hash += value.getHash(a.ptr + i * sz); | |
521 return hash; | |
522 } | |
523 | |
524 int equals(void *p1, void *p2) | |
525 { | |
526 void[] a1 = *cast(void[]*)p1; | |
527 void[] a2 = *cast(void[]*)p2; | |
528 if (a1.length != a2.length) | |
529 return 0; | |
530 size_t sz = value.tsize(); | |
531 for (size_t i = 0; i < a1.length; i++) | |
532 { | |
533 if (!value.equals(a1.ptr + i * sz, a2.ptr + i * sz)) | |
534 return 0; | |
535 } | |
536 return 1; | |
537 } | |
538 | |
539 int compare(void *p1, void *p2) | |
540 { | |
541 void[] a1 = *cast(void[]*)p1; | |
542 void[] a2 = *cast(void[]*)p2; | |
543 size_t sz = value.tsize(); | |
544 size_t len = a1.length; | |
545 | |
546 if (a2.length < len) | |
547 len = a2.length; | |
548 for (size_t u = 0; u < len; u++) | |
549 { | |
550 int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz); | |
551 if (result) | |
552 return result; | |
553 } | |
554 return cast(int)a1.length - cast(int)a2.length; | |
555 } | |
556 | |
557 size_t tsize() | |
558 { | |
559 return (void[]).sizeof; | |
560 } | |
561 | |
562 void swap(void *p1, void *p2) | |
563 { void[] tmp; | |
564 tmp = *cast(void[]*)p1; | |
565 *cast(void[]*)p1 = *cast(void[]*)p2; | |
566 *cast(void[]*)p2 = tmp; | |
567 } | |
568 | |
569 TypeInfo value; | |
570 | |
571 TypeInfo next() | |
572 { | |
573 return value; | |
574 } | |
575 | |
576 uint flags() { return 1; } | |
577 } | |
578 | |
579 class TypeInfo_StaticArray : TypeInfo | |
580 { | |
581 char[] toString() | |
582 { | |
583 return value.toString() ~ "[" ~ std.string.toString(len) ~ "]"; | |
584 } | |
585 | |
586 int opEquals(Object o) | |
587 { TypeInfo_StaticArray c; | |
588 | |
589 return cast(int) | |
590 (this is o || | |
591 ((c = cast(TypeInfo_StaticArray)o) !is null && | |
592 this.len == c.len && | |
593 this.value == c.value)); | |
594 } | |
595 | |
596 hash_t getHash(void *p) | |
597 { size_t sz = value.tsize(); | |
598 hash_t hash = 0; | |
599 for (size_t i = 0; i < len; i++) | |
600 hash += value.getHash(p + i * sz); | |
601 return hash; | |
602 } | |
603 | |
604 int equals(void *p1, void *p2) | |
605 { | |
606 size_t sz = value.tsize(); | |
607 | |
608 for (size_t u = 0; u < len; u++) | |
609 { | |
610 if (!value.equals(p1 + u * sz, p2 + u * sz)) | |
611 return 0; | |
612 } | |
613 return 1; | |
614 } | |
615 | |
616 int compare(void *p1, void *p2) | |
617 { | |
618 size_t sz = value.tsize(); | |
619 | |
620 for (size_t u = 0; u < len; u++) | |
621 { | |
622 int result = value.compare(p1 + u * sz, p2 + u * sz); | |
623 if (result) | |
624 return result; | |
625 } | |
626 return 0; | |
627 } | |
628 | |
629 size_t tsize() | |
630 { | |
631 return len * value.tsize(); | |
632 } | |
633 | |
634 void swap(void *p1, void *p2) | |
635 { void* tmp; | |
636 size_t sz = value.tsize(); | |
637 ubyte[16] buffer; | |
638 void* pbuffer; | |
639 | |
640 if (sz < buffer.sizeof) | |
641 tmp = buffer.ptr; | |
642 else | |
643 tmp = pbuffer = (new void[sz]).ptr; | |
644 | |
645 for (size_t u = 0; u < len; u += sz) | |
646 { size_t o = u * sz; | |
647 memcpy(tmp, p1 + o, sz); | |
648 memcpy(p1 + o, p2 + o, sz); | |
649 memcpy(p2 + o, tmp, sz); | |
650 } | |
651 if (pbuffer) | |
652 delete pbuffer; | |
653 } | |
654 | |
655 void[] init() { return value.init(); } | |
656 TypeInfo next() { return value; } | |
657 uint flags() { return value.flags(); } | |
658 | |
659 TypeInfo value; | |
660 size_t len; | |
661 } | |
662 | |
663 class TypeInfo_AssociativeArray : TypeInfo | |
664 { | |
665 char[] toString() | |
666 { | |
667 return value.toString() ~ "[" ~ key.toString() ~ "]"; | |
668 } | |
669 | |
670 int opEquals(Object o) | |
671 { TypeInfo_AssociativeArray c; | |
672 | |
673 return this is o || | |
674 ((c = cast(TypeInfo_AssociativeArray)o) !is null && | |
675 this.key == c.key && | |
676 this.value == c.value); | |
677 } | |
678 | |
679 // BUG: need to add the rest of the functions | |
680 | |
681 size_t tsize() | |
682 { | |
683 return (char[int]).sizeof; | |
684 } | |
685 | |
686 TypeInfo next() { return value; } | |
687 uint flags() { return 1; } | |
688 | |
689 TypeInfo value; | |
690 TypeInfo key; | |
691 } | |
692 | |
693 class TypeInfo_Function : TypeInfo | |
694 { | |
695 char[] toString() | |
696 { | |
697 return next.toString() ~ "()"; | |
698 } | |
699 | |
700 int opEquals(Object o) | |
701 { TypeInfo_Function c; | |
702 | |
703 return this is o || | |
704 ((c = cast(TypeInfo_Function)o) !is null && | |
705 this.next == c.next); | |
706 } | |
707 | |
708 // BUG: need to add the rest of the functions | |
709 | |
710 size_t tsize() | |
711 { | |
712 return 0; // no size for functions | |
713 } | |
714 | |
715 TypeInfo next; | |
716 } | |
717 | |
718 class TypeInfo_Delegate : TypeInfo | |
719 { | |
720 char[] toString() | |
721 { | |
722 return next.toString() ~ " delegate()"; | |
723 } | |
724 | |
725 int opEquals(Object o) | |
726 { TypeInfo_Delegate c; | |
727 | |
728 return this is o || | |
729 ((c = cast(TypeInfo_Delegate)o) !is null && | |
730 this.next == c.next); | |
731 } | |
732 | |
733 // BUG: need to add the rest of the functions | |
734 | |
735 size_t tsize() | |
736 { alias int delegate() dg; | |
737 return dg.sizeof; | |
738 } | |
739 | |
740 uint flags() { return 1; } | |
741 | |
742 TypeInfo next; | |
743 } | |
744 | |
745 class TypeInfo_Class : TypeInfo | |
746 { | |
747 char[] toString() { return info.name; } | |
748 | |
749 int opEquals(Object o) | |
750 { TypeInfo_Class c; | |
751 | |
752 return this is o || | |
753 ((c = cast(TypeInfo_Class)o) !is null && | |
754 this.info.name == c.classinfo.name); | |
755 } | |
756 | |
757 hash_t getHash(void *p) | |
758 { | |
759 Object o = *cast(Object*)p; | |
760 assert(o); | |
761 return o.toHash(); | |
762 } | |
763 | |
764 int equals(void *p1, void *p2) | |
765 { | |
766 Object o1 = *cast(Object*)p1; | |
767 Object o2 = *cast(Object*)p2; | |
768 | |
769 return (o1 is o2) || (o1 && o1.opEquals(o2)); | |
770 } | |
771 | |
772 int compare(void *p1, void *p2) | |
773 { | |
774 Object o1 = *cast(Object*)p1; | |
775 Object o2 = *cast(Object*)p2; | |
776 int c = 0; | |
777 | |
778 // Regard null references as always being "less than" | |
779 if (o1 !is o2) | |
780 { | |
781 if (o1) | |
782 { if (!o2) | |
783 c = 1; | |
784 else | |
785 c = o1.opCmp(o2); | |
786 } | |
787 else | |
788 c = -1; | |
789 } | |
790 return c; | |
791 } | |
792 | |
793 size_t tsize() | |
794 { | |
795 return Object.sizeof; | |
796 } | |
797 | |
798 uint flags() { return 1; } | |
799 | |
800 OffsetTypeInfo[] offTi() | |
801 { | |
802 return (info.flags & 4) ? info.offTi : null; | |
803 } | |
804 | |
805 ClassInfo info; | |
806 } | |
807 | |
808 class TypeInfo_Interface : TypeInfo | |
809 { | |
810 char[] toString() { return info.name; } | |
811 | |
812 int opEquals(Object o) | |
813 { TypeInfo_Interface c; | |
814 | |
815 return this is o || | |
816 ((c = cast(TypeInfo_Interface)o) !is null && | |
817 this.info.name == c.classinfo.name); | |
818 } | |
819 | |
820 hash_t getHash(void *p) | |
821 { | |
822 Interface* pi = **cast(Interface ***)*cast(void**)p; | |
823 Object o = cast(Object)(*cast(void**)p - pi.offset); | |
824 assert(o); | |
825 return o.toHash(); | |
826 } | |
827 | |
828 int equals(void *p1, void *p2) | |
829 { | |
830 Interface* pi = **cast(Interface ***)*cast(void**)p1; | |
831 Object o1 = cast(Object)(*cast(void**)p1 - pi.offset); | |
832 pi = **cast(Interface ***)*cast(void**)p2; | |
833 Object o2 = cast(Object)(*cast(void**)p2 - pi.offset); | |
834 | |
835 return o1 == o2 || (o1 && o1.opCmp(o2) == 0); | |
836 } | |
837 | |
838 int compare(void *p1, void *p2) | |
839 { | |
840 Interface* pi = **cast(Interface ***)*cast(void**)p1; | |
841 Object o1 = cast(Object)(*cast(void**)p1 - pi.offset); | |
842 pi = **cast(Interface ***)*cast(void**)p2; | |
843 Object o2 = cast(Object)(*cast(void**)p2 - pi.offset); | |
844 int c = 0; | |
845 | |
846 // Regard null references as always being "less than" | |
847 if (o1 != o2) | |
848 { | |
849 if (o1) | |
850 { if (!o2) | |
851 c = 1; | |
852 else | |
853 c = o1.opCmp(o2); | |
854 } | |
855 else | |
856 c = -1; | |
857 } | |
858 return c; | |
859 } | |
860 | |
861 size_t tsize() | |
862 { | |
863 return Object.sizeof; | |
864 } | |
865 | |
866 uint flags() { return 1; } | |
867 | |
868 ClassInfo info; | |
869 } | |
870 | |
871 class TypeInfo_Struct : TypeInfo | |
872 { | |
873 char[] toString() { return name; } | |
874 | |
875 int opEquals(Object o) | |
876 { TypeInfo_Struct s; | |
877 | |
878 return this is o || | |
879 ((s = cast(TypeInfo_Struct)o) !is null && | |
880 this.name == s.name && | |
881 this.init.length == s.init.length); | |
882 } | |
883 | |
884 hash_t getHash(void *p) | |
885 { hash_t h; | |
886 | |
887 assert(p); | |
888 if (xtoHash) | |
889 { //printf("getHash() using xtoHash\n"); | |
890 h = (*xtoHash)(p); | |
891 } | |
892 else | |
893 { | |
894 //printf("getHash() using default hash\n"); | |
895 // A sorry hash algorithm. | |
896 // Should use the one for strings. | |
897 // BUG: relies on the GC not moving objects | |
898 for (size_t i = 0; i < init.length; i++) | |
899 { h = h * 9 + *cast(ubyte*)p; | |
900 p++; | |
901 } | |
902 } | |
903 return h; | |
904 } | |
905 | |
906 int equals(void *p2, void *p1) | |
907 { int c; | |
908 | |
909 if (p1 == p2) | |
910 c = 1; | |
911 else if (!p1 || !p2) | |
912 c = 0; | |
913 else if (xopEquals) | |
914 c = (*xopEquals)(p1, p2); | |
915 else | |
916 // BUG: relies on the GC not moving objects | |
917 c = (memcmp(p1, p2, init.length) == 0); | |
918 return c; | |
919 } | |
920 | |
921 int compare(void *p2, void *p1) | |
922 { | |
923 int c = 0; | |
924 | |
925 // Regard null references as always being "less than" | |
926 if (p1 != p2) | |
927 { | |
928 if (p1) | |
929 { if (!p2) | |
930 c = 1; | |
931 else if (xopCmp) | |
932 c = (*xopCmp)(p1, p2); | |
933 else | |
934 // BUG: relies on the GC not moving objects | |
935 c = memcmp(p1, p2, init.length); | |
936 } | |
937 else | |
938 c = -1; | |
939 } | |
940 return c; | |
941 } | |
942 | |
943 size_t tsize() | |
944 { | |
945 return init.length; | |
946 } | |
947 | |
948 void[] init() { return m_init; } | |
949 | |
950 uint flags() { return m_flags; } | |
951 | |
952 char[] name; | |
953 void[] m_init; // initializer; init.ptr == null if 0 initialize | |
954 | |
955 hash_t function(void*) xtoHash; | |
956 int function(void*,void*) xopEquals; | |
957 int function(void*,void*) xopCmp; | |
958 char[] function(void*) xtoString; | |
959 | |
960 uint m_flags; | |
961 } | |
962 | |
963 class TypeInfo_Tuple : TypeInfo | |
964 { | |
965 TypeInfo[] elements; | |
966 | |
967 char[] toString() | |
968 { | |
969 char[] s; | |
970 s = "("; | |
971 foreach (i, element; elements) | |
972 { | |
973 if (i) | |
974 s ~= ','; | |
975 s ~= element.toString(); | |
976 } | |
977 s ~= ")"; | |
978 return s; | |
979 } | |
980 | |
981 int opEquals(Object o) | |
982 { | |
983 if (this is o) | |
984 return 1; | |
985 | |
986 auto t = cast(TypeInfo_Tuple)o; | |
987 if (t && elements.length == t.elements.length) | |
988 { | |
989 for (size_t i = 0; i < elements.length; i++) | |
990 { | |
991 if (elements[i] != t.elements[i]) | |
992 return 0; | |
993 } | |
994 return 1; | |
995 } | |
996 return 0; | |
997 } | |
998 | |
999 hash_t getHash(void *p) | |
1000 { | |
1001 assert(0); | |
1002 } | |
1003 | |
1004 int equals(void *p1, void *p2) | |
1005 { | |
1006 assert(0); | |
1007 } | |
1008 | |
1009 int compare(void *p1, void *p2) | |
1010 { | |
1011 assert(0); | |
1012 } | |
1013 | |
1014 size_t tsize() | |
1015 { | |
1016 assert(0); | |
1017 } | |
1018 | |
1019 void swap(void *p1, void *p2) | |
1020 { | |
1021 assert(0); | |
1022 } | |
1023 } | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
1
diff
changeset
|
1024 +/ |
1 | 1025 |
1026 /** | |
1027 * All recoverable exceptions should be derived from class Exception. | |
1028 */ | |
1029 class Exception : Object | |
1030 { | |
1031 char[] msg; | |
1032 | |
1033 /** | |
1034 * Constructor; msg is a descriptive message for the exception. | |
1035 */ | |
1036 this(char[] msg) | |
1037 { | |
1038 this.msg = msg; | |
1039 } | |
1040 | |
1041 void print() | |
1042 { | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
1
diff
changeset
|
1043 auto str = toString(); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
1
diff
changeset
|
1044 printf("%.*s\n", str.length, str.ptr); |
1 | 1045 } |
1046 | |
1047 char[] toString() { return msg; } | |
1048 } | |
1049 | |
1050 /** | |
1051 * All irrecoverable exceptions should be derived from class Error. | |
1052 */ | |
1053 class Error : Exception | |
1054 { | |
1055 Error next; | |
1056 | |
1057 /** | |
1058 * Constructor; msg is a descriptive message for the exception. | |
1059 */ | |
1060 this(char[] msg) | |
1061 { | |
1062 super(msg); | |
1063 } | |
1064 | |
1065 this(char[] msg, Error next) | |
1066 { | |
1067 super(msg); | |
1068 this.next = next; | |
1069 } | |
1070 } | |
1071 | |
1072 //extern (C) int nullext = 0; |