view mde/mergetag/dataset.d.old @ 0:d547009c104c

Repository creation. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 27 Oct 2007 18:05:39 +0100
parents
children
line wrap: on
line source

/// This file contains text removed from dataset.d, which might possibly still be useful.

/**
  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.
*/
class IncorrectType : Exception {
	this (char[] s) {
		super(s);
	}
}

/**
  Data class; contains a Data member for each loaded section of a file.
  
  Could be a struct, except structs are value types (not reference).
*/
class DataSet
{
/+	Data[SecID] sec;		/// Dynamic array of section data +/
	void*[char[]][char[]] data;
	
	void*[char[]] opIndex(char[] i) {
		return data[i];
	}
	void*[char[]][char[]] opSlice() {
		return data[];
	}
	void*[char[]][char[]] opSlice(char[] i, char[] j) {
		return data[i,j];
	}
}

struct Item {
	enum Type : ubyte {
		_void = 0,		// initial type
		tuple, dynlist,
		_bool, _byte, _ubyte, _short, _ushort, _int, _uint, _long, _ulong, _cent, _ucent,
		_char, _wchar, _dchar;
		_float, _double, _real,
		_ifloat, _idouble, _ireal,
		_cfloat, _cdouble, _creal
	}
	static char[][26] typeName = ["void","tuple",];
	Type type;
	new union {
		Tuple	tuple;
		DynList	dynlist;
//		DynMerge dynmerge; merging lists are stored as dynamic lists
		bool	_bool;
		byte	_byte;
		ubyte	_ubyte;
		short	_short;
		ushort	_ushort;
		int	_int;
		uint	_uint;
		long	_long;
		ulong	_ulong;
		cent	_cent;
		ucent	_ucent;
		char	_char;
		wchar	_wchar;
		dchar	_dchar;
		float	_float;
		double	_double;
		real	_real;
		ifloat	_ifloat;
		idouble	_idouble;
		ireal	_ireal;
		cfloat	_cfloat;
		cdouble	_cdouble;
		creal	_creal;
	}
	
	/** Functions to get data
	
	Each function will, if the element is of the appropriate type, return the element; if the type
	is incorrect it will throw an error.
	*/
	bool _bool () {
		if (type != _bool) throw new IncorrectType("Incorrect type when trying to read: tried to read as bool when item had type " ~ typeName[type]);
		return _bool;
	}
	int _int () {	/// ditto
		if (type != _int) throw new IncorrectType("Incorrect type when trying to read: tried to read as int");
		return _int;
	}
	uint _uint () {	/// ditto
		if (type != _uint) throw new IncorrectType("Incorrect type when trying to read: tried to read as uint");
		return _uint;
	}
}

struct DynList
{
	
}

class Data
{
	// added & accessed soley by templates
	private (uint,void*)[Index]	_gd;		// generic data
}

// Externally, types are given as a string:
typedef char[]	typeIDstr;
	private:
// Internally, types are given by a uint for performance:
typedef uint	typeID;
typeID[typeIDstr] typeIDTable;	// used to look up type typeID

// This (belongs in read.d) contains a table of reading functions for all supported types. Do similarly for writing.
(void function (Data, char[]))[typeID] genericReader;

// Template function for creating a function to read a new type and adding it to genericReader:
/+ don't actually use this without specialization
void addSupport (T) () {
	// 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
	// do same for write support
	// create a reader function, accessible by the user of the library, for accessing elements/converting to the proper type
}+/

/**
  Get data of the appropriate type.
  
  The function performs a check that the data is of the appropriate type and throws an exception if
  not.
  
  Note: can be called as d.get!($(I type))(i).
*/
get(T : int) (Data d, Index i) {
	return cast(T) *d._gd[i];
}

// add support for basic types (for all basic types):
void addSupport (T : int) () {
	T read_int (char[]);
	void* get_voidp (T d) {
		return cast(void*) &d;
	}
}

void addSupport (T : T[]) () {	// for any array type
	// reader: split input and call appropriate fct to convert sub-strings to type T
	// writer: use appropriate fct to convert to substrings; concat into "[val1,val2,...,valn]" format
	// access: store as void* to T[] and something like this:
	T[] get(Data d, Index i) {	// but cannot overload by return-type!
		return cast(T[]) genericData[i]
	}
}