Mercurial > projects > mde
diff mde/mergetag/dataset.d @ 8:f63f4f41a2dc
Big changes to init; got some way towards input event support; changed mergetag ID to char[] from uint.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 25 Jan 2008 18:17:38 +0000 |
parents | b544c3a7c9ca |
children | 1885a9080f2a |
line wrap: on
line diff
--- a/mde/mergetag/dataset.d Wed Jan 16 12:48:07 2008 +0000 +++ b/mde/mergetag/dataset.d Fri Jan 25 18:17:38 2008 +0000 @@ -1,20 +1,14 @@ -/// This module contains a minimal definition of a MergeTag DataSet. +/// This module contains the mergetag DataSet class together with an interface for DataSections. module mde.mergetag.dataset; // package imports -import mde.mergetag.exception; +public import mde.mergetag.exception; +public import mde.mergetag.defaultdata; // used in DataSet so it should be publically imported -// other mde imports -import mde.text.util; -import mde.text.parse : parse; -import mde.text.format : format; - -// tango imports -import Util = tango.text.Util; -import tango.util.log.Log : Log, Logger; +//import tango.util.log.Log : Log, Logger; /** Typedef for data & section indexes (can be changed to ulong if necessary.) */ -typedef uint ID; +typedef char[] ID; package struct MTFormatVersion { enum VERS : ubyte { // convenient list of all known file format versions @@ -34,26 +28,17 @@ } } -private Logger logger; +/+private Logger logger; static this () { logger = Log.getLogger ("mde.mergetag.dataset"); -} - -struct TextTag { - TextTag opCall (char[] _tp, ID _id, char[] _dt) { - TextTag ret; - ret.tp = _tp; - ret.id = _id; - ret.dt = _dt; - return ret; - } - char[] tp, dt; - ID id; -} +}+/ /************************************************************************************************** * Data class; contains a DataSection class instance for each loaded section of a file. * + * Stored data is available for direct access via header and sec; all functions are just helper + * functions. + * * Any class implementing DataSection may be used to store data; by default a DefaultData class is * used when reading a file. Another class may be used by creating the sections before reading the * file or passing the reader a function to create the sections (see Reader.dataSecCreator). @@ -62,14 +47,9 @@ */ class DataSet { - DefaultData header; /// Header + DefaultData header; /// Header section. DataSection[ID] sec; /// Dynamic array of sections - /// Return a reference to the indexed item - DataSection opIndex(ID i) { - return sec[i]; - } - /// Template to return all sections of a child-class type. T[ID] getSections (T : DataSection) () { T[ID] ret; @@ -113,183 +93,6 @@ debug void debugFunc (); /// Run in debug builds after parseSection. } -/** - * Default DataSection class. - * - * Supports all the basic types currently supported and array versions of - * each (except no arrays of binary or string types; these are already arrays). - * Doesn't support custom types, but inheriting classes may add support. - */ -/* Note: I wrote this comment when the code looked rather worse. It's still partially applicable though. - * - * Due to a failure to use generic programming techniques for most of this (maybe because it's not - * possible or maybe just because I don't know how to use templates properly) a lot of this code is - * really horrible and has to refer to EVERY data member. - * Be really careful if you add any items to this class. - * - * I really don't like having to do things like this, but it provides a lot of benefits such as no - * need to store types and no need to check an argument's type for every access (this could be done - * by throwing errors, but then an incorrect (perhaps hand-edited) data file could cause a lot of - * errors to be thrown). - */ -class DefaultData : DataSection -{ - //BEGIN DATA - /** Data Members - * - * These names are available for direct access. - * - * An alternative access method is to use the provided templates: - * -------------------- - * template Arg(T) { - * alias Name Arg; - * } - * -------------------- - * - * Use with a mixin or directly: - * -------------------- - * mixin Arg!(type); - * auto x = Arg; - * - * type y = Arg!(type).Arg; - * -------------------- - * Note: trying to use Arg!(type) to implicitly refer to Arg!(type).Arg causes compiler errors due to - * --- alias Name Arg; --- - * actually being a mixin. - */ - - bool [ID] _bool; - byte [ID] _byte; /// ditto - short [ID] _short; /// ditto - int [ID] _int; /// ditto - long [ID] _long; /// ditto - ubyte [ID] _ubyte; /// ditto - ushort [ID] _ushort; /// ditto - uint [ID] _uint; /// ditto - ulong [ID] _ulong; /// ditto - - char [ID] _char; /// ditto - - float [ID] _float; /// ditto - double [ID] _double; /// ditto - real [ID] _real; /// ditto - - bool[] [ID] _boolA; /// ditto - byte[] [ID] _byteA; /// ditto - short[] [ID] _shortA; /// ditto - int[] [ID] _intA; /// ditto - long[] [ID] _longA; /// ditto - ubyte[] [ID] _ubyteA; /// ditto - ushort[] [ID] _ushortA; /// ditto - uint[] [ID] _uintA; /// ditto - ulong[] [ID] _ulongA; /// ditto - - char[] [ID] _charA; /// ditto - - float[] [ID] _floatA; /// ditto - double[] [ID] _doubleA; /// ditto - real[] [ID] _realA; /// ditto - - /** Alias names */ - alias _ubyteA _binary; - alias _charA _string; /// ditto - //END DATA - - void addTag (char[] tp, ID id, char[] dt) { /// Supports all standard types. - if (tp.length == 0) throw new MTUnknownTypeException; - // split list up a bit for performance: - if (tp[0] < 'l') { - if (tp[0] < 'd') { - mixin ( `if (tp == "binary") addTag_add!(ubyte[]) (id, dt);` - ~ addTag_elifIsType_add!(bool) - ~ addTag_elifIsType_add!(bool[]) - ~ addTag_elifIsType_add!(byte) - ~ addTag_elifIsType_add!(byte[]) - ~ addTag_elifIsType_add!(char) - ~ addTag_elifIsType_add!(char[]) - ~ `else throw new MTUnknownTypeException;` ); - } else { - mixin ( `if (tp == "double") addTag_add!(double) (id, dt);` - ~ addTag_elifIsType_add!(double[]) - ~ addTag_elifIsType_add!(float) - ~ addTag_elifIsType_add!(float[]) - ~ addTag_elifIsType_add!(int) - ~ addTag_elifIsType_add!(int[]) - ~ `else throw new MTUnknownTypeException;` ); - } - } else { - if (tp[0] < 'u') { - mixin ( `if (tp == "long") addTag_add!(long) (id, dt);` - ~ addTag_elifIsType_add!(long[]) - ~ addTag_elifIsType_add!(real) - ~ addTag_elifIsType_add!(real[]) - ~ addTag_elifIsType_add!(short) - ~ addTag_elifIsType_add!(short[]) - ~ `else if (tp == "string") addTag_add!(char[]) (id, dt);` - ~ `else throw new MTUnknownTypeException;` ); - } else { - mixin ( `if (tp == "ubyte") addTag_add!(ubyte) (id, dt);` - ~ addTag_elifIsType_add!(ubyte[]) - ~ addTag_elifIsType_add!(ushort) - ~ addTag_elifIsType_add!(ushort[]) - ~ addTag_elifIsType_add!(uint) - ~ addTag_elifIsType_add!(uint[]) - ~ addTag_elifIsType_add!(ulong) - ~ addTag_elifIsType_add!(ulong[]) - ~ `else throw new MTUnknownTypeException;` ); - } - } - // try-catch block removed (caught by read) - } - private template addTag_elifIsType_add(T) { - const addTag_elifIsType_add = - `else if (tp == "`~T.stringof~`")` - `addTag_add!(`~T.stringof~`) (id, dt);` ; - } - private void addTag_add(T) (ID id, char[] dt) { - Arg!(T).Arg[id] = parse!(T) (dt); - } - - void writeAll (ItemDelg itemdlg) { - foreach (id, dt; _charA) itemdlg ("char[]", id, format!(char[])(dt)); - } - debug void debugFunc () {} - - /* These make no attempt to check Arg is valid. - * But if the symbol doesn't exist the complier will throw an error anyway, e.g.: - * Error: identifier '_boolAA' is not defined - */ - template Arg(T : T[]) { - const ArgString = Arg!(T).ArgString ~ `A`; - mixin(`alias `~ArgString~` Arg;`); - } - template Arg(T) { - const ArgString = `_` ~ T.stringof; - mixin(`alias `~ArgString~` Arg;`); - } -} - -/+class DynamicData : DataSection -{ - void*[TypeInfo] data; - -}+/ - -/+ -class TemplateData : DataSection -{ - void addTag (char[] tp, ID id, char[] dt) { - // runtime deduction of tp and aliasing? - // CANNOT add data at runtime though. - } - // will this work? no idea. - // templates can't be used to add non-static elements, so use a static array at index: this - template Data(T,TemplateData* p) { - static T[ID][TemplateData*] Data; - } -} -+/ - unittest { // Only covers DataSet really. DataSet ds = new DataSet; ds.sec[1] = new DefaultData;