serialize.hh revision 937
112027Sjungma@eit.uni-kl.de/* 212027Sjungma@eit.uni-kl.de * Copyright (c) 2002-2004 The Regents of The University of Michigan 312027Sjungma@eit.uni-kl.de * All rights reserved. 412027Sjungma@eit.uni-kl.de * 512027Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without 612027Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are 712027Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright 812027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer; 912027Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright 1012027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the 1112027Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution; 1212027Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its 1312027Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from 1412027Sjungma@eit.uni-kl.de * this software without specific prior written permission. 1512027Sjungma@eit.uni-kl.de * 1612027Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712027Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812027Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912027Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012027Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112027Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212027Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312027Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412027Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512027Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612027Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712027Sjungma@eit.uni-kl.de */ 2812027Sjungma@eit.uni-kl.de 2912027Sjungma@eit.uni-kl.de/* @file 3012027Sjungma@eit.uni-kl.de * Serialization Interface Declarations 3112027Sjungma@eit.uni-kl.de */ 3212027Sjungma@eit.uni-kl.de 3312027Sjungma@eit.uni-kl.de#ifndef __SERIALIZE_HH__ 3412027Sjungma@eit.uni-kl.de#define __SERIALIZE_HH__ 3512027Sjungma@eit.uni-kl.de 3612027Sjungma@eit.uni-kl.de 3712027Sjungma@eit.uni-kl.de#include <list> 3812027Sjungma@eit.uni-kl.de#include <iostream> 3912027Sjungma@eit.uni-kl.de#include <map> 4012027Sjungma@eit.uni-kl.de 4112027Sjungma@eit.uni-kl.de#include "sim/host.hh" 4212027Sjungma@eit.uni-kl.de#include "sim/configfile.hh" 4312027Sjungma@eit.uni-kl.de 4412027Sjungma@eit.uni-kl.declass Serializable; 4512027Sjungma@eit.uni-kl.declass Checkpoint; 4612027Sjungma@eit.uni-kl.de 4712027Sjungma@eit.uni-kl.detemplate <class T> 4812027Sjungma@eit.uni-kl.devoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 4912027Sjungma@eit.uni-kl.de 5012027Sjungma@eit.uni-kl.detemplate <class T> 5112027Sjungma@eit.uni-kl.devoid paramIn(Checkpoint *cp, const std::string §ion, 5212027Sjungma@eit.uni-kl.de const std::string &name, T ¶m); 5312027Sjungma@eit.uni-kl.de 5412027Sjungma@eit.uni-kl.detemplate <class T> 5512027Sjungma@eit.uni-kl.devoid arrayParamOut(std::ostream &os, const std::string &name, 5612027Sjungma@eit.uni-kl.de const T *param, int size); 5712027Sjungma@eit.uni-kl.de 5812027Sjungma@eit.uni-kl.detemplate <class T> 5912027Sjungma@eit.uni-kl.devoid arrayParamIn(Checkpoint *cp, const std::string §ion, 6012027Sjungma@eit.uni-kl.de const std::string &name, T *param, int size); 6112027Sjungma@eit.uni-kl.de 6212027Sjungma@eit.uni-kl.devoid 6312027Sjungma@eit.uni-kl.deobjParamIn(Checkpoint *cp, const std::string §ion, 6412027Sjungma@eit.uni-kl.de const std::string &name, Serializable * ¶m); 6512027Sjungma@eit.uni-kl.de 6612027Sjungma@eit.uni-kl.de 6712027Sjungma@eit.uni-kl.de// 6812027Sjungma@eit.uni-kl.de// These macros are streamlined to use in serialize/unserialize 6912027Sjungma@eit.uni-kl.de// functions. It's assumed that serialize() has a parameter 'os' for 7012027Sjungma@eit.uni-kl.de// the ostream, and unserialize() has parameters 'cp' and 'section'. 7112027Sjungma@eit.uni-kl.de#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 7212027Sjungma@eit.uni-kl.de 7312027Sjungma@eit.uni-kl.de#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 7412027Sjungma@eit.uni-kl.de 7512027Sjungma@eit.uni-kl.de// ENUMs are like SCALARs, but we cast them to ints on the way out 7612027Sjungma@eit.uni-kl.de#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 7712027Sjungma@eit.uni-kl.de 7812027Sjungma@eit.uni-kl.de#define UNSERIALIZE_ENUM(scalar) \ 7912027Sjungma@eit.uni-kl.de do { \ 8012027Sjungma@eit.uni-kl.de int tmp; \ 8112027Sjungma@eit.uni-kl.de paramIn(cp, section, #scalar, tmp); \ 8212027Sjungma@eit.uni-kl.de scalar = (typeof(scalar))tmp; \ 8312027Sjungma@eit.uni-kl.de } while (0) 8412027Sjungma@eit.uni-kl.de 8512027Sjungma@eit.uni-kl.de#define SERIALIZE_ARRAY(member, size) \ 8612027Sjungma@eit.uni-kl.de arrayParamOut(os, #member, member, size) 8712027Sjungma@eit.uni-kl.de 8812027Sjungma@eit.uni-kl.de#define UNSERIALIZE_ARRAY(member, size) \ 8912027Sjungma@eit.uni-kl.de arrayParamIn(cp, section, #member, member, size) 9012027Sjungma@eit.uni-kl.de 9112027Sjungma@eit.uni-kl.de#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 9212027Sjungma@eit.uni-kl.de 9312027Sjungma@eit.uni-kl.de#define UNSERIALIZE_OBJPTR(objptr) \ 9412027Sjungma@eit.uni-kl.de do { \ 9512027Sjungma@eit.uni-kl.de Serializable *sptr; \ 9612027Sjungma@eit.uni-kl.de objParamIn(cp, section, #objptr, sptr); \ 9712027Sjungma@eit.uni-kl.de objptr = dynamic_cast<typeof(objptr)>(sptr); \ 9812027Sjungma@eit.uni-kl.de } while (0) 9912027Sjungma@eit.uni-kl.de 10012027Sjungma@eit.uni-kl.de/* 10112027Sjungma@eit.uni-kl.de * Basic support for object serialization. 10212027Sjungma@eit.uni-kl.de */ 10312027Sjungma@eit.uni-kl.declass Serializable 10412027Sjungma@eit.uni-kl.de{ 10512027Sjungma@eit.uni-kl.de protected: 10612027Sjungma@eit.uni-kl.de void nameOut(std::ostream &os); 10712027Sjungma@eit.uni-kl.de void nameOut(std::ostream &os, const std::string &_name); 10812027Sjungma@eit.uni-kl.de 10912027Sjungma@eit.uni-kl.de public: 11012027Sjungma@eit.uni-kl.de Serializable() {} 11112027Sjungma@eit.uni-kl.de virtual ~Serializable() {} 11212027Sjungma@eit.uni-kl.de 11312027Sjungma@eit.uni-kl.de // manditory virtual function, so objects must provide names 11412027Sjungma@eit.uni-kl.de virtual const std::string name() const = 0; 11512027Sjungma@eit.uni-kl.de 11612027Sjungma@eit.uni-kl.de virtual void serialize(std::ostream &os) {} 11712027Sjungma@eit.uni-kl.de virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 11812027Sjungma@eit.uni-kl.de 11912027Sjungma@eit.uni-kl.de static Serializable *create(Checkpoint *cp, 12012027Sjungma@eit.uni-kl.de const std::string §ion); 12112027Sjungma@eit.uni-kl.de 12212027Sjungma@eit.uni-kl.de static void serializeAll(); 12312027Sjungma@eit.uni-kl.de static void unserializeGlobals(Checkpoint *cp); 12412027Sjungma@eit.uni-kl.de}; 12512027Sjungma@eit.uni-kl.de 12612027Sjungma@eit.uni-kl.de// 12712027Sjungma@eit.uni-kl.de// A SerializableBuilder serves as an evaluation context for a set of 12812027Sjungma@eit.uni-kl.de// parameters that describe a specific instance of a Serializable. This 12912027Sjungma@eit.uni-kl.de// evaluation context corresponds to a section in the .ini file (as 13012027Sjungma@eit.uni-kl.de// with the base ParamContext) plus an optional node in the 13112027Sjungma@eit.uni-kl.de// configuration hierarchy (the configNode member) for resolving 13212027Sjungma@eit.uni-kl.de// Serializable references. SerializableBuilder is an abstract superclass; 13312027Sjungma@eit.uni-kl.de// derived classes specialize the class for particular subclasses of 13412027Sjungma@eit.uni-kl.de// Serializable (e.g., BaseCache). 13512027Sjungma@eit.uni-kl.de// 13612027Sjungma@eit.uni-kl.de// For typical usage, see the definition of 13712027Sjungma@eit.uni-kl.de// SerializableClass::createObject(). 13812027Sjungma@eit.uni-kl.de// 13912027Sjungma@eit.uni-kl.declass SerializableBuilder 14012027Sjungma@eit.uni-kl.de{ 14112027Sjungma@eit.uni-kl.de public: 14212027Sjungma@eit.uni-kl.de 143 SerializableBuilder() {} 144 145 virtual ~SerializableBuilder() {} 146 147 // Create the actual Serializable corresponding to the parameter 148 // values in this context. This function is overridden in derived 149 // classes to call a specific constructor for a particular 150 // subclass of Serializable. 151 virtual Serializable *create() = 0; 152}; 153 154// 155// An instance of SerializableClass corresponds to a class derived from 156// Serializable. The SerializableClass instance serves to bind the string 157// name (found in the config file) to a function that creates an 158// instance of the appropriate derived class. 159// 160// This would be much cleaner in Smalltalk or Objective-C, where types 161// are first-class objects themselves. 162// 163class SerializableClass 164{ 165 public: 166 167 // Type CreateFunc is a pointer to a function that creates a new 168 // simulation object builder based on a .ini-file parameter 169 // section (specified by the first string argument), a unique name 170 // for the object (specified by the second string argument), and 171 // an optional config hierarchy node (specified by the third 172 // argument). A pointer to the new SerializableBuilder is returned. 173 typedef Serializable *(*CreateFunc)(Checkpoint *cp, 174 const std::string §ion); 175 176 static std::map<std::string,CreateFunc> *classMap; 177 178 // Constructor. For example: 179 // 180 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 181 // newBaseCacheSerializableBuilder); 182 // 183 SerializableClass(const std::string &className, CreateFunc createFunc); 184 185 // create Serializable given name of class and pointer to 186 // configuration hierarchy node 187 static Serializable *createObject(Checkpoint *cp, 188 const std::string §ion); 189}; 190 191// 192// Macros to encapsulate the magic of declaring & defining 193// SerializableBuilder and SerializableClass objects 194// 195 196#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 197SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 198 OBJ_CLASS::createForUnserialize); 199 200class Checkpoint 201{ 202 private: 203 204 IniFile *db; 205 const std::string basePath; 206 const ConfigNode *configNode; 207 std::map<std::string, Serializable*> objMap; 208 209 public: 210 Checkpoint(const std::string &cpt_dir, const std::string &path, 211 const ConfigNode *_configNode); 212 213 const std::string cptDir; 214 215 bool find(const std::string §ion, const std::string &entry, 216 std::string &value); 217 218 bool findObj(const std::string §ion, const std::string &entry, 219 Serializable *&value); 220 221 bool sectionExists(const std::string §ion); 222 223 // The following static functions have to do with checkpoint 224 // creation rather than restoration. This class makes a handy 225 // namespace for them though. 226 227 // Export current checkpoint directory name so other objects can 228 // derive filenames from it (e.g., memory). The return value is 229 // guaranteed to end in '/' so filenames can be directly appended. 230 // This function is only valid while a checkpoint is being created. 231 static std::string dir(); 232 233 // Filename for base checkpoint file within directory. 234 static const char *baseFilename; 235 236 // Set up a checkpoint creation event or series of events. 237 static void setup(Tick when, Tick period = 0); 238}; 239 240#endif // __SERIALIZE_HH__ 241