view orange/serialization/archives/Archive.d @ 25:b51e953f79eb experimental

Second step in refactoring the API.
author Jacob Carlborg <doob@me.com>
date Wed, 06 Oct 2010 16:18:02 +0200
parents 55f0a9d5df8d
children 78e5fef4bbf2
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2010 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Feb 6, 2010
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module orange.serialization.archives.Archive;

version (Tango)
	import tango.util.Convert;

else
{
	import std.conv;
	alias ConvError ConversionException;
}

import orange.serialization.archives.ArchiveException;
import orange.util.string;

private enum ArchiveMode
{
	archiving,
	unarchiving
}

struct Array
{
	void* ptr;
	size_t length;
	size_t elementSize;
	
	bool isSliceOf (Array b)
	{
		return ptr >= b.ptr && ptr + length * elementSize <= b.ptr + b.length * b.elementSize;
	}
}

struct Slice
{
	size_t length;
	size_t offset;
	size_t id;
}

interface IArchive
{
	version (Tango) alias void[] IDataType;
	else mixin ("alias immutable(void)[] IDataType;");
	
	void beginArchiving ();
	void beginUnarchiving (IDataType data);
	
	IDataType data ();
	void postProcess ();
	void reset ();
	
	void archiveArray (Array array, string type, string key, size_t id, void delegate () dg);
	void archiveAssociativeArray (string keyType, string valueType, size_t length, string key, size_t id, void delegate () dg);
	
	void archiveEnum (bool value, string key, size_t id);
	void archiveEnum (byte value, string key, size_t id);
	void archiveEnum (char value, string key, size_t id);
	void archiveEnum (dchar value, string key, size_t id);
	void archiveEnum (int value, string key, size_t id);
	void archiveEnum (long value, string key, size_t id);
	void archiveEnum (short value, string key, size_t id);
	void archiveEnum (ubyte value, string key, size_t id);
	void archiveEnum (uint value, string key, size_t id);
	void archiveEnum (ulong value, string key, size_t id);
	void archiveEnum (ushort value, string key, size_t id);
	void archiveEnum (wchar value, string key, size_t id);
	
	void archiveBaseClass (string type, string key, size_t id);
	void archiveNull (string type, string key);
	void archiveObject (string runtimeType, string type, string key, size_t id, void delegate () dg);
	void archivePointer (string key, size_t id);
	void archiveReference (string key, size_t id);
	//void archiveSlice (size_t length, size_t offset, string key, size_t id);
	void archiveStruct (string type, string key, size_t id, void delegate () dg);
	void archiveTypedef (string type, string key, size_t id, void delegate () dg);

	void archive (string value, string key, size_t id);
	void archive (wstring value, string key, size_t id);
	void archive (dstring value, string key, size_t id);	
	void archive (bool value, string key, size_t id);
	void archive (byte value, string key, size_t id);
	//void archive (cdouble value, string key, size_t id);
	//void archive (cent value, string key, size_t id);
	//void archive (cfloat value, string key, size_t id);
	void archive (char value, string key, size_t id);
	//void archive (creal value, string key, size_t id);
	void archive (dchar value, string key, size_t id);
	void archive (double value, string key, size_t id);
	void archive (float value, string key, size_t id);
	//void archive (idouble value, string key, size_t id);
	//void archive (ifloat value, string key, size_t id);
	void archive (int value, string key, size_t id);
	//void archive (ireal value, string key, size_t id);
	void archive (long value, string key, size_t id);
	void archive (real value, string key, size_t id);
	void archive (short value, string key, size_t id);
	void archive (ubyte value, string key, size_t id);
	//void archive (ucent value, string key, size_t id);
	void archive (uint value, string key, size_t id);
	void archive (ulong value, string key, size_t id);
	void archive (ushort value, string key, size_t id);
	void archive (wchar value, string key, size_t id);
}

abstract class Archive (U) : IArchive
{
	version (Tango) alias U[] DataType;
	else mixin ("alias immutable(U)[] DataType;");
	
	alias void delegate (ArchiveException exception, DataType[] data) ErrorCallback;
	
	protected ErrorCallback errorCallback;
	
	protected this (ErrorCallback errorCallback)
	{
		this.errorCallback = errorCallback;
	}
	
	protected DataType toDataType (T) (T value)
	{
		try
			return to!(DataType)(value);
		
		catch (ConversionException e)
			throw new ArchiveException(e);
	}
	
	protected T fromDataType (T) (DataType value)
	{
		try
			return to!(T)(value);
		
		catch (ConversionException e)
			throw new ArchiveException(e);
	}
	
	protected bool isSliceOf (T, U = T) (T[] a, U[] b)
	{
		void* aPtr = a.ptr;
		void* bPtr = b.ptr;
		
		return aPtr >= bPtr && aPtr + a.length * T.sizeof <= bPtr + b.length * U.sizeof;
	}
}