view mde/mergetag/DataSet.d @ 17:5f90774ea1ef

Applied the GNU GPL v2 to mde. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Mar 2008 15:14:25 +0000
parents 0047b364b6d9
children 611f7b9063c6
line wrap: on
line source

/* LICENSE BLOCK
Part of mde: a Modular D game-oriented Engine
Copyright © 2007-2008 Diggory Hardy

This program is free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License, version 2, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

/** This module contains the mergetag DataSet class, used for all reading and writing operations.
 */
module mde.mergetag.DataSet;

// package imports
public import mde.mergetag.iface.IDataSection;
import mde.mergetag.DefaultData;
import mde.mergetag.exception;


/**************************************************************************************************
 * 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 IDataSection 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).
 *
 * Could be a struct, except that structs are value types (not reference types).
 */
class DataSet
{
    DefaultData header;			/// Header section.
    IDataSection[ID] sec;		/// Dynamic array of sections
    
    /// Template to return all sections of a child-class type.
    T[ID] getSections (T : IDataSection) () {
        T[ID] ret;
        foreach (ID id, IDataSection s; sec) {
            T x = cast(T) s;
            if (x) ret[id] = x;	// if non-null
        }
        return ret;
    }
}

debug (mdeUnitTest) {
    import tango.util.log.Log : Log, Logger;

    private Logger logger;
    static this() {
        logger = Log.getLogger ("mde.mergetag.DataSet");
    }
    
    unittest {	// Only covers DataSet really.
        DataSet ds = new DataSet;
        ds.sec[cast(ID)"test"] = new DefaultData;
        assert (ds.getSections!(DefaultData)().length == 1);
        ds.sec[cast(ID)"test"].addTag ("int",cast(ID)"T"," -543 ");
        assert (ds.getSections!(DefaultData)()[cast(ID)"test"]._int[cast(ID)"T"] == -543);
    
        logger.info ("Unittest complete.");
    }
}