1 module yu.tools.serialize.types; 2 3 import yu.traits; 4 public import std.datetime; 5 6 /** 7 * 数字有字节序,大端字节序 8 * 时间和日期是内置的组合类型,顺序是固定的,其中占用2个byte的有字节序 (日期时间内置组合是因为其字段固定还比较常用,比自定义结构省去存储字段类型的空间) 9 * 10 */ 11 enum Types : ubyte 12 { 13 End = 0x00, 14 //base type 15 Char = 0x01, // 1 byte 16 UChar = 0x02, // 1 byte 17 Byte = Char, // 1 byte 18 UByte = UChar,// 1 byte 19 Bool = 0x03, // 1 byte 20 Short = 0x04, // 2 byte 21 UShort = 0x05, // 2 byte 22 Int = 0x06, // 4 byte 23 UInt = 0x07, // 4 byte 24 Long = 0x08, // 8 byte 25 ULong = 0x09, // 8 byte 26 Float = 0x0A, // 4 byte 27 Double = 0x0B, // 8 byte 28 29 // 内置组合type 30 // Time 序列化后布局: 00(时 1byte) 00(分 1byte) 00(秒 1byte) 00 00(毫秒 2byte) 31 Time = 0x0C, // 5 byte: hour ,minute,second is ubyte, msecond is ushort 32 // Date 序列化后布局: 00 00(年 2byte) 00(月 1byte) 00(日 1byte) 33 Date = 0x0D, // 4 byte: 1 2 是ushort year。 month and day is ubyte 34 // DateTime 序列化后布局: 00 00(年 2byte) 00(月 1byte) 00(日 1byte) 00(时 1byte) 00(分 1byte) 00(秒 1byte) 35 DateTime = 0x0E, // 7 byte: 1 2 是ushort year。 month ,day, hour, minute, second is ubyte 36 37 // 组合 type, 内容是其字段按照顺序分解为基本或者内置组合类型写入,不关心变量名,不保存顺序信息。 38 Struct = 0x0F, 39 // 数组类型, 元素按照从0~$顺序写入,如果是多维数组也是一维一维的记录写入。 40 Array = 0x10, 41 42 // Map = 0x11, Not support 43 } 44 45 struct Time 46 { 47 this(ubyte h, ubyte m, ubyte s = 0x00, ushort ms = 0) { 48 hour = h; 49 minute = m; 50 second = s; 51 msecond = ms; 52 } 53 54 ubyte hour; 55 ubyte minute; 56 ubyte second; 57 ushort msecond; 58 59 string toString(){ 60 import std.string; 61 import std.format; 62 return format("%d:%d:%d.%d",hour,minute,second,msecond); 63 } 64 } 65 66 template isBasicSupport(T) 67 { 68 enum isChar = is(T == char) || is(T == ubyte) || is(T == byte) ; 69 enum isNum = (is(T == short) || is(T == ushort) || is(T == int) || is(T == uint) || is(T == long) || is(T == ulong) || is(T == double) || is(T == float)); 70 enum isDateTime = is(T == Date) || is(T == DateTime) || is(T == Time); 71 static if(isArray!T && !is(T == string)) 72 enum isBSupport = isBasicSupport!(ForeachType!T).isBSupport; 73 else 74 enum isBSupport = is(T == bool) || isChar || isNum || isDateTime || is(T == string) || is(T == struct) ; 75 } 76 77 template isStruct(T) 78 { 79 static if(isArray!T) 80 enum isStruct = isStruct!(ForeachType!T); 81 else 82 enum isStruct = is(T == struct) && !isBasicSupport!(T).isDateTime; 83 } 84 85 template dtTypes(T) 86 { 87 static if(is(T == char) || is(T == byte)) 88 enum dtTypes = Types.Char; 89 else static if(is(T == ubyte)) 90 enum dtTypes = Types.UByte; 91 else static if(is(T == short)) 92 enum dtTypes = Types.Short; 93 else static if(is(T == ushort)) 94 enum dtTypes = Types.UShort; 95 else static if(is(T == int)) 96 enum dtTypes = Types.Int; 97 else static if(is(T == uint)) 98 enum dtTypes = Types.UInt; 99 else static if(is(T == long)) 100 enum dtTypes = Types.Long; 101 else static if(is(T == ulong)) 102 enum dtTypes = Types.ULong; 103 else static if(is(T == float)) 104 enum dtTypes = Types.Float; 105 else static if(is(T == double)) 106 enum dtTypes = Types.Double; 107 else static if(is(T == bool)) 108 enum dtTypes = Types.Bool; 109 else static if(is(T == Date)) 110 enum dtTypes = Types.Date; 111 else static if(is(T == Time)) 112 enum dtTypes = Types.Time; 113 else static if(is(T == DateTime)) 114 enum dtTypes = Types.DateTime; 115 else static if(is(T == struct)) 116 enum dtTypes = Types.Struct; 117 else static if(isArray!T) 118 enum dtTypes = Types.Array; 119 }