PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 6ms 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. module runtime.typeinfo.ti_struct;
  8. class TypeInfo_Struct : TypeInfo {
  9. char[] toString() {
  10. return name;
  11. }
  12. int opEquals(Object o) {
  13. TypeInfo_Struct s;
  14. return this is o ||
  15. ((s = cast(TypeInfo_Struct)o) !is null &&
  16. this.name == s.name &&
  17. this.init.length == s.init.length);
  18. }
  19. hash_t getHash(void *p) {
  20. hash_t h;
  21. assert(p);
  22. if (xtoHash) {
  23. h = (*xtoHash)(p);
  24. }
  25. else {
  26. //printf("getHash() using default hash\n");
  27. // A sorry hash algorithm.
  28. // Should use the one for strings.
  29. // BUG: relies on the GC not moving objects
  30. for (size_t i = 0; i < init.length; i++) {
  31. h = h * 9 + *cast(ubyte*)p;
  32. p++;
  33. }
  34. }
  35. return h;
  36. }
  37. int equals(void *p2, void *p1) {
  38. int c;
  39. if (p1 == p2)
  40. c = 1;
  41. else if (!p1 || !p2)
  42. c = 0;
  43. else if (xopEquals)
  44. c = (*xopEquals)(p1, p2);
  45. else
  46. // BUG: relies on the GC not moving objects
  47. c = (memcmp(cast(ubyte*)p1, cast(ubyte*)p2, init.length) == 0);
  48. return c;
  49. }
  50. int compare(void *p2, void *p1) {
  51. int c = 0;
  52. // Regard null references as always being "less than"
  53. if (p1 != p2) {
  54. if (p1) {
  55. if (!p2)
  56. c = 1;
  57. else if (xopCmp)
  58. c = (*xopCmp)(p1, p2);
  59. else
  60. // BUG: relies on the GC not moving objects
  61. c = memcmp(cast(ubyte*)p1, cast(ubyte*)p2, init.length);
  62. }
  63. else
  64. c = -1;
  65. }
  66. return c;
  67. }
  68. size_t tsize() {
  69. return init.length;
  70. }
  71. void[] init() {
  72. return m_init;
  73. }
  74. uint flags() {
  75. return m_flags;
  76. }
  77. char[] name;
  78. void[] m_init; // initializer; init.ptr == null if 0 initialize
  79. hash_t function(void*) xtoHash;
  80. int function(void*,void*) xopEquals;
  81. int function(void*,void*) xopCmp;
  82. char[] function(void*) xtoString;
  83. uint m_flags;
  84. }