1 module dvardumper.d; 2 3 public import dvardumper.dumper; 4 5 template V(alias a) 6 { 7 enum string V = __traits(identifier, a); 8 } 9 10 TypeVar toTypeVar(T)(T var, string varname = "") 11 { 12 import std.conv : to; 13 import std.traits; 14 15 TypeVar typeVar; 16 17 static if (isAggregateType!T) { 18 typeVar = new AggregateTypeVar(typeid(var).to!string, var.sizeof) 19 .name(varname); 20 static foreach(member; __traits(allMembers, T)) 21 { 22 (cast(AggregateTypeVar)typeVar) 23 .addField(mixin("var." ~ member).toTypeVar(member)); 24 } 25 26 } else static if (isBasicType!T) { 27 typeVar = new BasicTypeVar(typeid(var).to!string, var.sizeof) 28 .value(var.to!string) 29 .name(varname); 30 } else { 31 typeVar = new UnknownTypeVar(typeid(var).to!string, var.sizeof) 32 .name(varname); 33 } 34 35 return typeVar; 36 } 37 38 abstract class TypeVar 39 { 40 protected: 41 string _name; // var name or field name 42 string _typeName; 43 size_t _size; 44 45 public: 46 this(string typeName, size_t size) 47 { 48 this.typeName(typeName); 49 this.size(size); 50 } 51 52 @property pure 53 string name() 54 { 55 return _name; 56 } 57 58 @property pure 59 typeof(this) name(string name) 60 { 61 _name = name; 62 63 return this; 64 } 65 66 @property pure 67 string typeName() 68 { 69 return _typeName; 70 } 71 72 @property pure 73 typeof(this) typeName(string typeName) 74 { 75 _typeName = typeName; 76 77 return this; 78 } 79 80 @property pure 81 size_t size() 82 { 83 return _size; 84 } 85 86 @property pure 87 typeof(this) size(size_t size) 88 { 89 _size = size; 90 91 return this; 92 } 93 } 94 95 class BasicTypeVar : TypeVar 96 { 97 protected: 98 string _value; 99 100 public: 101 this(string typeName, size_t size) 102 { 103 super(typeName, size); 104 } 105 106 @property pure 107 string value() 108 { 109 return _value; 110 } 111 112 @property pure 113 typeof(this) value(string value) 114 { 115 _value = value; 116 117 return this; 118 } 119 } 120 121 class UnknownTypeVar : TypeVar 122 { 123 this(string typeName, size_t size) 124 { 125 super(typeName, size); 126 } 127 } 128 129 class AggregateTypeVar : TypeVar 130 { 131 protected: 132 TypeVar[string] _fields; 133 134 public: 135 this(string typeName, size_t size) 136 { 137 super(typeName, size); 138 } 139 140 pure 141 void addField(TypeVar typeVar) 142 in 143 { 144 assert(typeVar.name != ""); 145 } 146 body 147 { 148 _fields[typeVar.name] = typeVar; 149 } 150 151 @property pure 152 auto fields() 153 { 154 return _fields; 155 } 156 }