serialize.hh revision 1762
15222Sksewell@umich.edu/* 25254Sksewell@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan 35254Sksewell@umich.edu * All rights reserved. 45254Sksewell@umich.edu * 55222Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are 75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 145254Sksewell@umich.edu * this software without specific prior written permission. 155254Sksewell@umich.edu * 165222Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275254Sksewell@umich.edu */ 285222Sksewell@umich.edu 295254Sksewell@umich.edu/* @file 305254Sksewell@umich.edu * Serialization Interface Declarations 315254Sksewell@umich.edu */ 325222Sksewell@umich.edu 335222Sksewell@umich.edu#ifndef __SERIALIZE_HH__ 345222Sksewell@umich.edu#define __SERIALIZE_HH__ 355222Sksewell@umich.edu 365222Sksewell@umich.edu 375222Sksewell@umich.edu#include <list> 385222Sksewell@umich.edu#include <iostream> 395222Sksewell@umich.edu#include <map> 405222Sksewell@umich.edu 415222Sksewell@umich.edu#include "sim/host.hh" 425222Sksewell@umich.edu#include "sim/configfile.hh" 435222Sksewell@umich.edu 445222Sksewell@umich.educlass Serializable; 455222Sksewell@umich.educlass Checkpoint; 465222Sksewell@umich.edu 475222Sksewell@umich.edutemplate <class T> 485222Sksewell@umich.eduvoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 495222Sksewell@umich.edu 505222Sksewell@umich.edutemplate <class T> 515222Sksewell@umich.eduvoid paramIn(Checkpoint *cp, const std::string §ion, 525222Sksewell@umich.edu const std::string &name, T ¶m); 535222Sksewell@umich.edu 545222Sksewell@umich.edutemplate <class T> 555222Sksewell@umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 565222Sksewell@umich.edu const T *param, int size); 575222Sksewell@umich.edu 585222Sksewell@umich.edutemplate <class T> 595222Sksewell@umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 605222Sksewell@umich.edu const std::string &name, T *param, int size); 615222Sksewell@umich.edu 625222Sksewell@umich.eduvoid 635222Sksewell@umich.eduobjParamIn(Checkpoint *cp, const std::string §ion, 645222Sksewell@umich.edu const std::string &name, Serializable * ¶m); 655222Sksewell@umich.edu 665222Sksewell@umich.edu 675222Sksewell@umich.edu// 685222Sksewell@umich.edu// These macros are streamlined to use in serialize/unserialize 695222Sksewell@umich.edu// functions. It's assumed that serialize() has a parameter 'os' for 705222Sksewell@umich.edu// the ostream, and unserialize() has parameters 'cp' and 'section'. 715222Sksewell@umich.edu#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 725222Sksewell@umich.edu 735222Sksewell@umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 745222Sksewell@umich.edu 755222Sksewell@umich.edu// ENUMs are like SCALARs, but we cast them to ints on the way out 765222Sksewell@umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 775222Sksewell@umich.edu 785222Sksewell@umich.edu#define UNSERIALIZE_ENUM(scalar) \ 795222Sksewell@umich.edu do { \ 805222Sksewell@umich.edu int tmp; \ 815222Sksewell@umich.edu paramIn(cp, section, #scalar, tmp); \ 825222Sksewell@umich.edu scalar = (typeof(scalar))tmp; \ 835222Sksewell@umich.edu } while (0) 845222Sksewell@umich.edu 855222Sksewell@umich.edu#define SERIALIZE_ARRAY(member, size) \ 865222Sksewell@umich.edu arrayParamOut(os, #member, member, size) 875222Sksewell@umich.edu 885222Sksewell@umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 895222Sksewell@umich.edu arrayParamIn(cp, section, #member, member, size) 905222Sksewell@umich.edu 915222Sksewell@umich.edu#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 925222Sksewell@umich.edu 935222Sksewell@umich.edu#define UNSERIALIZE_OBJPTR(objptr) \ 945222Sksewell@umich.edu do { \ 955222Sksewell@umich.edu Serializable *sptr; \ 965222Sksewell@umich.edu objParamIn(cp, section, #objptr, sptr); \ 975222Sksewell@umich.edu objptr = dynamic_cast<typeof(objptr)>(sptr); \ 985222Sksewell@umich.edu } while (0) 995222Sksewell@umich.edu 1005222Sksewell@umich.edu/* 1015222Sksewell@umich.edu * Basic support for object serialization. 1025222Sksewell@umich.edu */ 1035222Sksewell@umich.educlass Serializable 1045222Sksewell@umich.edu{ 1055222Sksewell@umich.edu protected: 1065222Sksewell@umich.edu void nameOut(std::ostream &os); 1075222Sksewell@umich.edu void nameOut(std::ostream &os, const std::string &_name); 1085222Sksewell@umich.edu 1095222Sksewell@umich.edu public: 1105222Sksewell@umich.edu Serializable() {} 1115222Sksewell@umich.edu virtual ~Serializable() {} 1125222Sksewell@umich.edu 1135222Sksewell@umich.edu // manditory virtual function, so objects must provide names 1145222Sksewell@umich.edu virtual const std::string name() const = 0; 1155222Sksewell@umich.edu 1165222Sksewell@umich.edu virtual void serialize(std::ostream &os) {} 1175222Sksewell@umich.edu virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 1185222Sksewell@umich.edu 1195222Sksewell@umich.edu static Serializable *create(Checkpoint *cp, 1205222Sksewell@umich.edu const std::string §ion); 1215222Sksewell@umich.edu 1225222Sksewell@umich.edu static int count; 1235222Sksewell@umich.edu static int maxCount; 1245222Sksewell@umich.edu static void serializeAll(); 1255222Sksewell@umich.edu static void unserializeGlobals(Checkpoint *cp); 1265222Sksewell@umich.edu}; 1275222Sksewell@umich.edu 1285222Sksewell@umich.edu// 1295222Sksewell@umich.edu// A SerializableBuilder serves as an evaluation context for a set of 1305222Sksewell@umich.edu// parameters that describe a specific instance of a Serializable. This 1315222Sksewell@umich.edu// evaluation context corresponds to a section in the .ini file (as 1325222Sksewell@umich.edu// with the base ParamContext) plus an optional node in the 1335222Sksewell@umich.edu// configuration hierarchy (the configNode member) for resolving 1345222Sksewell@umich.edu// Serializable references. SerializableBuilder is an abstract superclass; 1355222Sksewell@umich.edu// derived classes specialize the class for particular subclasses of 1365222Sksewell@umich.edu// Serializable (e.g., BaseCache). 1375222Sksewell@umich.edu// 1385222Sksewell@umich.edu// For typical usage, see the definition of 1395222Sksewell@umich.edu// SerializableClass::createObject(). 1405222Sksewell@umich.edu// 1415222Sksewell@umich.educlass SerializableBuilder 1425222Sksewell@umich.edu{ 1435222Sksewell@umich.edu public: 1445222Sksewell@umich.edu 1455222Sksewell@umich.edu SerializableBuilder() {} 1465222Sksewell@umich.edu 1475222Sksewell@umich.edu virtual ~SerializableBuilder() {} 1485222Sksewell@umich.edu 1495222Sksewell@umich.edu // Create the actual Serializable corresponding to the parameter 1505222Sksewell@umich.edu // values in this context. This function is overridden in derived 1515222Sksewell@umich.edu // classes to call a specific constructor for a particular 1525222Sksewell@umich.edu // subclass of Serializable. 1535222Sksewell@umich.edu virtual Serializable *create() = 0; 1545222Sksewell@umich.edu}; 1555222Sksewell@umich.edu 1565222Sksewell@umich.edu// 1575222Sksewell@umich.edu// An instance of SerializableClass corresponds to a class derived from 1585222Sksewell@umich.edu// Serializable. The SerializableClass instance serves to bind the string 1595222Sksewell@umich.edu// name (found in the config file) to a function that creates an 1605222Sksewell@umich.edu// instance of the appropriate derived class. 1615222Sksewell@umich.edu// 1625222Sksewell@umich.edu// This would be much cleaner in Smalltalk or Objective-C, where types 1635222Sksewell@umich.edu// are first-class objects themselves. 1645222Sksewell@umich.edu// 1655222Sksewell@umich.educlass SerializableClass 1665222Sksewell@umich.edu{ 1675222Sksewell@umich.edu public: 1685222Sksewell@umich.edu 1695222Sksewell@umich.edu // Type CreateFunc is a pointer to a function that creates a new 1705222Sksewell@umich.edu // simulation object builder based on a .ini-file parameter 1715222Sksewell@umich.edu // section (specified by the first string argument), a unique name 1725222Sksewell@umich.edu // for the object (specified by the second string argument), and 1735222Sksewell@umich.edu // an optional config hierarchy node (specified by the third 1745222Sksewell@umich.edu // argument). A pointer to the new SerializableBuilder is returned. 1755222Sksewell@umich.edu typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1765222Sksewell@umich.edu const std::string §ion); 1775222Sksewell@umich.edu 1785222Sksewell@umich.edu static std::map<std::string,CreateFunc> *classMap; 1795222Sksewell@umich.edu 1805222Sksewell@umich.edu // Constructor. For example: 1815222Sksewell@umich.edu // 1825222Sksewell@umich.edu // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 1835222Sksewell@umich.edu // newBaseCacheSerializableBuilder); 1845222Sksewell@umich.edu // 1855222Sksewell@umich.edu SerializableClass(const std::string &className, CreateFunc createFunc); 1865222Sksewell@umich.edu 1875222Sksewell@umich.edu // create Serializable given name of class and pointer to 1885222Sksewell@umich.edu // configuration hierarchy node 1895222Sksewell@umich.edu static Serializable *createObject(Checkpoint *cp, 1905222Sksewell@umich.edu const std::string §ion); 1915222Sksewell@umich.edu}; 1925222Sksewell@umich.edu 1935222Sksewell@umich.edu// 1945222Sksewell@umich.edu// Macros to encapsulate the magic of declaring & defining 1955222Sksewell@umich.edu// SerializableBuilder and SerializableClass objects 1965222Sksewell@umich.edu// 1975222Sksewell@umich.edu 1985222Sksewell@umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 1995222Sksewell@umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 2005222Sksewell@umich.edu OBJ_CLASS::createForUnserialize); 2015222Sksewell@umich.edu 2025222Sksewell@umich.educlass Checkpoint 2035222Sksewell@umich.edu{ 2045222Sksewell@umich.edu private: 2055222Sksewell@umich.edu 2065222Sksewell@umich.edu IniFile *db; 2075222Sksewell@umich.edu const std::string basePath; 2085222Sksewell@umich.edu const ConfigNode *configNode; 2095222Sksewell@umich.edu std::map<std::string, Serializable*> objMap; 2105222Sksewell@umich.edu 2115222Sksewell@umich.edu public: 2125222Sksewell@umich.edu Checkpoint(const std::string &cpt_dir, const std::string &path, 2135222Sksewell@umich.edu const ConfigNode *_configNode); 2145222Sksewell@umich.edu 2155222Sksewell@umich.edu const std::string cptDir; 2165222Sksewell@umich.edu 2175222Sksewell@umich.edu bool find(const std::string §ion, const std::string &entry, 2185222Sksewell@umich.edu std::string &value); 2195222Sksewell@umich.edu 2205222Sksewell@umich.edu bool findObj(const std::string §ion, const std::string &entry, 2215222Sksewell@umich.edu Serializable *&value); 2225222Sksewell@umich.edu 2235222Sksewell@umich.edu bool sectionExists(const std::string §ion); 2245222Sksewell@umich.edu 2255222Sksewell@umich.edu // The following static functions have to do with checkpoint 2265222Sksewell@umich.edu // creation rather than restoration. This class makes a handy 2275222Sksewell@umich.edu // namespace for them though. 2285222Sksewell@umich.edu 2295222Sksewell@umich.edu // Export current checkpoint directory name so other objects can 2305222Sksewell@umich.edu // derive filenames from it (e.g., memory). The return value is 2315222Sksewell@umich.edu // guaranteed to end in '/' so filenames can be directly appended. 2325222Sksewell@umich.edu // This function is only valid while a checkpoint is being created. 2335222Sksewell@umich.edu static std::string dir(); 2345222Sksewell@umich.edu 2355222Sksewell@umich.edu // Filename for base checkpoint file within directory. 2365222Sksewell@umich.edu static const char *baseFilename; 2375222Sksewell@umich.edu 2385222Sksewell@umich.edu // Set up a checkpoint creation event or series of events. 2395222Sksewell@umich.edu static void setup(Tick when, Tick period = 0); 2405222Sksewell@umich.edu}; 2415222Sksewell@umich.edu 2425222Sksewell@umich.edu#endif // __SERIALIZE_HH__ 2435222Sksewell@umich.edu