view mde/file/mergetag/DataSet.d @ 82:ac1e3fd07275

New ssi file format. (De)serializer now supports non-ascii wide characters (encoded to UTF-8) and no longer supports non-ascii 8-bit chars which would result in bad UTF-8. Moved/renamed a few things left over from the last commit.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 30 Aug 2008 09:37:35 +0100
parents d8fccaa45d5f
children 01f4f5f1acc9
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 as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

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, see <http://www.gnu.org/licenses/>. */

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

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


/**************************************************************************************************
 * 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.file.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 ("char[]",cast(ID)"T"," \"ut tag 1 \" ");
        assert (ds.getSections!(DefaultData)()[cast(ID)"test"].Arg!(char[])[cast(ID)"T"] == "ut tag 1 ");
    
        logger.info ("Unittest complete.");
    }
}