serialize.hh revision 304
1/* 2 * Copyright (c) 2003 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 Serializeable; 45class Checkpoint; 46 47template <class T> 48void paramOut(std::ostream &os, const std::string &name, const T& param); 49 50template <class T> 51void paramIn(Checkpoint *cp, const std::string §ion, 52 const std::string &name, T& param); 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, Serializeable * ¶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 Serializeable *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 Serializeable 104{ 105 public: 106 107 friend class Serializer; 108 109 protected: 110 bool serialized; 111 static Serializer *serializer; 112 113 void mark(); 114 void nameOut(std::ostream& os); 115 void nameOut(std::ostream& os, const std::string &_name); 116 117 public: 118 Serializeable(); 119 virtual ~Serializeable(); 120 121 // manditory virtual function, so objects must provide names 122 virtual std::string name() const = 0; 123 124 virtual void serialize(std::ostream& os) {} 125 virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 126 127 static Serializeable *create(Checkpoint *cp, 128 const std::string §ion); 129}; 130 131class Serializer 132{ 133 friend class Serializeable; 134 135 protected: 136 typedef std::list<Serializeable *> serlist_t; 137 serlist_t objects; 138 std::string file; 139 std::ostream *output; 140 std::ostream &out() const; 141 142 public: 143 Serializer(); 144 virtual ~Serializer(); 145 146 private: 147 void add_object(Serializeable *obj); 148 void add_objects(); 149 150 public: 151 void serialize(); 152 const std::string &filename() const { return file; } 153}; 154 155// 156// A SerializeableBuilder serves as an evaluation context for a set of 157// parameters that describe a specific instance of a Serializeable. This 158// evaluation context corresponds to a section in the .ini file (as 159// with the base ParamContext) plus an optional node in the 160// configuration hierarchy (the configNode member) for resolving 161// Serializeable references. SerializeableBuilder is an abstract superclass; 162// derived classes specialize the class for particular subclasses of 163// Serializeable (e.g., BaseCache). 164// 165// For typical usage, see the definition of 166// SerializeableClass::createObject(). 167// 168class SerializeableBuilder 169{ 170 public: 171 172 SerializeableBuilder() {} 173 174 virtual ~SerializeableBuilder() {} 175 176 // Create the actual Serializeable corresponding to the parameter 177 // values in this context. This function is overridden in derived 178 // classes to call a specific constructor for a particular 179 // subclass of Serializeable. 180 virtual Serializeable *create() = 0; 181}; 182 183// 184// An instance of SerializeableClass corresponds to a class derived from 185// Serializeable. The SerializeableClass instance serves to bind the string 186// name (found in the config file) to a function that creates an 187// instance of the appropriate derived class. 188// 189// This would be much cleaner in Smalltalk or Objective-C, where types 190// are first-class objects themselves. 191// 192class SerializeableClass 193{ 194 public: 195 196 // Type CreateFunc is a pointer to a function that creates a new 197 // simulation object builder based on a .ini-file parameter 198 // section (specified by the first string argument), a unique name 199 // for the object (specified by the second string argument), and 200 // an optional config hierarchy node (specified by the third 201 // argument). A pointer to the new SerializeableBuilder is returned. 202 typedef Serializeable *(*CreateFunc)(Checkpoint *cp, 203 const std::string §ion); 204 205 static std::map<std::string,CreateFunc> *classMap; 206 207 // Constructor. For example: 208 // 209 // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable", 210 // newBaseCacheSerializeableBuilder); 211 // 212 SerializeableClass(const std::string &className, CreateFunc createFunc); 213 214 // create Serializeable given name of class and pointer to 215 // configuration hierarchy node 216 static Serializeable *createObject(Checkpoint *cp, 217 const std::string §ion); 218}; 219 220// 221// Macros to encapsulate the magic of declaring & defining 222// SerializeableBuilder and SerializeableClass objects 223// 224 225#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 226SerializeableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 227 OBJ_CLASS::createForUnserialize); 228 229class Checkpoint 230{ 231 private: 232 233 IniFile *db; 234 const std::string basePath; 235 const ConfigNode *configNode; 236 std::map<std::string, Serializeable*> objMap; 237 238 public: 239 Checkpoint(const std::string &filename, const std::string &path, 240 const ConfigNode *_configNode); 241 242 bool find(const std::string §ion, const std::string &entry, 243 std::string &value); 244 245 bool findObj(const std::string §ion, const std::string &entry, 246 Serializeable *&value); 247 248 bool sectionExists(const std::string §ion); 249}; 250 251 252// 253// Export checkpoint filename param so other objects can derive 254// filenames from it (e.g., memory). 255// 256std::string CheckpointFile(); 257void SetupCheckpoint(Tick when, Tick period = 0); 258 259#endif // __SERIALIZE_HH__ 260