serialize.hh revision 449
12623SN/A/* 22623SN/A * Copyright (c) 2003 The Regents of The University of Michigan 32623SN/A * All rights reserved. 42623SN/A * 52623SN/A * Redistribution and use in source and binary forms, with or without 62623SN/A * modification, are permitted provided that the following conditions are 72623SN/A * met: redistributions of source code must retain the above copyright 82623SN/A * notice, this list of conditions and the following disclaimer; 92623SN/A * redistributions in binary form must reproduce the above copyright 102623SN/A * notice, this list of conditions and the following disclaimer in the 112623SN/A * documentation and/or other materials provided with the distribution; 122623SN/A * neither the name of the copyright holders nor the names of its 132623SN/A * contributors may be used to endorse or promote products derived from 142623SN/A * this software without specific prior written permission. 152623SN/A * 162623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272623SN/A */ 282623SN/A 292623SN/A/* @file 302623SN/A * Serialization Interface Declarations 312623SN/A */ 322623SN/A 332623SN/A#ifndef __SERIALIZE_HH__ 342623SN/A#define __SERIALIZE_HH__ 352623SN/A 362623SN/A 372623SN/A#include <list> 382623SN/A#include <iostream> 392623SN/A#include <map> 402623SN/A 412623SN/A#include "sim/host.hh" 422623SN/A#include "sim/configfile.hh" 432623SN/A 442623SN/Aclass Serializable; 452623SN/Aclass Checkpoint; 462623SN/A 472623SN/Atemplate <class T> 482623SN/Avoid paramOut(std::ostream &os, const std::string &name, const T& param); 492623SN/A 502623SN/Atemplate <class T> 512623SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 522623SN/A const std::string &name, T& param); 532623SN/A 542623SN/Atemplate <class T> 552623SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 562623SN/A const T *param, int size); 572623SN/A 582623SN/Atemplate <class T> 592623SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 602623SN/A const std::string &name, T *param, int size); 612623SN/A 622623SN/Avoid 632630SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 642623SN/A const std::string &name, Serializable * ¶m); 652623SN/A 662623SN/A 672623SN/A// 682623SN/A// These macros are streamlined to use in serialize/unserialize 692623SN/A// functions. It's assumed that serialize() has a parameter 'os' for 702630SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 712623SN/A#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 722623SN/A 732623SN/A#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 742623SN/A 752623SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 762623SN/A#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 772623SN/A 782631SN/A#define UNSERIALIZE_ENUM(scalar) \ 792631SN/A do { \ 802631SN/A int tmp; \ 812623SN/A paramIn(cp, section, #scalar, tmp); \ 822623SN/A scalar = (typeof(scalar))tmp; \ 832623SN/A } while (0) 842623SN/A 852623SN/A#define SERIALIZE_ARRAY(member, size) \ 862623SN/A arrayParamOut(os, #member, member, size) 872623SN/A 882623SN/A#define UNSERIALIZE_ARRAY(member, size) \ 892623SN/A arrayParamIn(cp, section, #member, member, size) 902623SN/A 912623SN/A#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 922623SN/A 932623SN/A#define UNSERIALIZE_OBJPTR(objptr) \ 942623SN/A do { \ 952623SN/A Serializable *sptr; \ 962623SN/A objParamIn(cp, section, #objptr, sptr); \ 972623SN/A objptr = dynamic_cast<typeof(objptr)>(sptr); \ 982623SN/A } while (0) 992623SN/A 1002623SN/A/* 1012623SN/A * Basic support for object serialization. 1022623SN/A */ 1032623SN/Aclass Serializable 1042623SN/A{ 1052623SN/A protected: 1062623SN/A void nameOut(std::ostream& os); 1072623SN/A void nameOut(std::ostream& os, const std::string &_name); 1082623SN/A 1092623SN/A public: 1102623SN/A Serializable() {} 1112623SN/A virtual ~Serializable() {} 1122623SN/A 1132623SN/A // manditory virtual function, so objects must provide names 1142623SN/A virtual std::string name() const = 0; 1152623SN/A 1162623SN/A virtual void serialize(std::ostream& os) {} 1172623SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 1182623SN/A 1192623SN/A static Serializable *create(Checkpoint *cp, 1202623SN/A const std::string §ion); 1212623SN/A 1222623SN/A static void serializeAll(); 1232623SN/A static void unserializeGlobals(Checkpoint *cp); 1242623SN/A}; 1252623SN/A 1262623SN/A// 1272623SN/A// A SerializableBuilder serves as an evaluation context for a set of 1282623SN/A// parameters that describe a specific instance of a Serializable. This 1292623SN/A// evaluation context corresponds to a section in the .ini file (as 1302623SN/A// with the base ParamContext) plus an optional node in the 1312623SN/A// configuration hierarchy (the configNode member) for resolving 1322623SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1332623SN/A// derived classes specialize the class for particular subclasses of 1342623SN/A// Serializable (e.g., BaseCache). 1352623SN/A// 1362623SN/A// For typical usage, see the definition of 1372623SN/A// SerializableClass::createObject(). 1382623SN/A// 1392623SN/Aclass SerializableBuilder 1402623SN/A{ 1412623SN/A public: 1422623SN/A 1432623SN/A SerializableBuilder() {} 1442623SN/A 1452623SN/A virtual ~SerializableBuilder() {} 1462623SN/A 1472623SN/A // Create the actual Serializable corresponding to the parameter 1482623SN/A // values in this context. This function is overridden in derived 1492623SN/A // classes to call a specific constructor for a particular 1502623SN/A // subclass of Serializable. 1512623SN/A virtual Serializable *create() = 0; 1522623SN/A}; 1532623SN/A 1542623SN/A// 1552623SN/A// An instance of SerializableClass corresponds to a class derived from 1562623SN/A// Serializable. The SerializableClass instance serves to bind the string 1572623SN/A// name (found in the config file) to a function that creates an 1582623SN/A// instance of the appropriate derived class. 1592623SN/A// 1602623SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1612623SN/A// are first-class objects themselves. 1622623SN/A// 1632623SN/Aclass SerializableClass 1642623SN/A{ 1652623SN/A public: 1662623SN/A 1672623SN/A // Type CreateFunc is a pointer to a function that creates a new 1682623SN/A // simulation object builder based on a .ini-file parameter 1692623SN/A // section (specified by the first string argument), a unique name 1702623SN/A // for the object (specified by the second string argument), and 1712623SN/A // an optional config hierarchy node (specified by the third 1722623SN/A // argument). A pointer to the new SerializableBuilder is returned. 1732623SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1742623SN/A const std::string §ion); 1752623SN/A 1762623SN/A static std::map<std::string,CreateFunc> *classMap; 1772623SN/A 1782623SN/A // Constructor. For example: 1792623SN/A // 1802623SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 1812623SN/A // newBaseCacheSerializableBuilder); 1822623SN/A // 1832623SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 1842623SN/A 1852623SN/A // create Serializable given name of class and pointer to 1862623SN/A // configuration hierarchy node 1872623SN/A static Serializable *createObject(Checkpoint *cp, 1882623SN/A const std::string §ion); 1892623SN/A}; 1902641Sstever@eecs.umich.edu 1912641Sstever@eecs.umich.edu// 1922623SN/A// Macros to encapsulate the magic of declaring & defining 1932623SN/A// SerializableBuilder and SerializableClass objects 1942630SN/A// 1952623SN/A 1962623SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 1972623SN/ASerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 1982623SN/A OBJ_CLASS::createForUnserialize); 1992623SN/A 2002623SN/Aclass Checkpoint 2012623SN/A{ 2022623SN/A private: 2032623SN/A 2042623SN/A IniFile *db; 2052623SN/A const std::string basePath; 2062623SN/A const ConfigNode *configNode; 2072623SN/A std::map<std::string, Serializable*> objMap; 2082623SN/A 2092623SN/A public: 2102623SN/A Checkpoint(const std::string &cpt_dir, const std::string &path, 2112623SN/A const ConfigNode *_configNode); 2122623SN/A 2132623SN/A bool find(const std::string §ion, const std::string &entry, 2142623SN/A std::string &value); 2152623SN/A 2162623SN/A bool findObj(const std::string §ion, const std::string &entry, 2172623SN/A Serializable *&value); 2182623SN/A 2192623SN/A bool sectionExists(const std::string §ion); 2202623SN/A 2212623SN/A // The following static functions have to do with checkpoint 2222623SN/A // creation rather than restoration. This class makes a handy 2232623SN/A // namespace for them though. 2242623SN/A 2252623SN/A // Export current checkpoint directory name so other objects can 2262623SN/A // derive filenames from it (e.g., memory). The return value is 2272623SN/A // guaranteed to end in '/' so filenames can be directly appended. 2282623SN/A // This function is only valid while a checkpoint is being created. 2292623SN/A static std::string dir(); 2302623SN/A 2312623SN/A // Filename for base checkpoint file within directory. 2322623SN/A static const char *baseFilename; 2332623SN/A 2342623SN/A // Set up a checkpoint creation event or series of events. 2352623SN/A static void setup(Tick when, Tick period = 0); 2362623SN/A}; 2372623SN/A 2382623SN/A#endif // __SERIALIZE_HH__ 2392623SN/A