0
|
1 /// This file contains text removed from dataset.d, which might possibly still be useful.
|
|
2
|
|
3 /**
|
|
4 Class for exceptions of type "Incorrect Type" thrown when trying to read an item. Identical use to class Exception, but provides an easy way to handle only exceptions of this type.
|
|
5 */
|
|
6 class IncorrectType : Exception {
|
|
7 this (char[] s) {
|
|
8 super(s);
|
|
9 }
|
|
10 }
|
|
11
|
|
12 /**
|
|
13 Data class; contains a Data member for each loaded section of a file.
|
|
14
|
|
15 Could be a struct, except structs are value types (not reference).
|
|
16 */
|
|
17 class DataSet
|
|
18 {
|
|
19 /+ Data[SecID] sec; /// Dynamic array of section data +/
|
|
20 void*[char[]][char[]] data;
|
|
21
|
|
22 void*[char[]] opIndex(char[] i) {
|
|
23 return data[i];
|
|
24 }
|
|
25 void*[char[]][char[]] opSlice() {
|
|
26 return data[];
|
|
27 }
|
|
28 void*[char[]][char[]] opSlice(char[] i, char[] j) {
|
|
29 return data[i,j];
|
|
30 }
|
|
31 }
|
|
32
|
|
33 struct Item {
|
|
34 enum Type : ubyte {
|
|
35 _void = 0, // initial type
|
|
36 tuple, dynlist,
|
|
37 _bool, _byte, _ubyte, _short, _ushort, _int, _uint, _long, _ulong, _cent, _ucent,
|
|
38 _char, _wchar, _dchar;
|
|
39 _float, _double, _real,
|
|
40 _ifloat, _idouble, _ireal,
|
|
41 _cfloat, _cdouble, _creal
|
|
42 }
|
|
43 static char[][26] typeName = ["void","tuple",];
|
|
44 Type type;
|
|
45 new union {
|
|
46 Tuple tuple;
|
|
47 DynList dynlist;
|
|
48 // DynMerge dynmerge; merging lists are stored as dynamic lists
|
|
49 bool _bool;
|
|
50 byte _byte;
|
|
51 ubyte _ubyte;
|
|
52 short _short;
|
|
53 ushort _ushort;
|
|
54 int _int;
|
|
55 uint _uint;
|
|
56 long _long;
|
|
57 ulong _ulong;
|
|
58 cent _cent;
|
|
59 ucent _ucent;
|
|
60 char _char;
|
|
61 wchar _wchar;
|
|
62 dchar _dchar;
|
|
63 float _float;
|
|
64 double _double;
|
|
65 real _real;
|
|
66 ifloat _ifloat;
|
|
67 idouble _idouble;
|
|
68 ireal _ireal;
|
|
69 cfloat _cfloat;
|
|
70 cdouble _cdouble;
|
|
71 creal _creal;
|
|
72 }
|
|
73
|
|
74 /** Functions to get data
|
|
75
|
|
76 Each function will, if the element is of the appropriate type, return the element; if the type
|
|
77 is incorrect it will throw an error.
|
|
78 */
|
|
79 bool _bool () {
|
|
80 if (type != _bool) throw new IncorrectType("Incorrect type when trying to read: tried to read as bool when item had type " ~ typeName[type]);
|
|
81 return _bool;
|
|
82 }
|
|
83 int _int () { /// ditto
|
|
84 if (type != _int) throw new IncorrectType("Incorrect type when trying to read: tried to read as int");
|
|
85 return _int;
|
|
86 }
|
|
87 uint _uint () { /// ditto
|
|
88 if (type != _uint) throw new IncorrectType("Incorrect type when trying to read: tried to read as uint");
|
|
89 return _uint;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 struct DynList
|
|
94 {
|
|
95
|
|
96 }
|
|
97
|
|
98 class Data
|
|
99 {
|
|
100 // added & accessed soley by templates
|
|
101 private (uint,void*)[Index] _gd; // generic data
|
|
102 }
|
|
103
|
|
104 // Externally, types are given as a string:
|
|
105 typedef char[] typeIDstr;
|
|
106 private:
|
|
107 // Internally, types are given by a uint for performance:
|
|
108 typedef uint typeID;
|
|
109 typeID[typeIDstr] typeIDTable; // used to look up type typeID
|
|
110
|
|
111 // This (belongs in read.d) contains a table of reading functions for all supported types. Do similarly for writing.
|
|
112 (void function (Data, char[]))[typeID] genericReader;
|
|
113
|
|
114 // Template function for creating a function to read a new type and adding it to genericReader:
|
|
115 /+ don't actually use this without specialization
|
|
116 void addSupport (T) () {
|
|
117 // create a function to read this type from a string and add it into Data.genericData as type void*; put a function pointer into generic Reader
|
|
118 // do same for write support
|
|
119 // create a reader function, accessible by the user of the library, for accessing elements/converting to the proper type
|
|
120 }+/
|
|
121
|
|
122 /**
|
|
123 Get data of the appropriate type.
|
|
124
|
|
125 The function performs a check that the data is of the appropriate type and throws an exception if
|
|
126 not.
|
|
127
|
|
128 Note: can be called as d.get!($(I type))(i).
|
|
129 */
|
|
130 get(T : int) (Data d, Index i) {
|
|
131 return cast(T) *d._gd[i];
|
|
132 }
|
|
133
|
|
134 // add support for basic types (for all basic types):
|
|
135 void addSupport (T : int) () {
|
|
136 T read_int (char[]);
|
|
137 void* get_voidp (T d) {
|
|
138 return cast(void*) &d;
|
|
139 }
|
|
140 }
|
|
141
|
|
142 void addSupport (T : T[]) () { // for any array type
|
|
143 // reader: split input and call appropriate fct to convert sub-strings to type T
|
|
144 // writer: use appropriate fct to convert to substrings; concat into "[val1,val2,...,valn]" format
|
|
145 // access: store as void* to T[] and something like this:
|
|
146 T[] get(Data d, Index i) { // but cannot overload by return-type!
|
|
147 return cast(T[]) genericData[i]
|
|
148 }
|
|
149 }
|