PageRenderTime 38ms CodeModel.GetById 11ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/runtime/typeinfo/ti_struct.d

http://github.com/wilkie/djehuty
D | 102 lines | 71 code | 18 blank | 13 comment | 19 complexity | 352a74539dcd467dcd195cbc831ca194 MD5 | raw file
  1/*
  2 * ti_struct.d
  3 *
  4 * This module implements the TypeInfo for struct types.
  5 *
  6 */
  7
  8module runtime.typeinfo.ti_struct;
  9
 10class TypeInfo_Struct : TypeInfo {
 11	char[] toString() {
 12		return name;
 13	}
 14
 15	int opEquals(Object o) {
 16		TypeInfo_Struct s;
 17
 18		return this is o ||
 19				((s = cast(TypeInfo_Struct)o) !is null &&
 20				 this.name == s.name &&
 21				 this.init.length == s.init.length);
 22	}
 23
 24	hash_t getHash(void *p) {
 25		hash_t h;
 26
 27		assert(p);
 28		if (xtoHash) {
 29			h = (*xtoHash)(p);
 30		}
 31		else {
 32			//printf("getHash() using default hash\n");
 33			// A sorry hash algorithm.
 34			// Should use the one for strings.
 35			// BUG: relies on the GC not moving objects
 36			for (size_t i = 0; i < init.length; i++) {
 37				h = h * 9 + *cast(ubyte*)p;
 38				p++;
 39			}
 40		}
 41		return h;
 42	}
 43
 44	int equals(void *p2, void *p1) {
 45		int c;
 46
 47		if (p1 == p2)
 48			c = 1;
 49		else if (!p1 || !p2)
 50			c = 0;
 51		else if (xopEquals)
 52			c = (*xopEquals)(p1, p2);
 53		else
 54			// BUG: relies on the GC not moving objects
 55			c = (memcmp(cast(ubyte*)p1, cast(ubyte*)p2, init.length) == 0);
 56		return c;
 57	}
 58
 59	int compare(void *p2, void *p1) {
 60		int c = 0;
 61
 62		// Regard null references as always being "less than"
 63		if (p1 != p2) {
 64			if (p1) {
 65				if (!p2)
 66					c = 1;
 67				else if (xopCmp)
 68					c = (*xopCmp)(p1, p2);
 69				else
 70					// BUG: relies on the GC not moving objects
 71					c = memcmp(cast(ubyte*)p1, cast(ubyte*)p2, init.length);
 72			}
 73			else
 74				c = -1;
 75		}
 76		return c;
 77	}
 78
 79	size_t tsize() {
 80		return init.length;
 81	}
 82
 83	void[] init() {
 84		return m_init;
 85	}
 86
 87	uint flags() {
 88		return m_flags;
 89	}
 90
 91	char[] name;
 92	void[] m_init;		// initializer; init.ptr == null if 0 initialize
 93
 94	hash_t function(void*) xtoHash;
 95	int function(void*,void*) xopEquals;
 96	int function(void*,void*) xopCmp;
 97	char[] function(void*) xtoString;
 98
 99	uint m_flags;
100}
101
102