serialize.hh revision 937
1/* 2 * Copyright (c) 2002-2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* @file 30 * Serialization Interface Declarations 31 */ 32 33#ifndef __SERIALIZE_HH__ 34#define __SERIALIZE_HH__ 35 36 37#include <list> 38#include <iostream> 39#include <map> 40 41#include "sim/host.hh" 42#include "sim/configfile.hh" 43 44class Serializable; 45class Checkpoint; 46 47template <class T> 48void paramOut(std::ostream &os, const std::string &name, const T ¶m); 49 50template <class T> 51void paramIn(Checkpoint *cp, const std::string §ion, 52 const std::string &name, T ¶m); 53 54template <class T> 55void arrayParamOut(std::ostream &os, const std::string &name, 56 const T *param, int size); 57 58template <class T> 59void arrayParamIn(Checkpoint *cp, const std::string §ion, 60 const std::string &name, T *param, int size); 61 62void 63objParamIn(Checkpoint *cp, const std::string §ion, 64 const std::string &name, Serializable * ¶m); 65 66 67// 68// These macros are streamlined to use in serialize/unserialize 69// functions. It's assumed that serialize() has a parameter 'os' for 70// the ostream, and unserialize() has parameters 'cp' and 'section'. 71#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 72 73#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 74 75// ENUMs are like SCALARs, but we cast them to ints on the way out 76#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 77 78#define UNSERIALIZE_ENUM(scalar) \ 79 do { \ 80 int tmp; \ 81 paramIn(cp, section, #scalar, tmp); \ 82 scalar = (typeof(scalar))tmp; \ 83 } while (0) 84 85#define SERIALIZE_ARRAY(member, size) \ 86 arrayParamOut(os, #member, member, size) 87 88#define UNSERIALIZE_ARRAY(member, size) \ 89 arrayParamIn(cp, section, #member, member, size) 90 91#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 92 93#define UNSERIALIZE_OBJPTR(objptr) \ 94 do { \ 95 Serializable *sptr; \ 96 objParamIn(cp, section, #objptr, sptr); \ 97 objptr = dynamic_cast<typeof(objptr)>(sptr); \ 98 } while (0) 99 100/* 101 * Basic support for object serialization. 102 */ 103class Serializable 104{ 105 protected: 106 void nameOut(std::ostream &os); 107 void nameOut(std::ostream &os, const std::string &_name); 108 109 public: 110 Serializable() {} 111 virtual ~Serializable() {} 112 113 // manditory virtual function, so objects must provide names 114 virtual const std::string name() const = 0; 115 116 virtual void serialize(std::ostream &os) {} 117 virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 118 119 static Serializable *create(Checkpoint *cp, 120 const std::string §ion); 121 122 static void serializeAll(); 123 static void unserializeGlobals(Checkpoint *cp); 124}; 125 126// 127// A SerializableBuilder serves as an evaluation context for a set of 128// parameters that describe a specific instance of a Serializable. This 129// evaluation context corresponds to a section in the .ini file (as 130// with the base ParamContext) plus an optional node in the 131// configuration hierarchy (the configNode member) for resolving 132// Serializable references. SerializableBuilder is an abstract superclass; 133// derived classes specialize the class for particular subclasses of 134// Serializable (e.g., BaseCache). 135// 136// For typical usage, see the definition of 137// SerializableClass::createObject(). 138// 139class SerializableBuilder 140{ 141 public: 142 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