serialize.hh revision 395
112838Sgabeblack@google.com/* 212838Sgabeblack@google.com * Copyright (c) 2003 The Regents of The University of Michigan 312838Sgabeblack@google.com * All rights reserved. 412838Sgabeblack@google.com * 512838Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 612838Sgabeblack@google.com * modification, are permitted provided that the following conditions are 712838Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 812838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 912838Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1012838Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1112838Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1212838Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1312838Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1412838Sgabeblack@google.com * this software without specific prior written permission. 1512838Sgabeblack@google.com * 1612838Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712838Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812838Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912838Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012838Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112838Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212838Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312838Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412838Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512838Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612838Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712838Sgabeblack@google.com */ 2812838Sgabeblack@google.com 2912838Sgabeblack@google.com/* @file 3012838Sgabeblack@google.com * Serialization Interface Declarations 3112952Sgabeblack@google.com */ 3212953Sgabeblack@google.com 3312960Sgabeblack@google.com#ifndef __SERIALIZE_HH__ 3412838Sgabeblack@google.com#define __SERIALIZE_HH__ 3512838Sgabeblack@google.com 3612838Sgabeblack@google.com 3712838Sgabeblack@google.com#include <list> 3812838Sgabeblack@google.com#include <iostream> 3912838Sgabeblack@google.com#include <map> 4012838Sgabeblack@google.com 4112838Sgabeblack@google.com#include "sim/host.hh" 4212952Sgabeblack@google.com#include "sim/configfile.hh" 4312838Sgabeblack@google.com 4412838Sgabeblack@google.comclass Serializable; 4512838Sgabeblack@google.comclass Checkpoint; 4612838Sgabeblack@google.com 4712838Sgabeblack@google.comtemplate <class T> 4812952Sgabeblack@google.comvoid paramOut(std::ostream &os, const std::string &name, const T& param); 4912838Sgabeblack@google.com 5012838Sgabeblack@google.comtemplate <class T> 5112952Sgabeblack@google.comvoid paramIn(Checkpoint *cp, const std::string §ion, 5212952Sgabeblack@google.com const std::string &name, T& param); 5312952Sgabeblack@google.com 5412838Sgabeblack@google.comtemplate <class T> 5512838Sgabeblack@google.comvoid arrayParamOut(std::ostream &os, const std::string &name, 5612939Sgabeblack@google.com const T *param, int size); 5712939Sgabeblack@google.com 5812939Sgabeblack@google.comtemplate <class T> 5912939Sgabeblack@google.comvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 6012939Sgabeblack@google.com const std::string &name, T *param, int size); 6112939Sgabeblack@google.com 6212939Sgabeblack@google.comvoid 6312939Sgabeblack@google.comobjParamIn(Checkpoint *cp, const std::string §ion, 6412939Sgabeblack@google.com const std::string &name, Serializable * ¶m); 6512939Sgabeblack@google.com 6612939Sgabeblack@google.com 6712939Sgabeblack@google.com// 6812939Sgabeblack@google.com// These macros are streamlined to use in serialize/unserialize 6912939Sgabeblack@google.com// functions. It's assumed that serialize() has a parameter 'os' for 7012939Sgabeblack@google.com// the ostream, and unserialize() has parameters 'cp' and 'section'. 7112939Sgabeblack@google.com#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 7212939Sgabeblack@google.com 7312939Sgabeblack@google.com#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 7412939Sgabeblack@google.com 7512939Sgabeblack@google.com// ENUMs are like SCALARs, but we cast them to ints on the way out 7612939Sgabeblack@google.com#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 7712939Sgabeblack@google.com 7812939Sgabeblack@google.com#define UNSERIALIZE_ENUM(scalar) \ 7912952Sgabeblack@google.com do { \ 8012952Sgabeblack@google.com int tmp; \ 8112952Sgabeblack@google.com paramIn(cp, section, #scalar, tmp); \ 8212952Sgabeblack@google.com scalar = (typeof(scalar))tmp; \ 8312838Sgabeblack@google.com } while (0) 8412952Sgabeblack@google.com 8512952Sgabeblack@google.com#define SERIALIZE_ARRAY(member, size) \ 8612838Sgabeblack@google.com arrayParamOut(os, #member, member, size) 8712838Sgabeblack@google.com 8812952Sgabeblack@google.com#define UNSERIALIZE_ARRAY(member, size) \ 8912952Sgabeblack@google.com arrayParamIn(cp, section, #member, member, size) 9012838Sgabeblack@google.com 9112952Sgabeblack@google.com#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 9212952Sgabeblack@google.com 9312838Sgabeblack@google.com#define UNSERIALIZE_OBJPTR(objptr) \ 9412838Sgabeblack@google.com do { \ 9512838Sgabeblack@google.com Serializable *sptr; \ 9612838Sgabeblack@google.com objParamIn(cp, section, #objptr, sptr); \ 9712952Sgabeblack@google.com objptr = dynamic_cast<typeof(objptr)>(sptr); \ 9812952Sgabeblack@google.com } while (0) 9912838Sgabeblack@google.com 10012838Sgabeblack@google.com/* 10112838Sgabeblack@google.com * Basic support for object serialization. 10212838Sgabeblack@google.com */ 10312838Sgabeblack@google.comclass Serializable 10412838Sgabeblack@google.com{ 10512952Sgabeblack@google.com protected: 10612838Sgabeblack@google.com void nameOut(std::ostream& os); 10712838Sgabeblack@google.com void nameOut(std::ostream& os, const std::string &_name); 10812838Sgabeblack@google.com 10912838Sgabeblack@google.com public: 11012952Sgabeblack@google.com Serializable() {} 11112838Sgabeblack@google.com virtual ~Serializable() {} 11212952Sgabeblack@google.com 11312952Sgabeblack@google.com // manditory virtual function, so objects must provide names 11412952Sgabeblack@google.com virtual std::string name() const = 0; 11512952Sgabeblack@google.com 11612952Sgabeblack@google.com virtual void serialize(std::ostream& os) {} 11712838Sgabeblack@google.com virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 11812838Sgabeblack@google.com 11912838Sgabeblack@google.com static Serializable *create(Checkpoint *cp, 12012838Sgabeblack@google.com const std::string §ion); 12112952Sgabeblack@google.com 12212838Sgabeblack@google.com static void serializeAll(); 12312952Sgabeblack@google.com static void unserializeGlobals(Checkpoint *cp); 12412952Sgabeblack@google.com}; 12512838Sgabeblack@google.com 12612838Sgabeblack@google.com// 12712838Sgabeblack@google.com// A SerializableBuilder serves as an evaluation context for a set of 12812952Sgabeblack@google.com// parameters that describe a specific instance of a Serializable. This 12912838Sgabeblack@google.com// evaluation context corresponds to a section in the .ini file (as 13012952Sgabeblack@google.com// with the base ParamContext) plus an optional node in the 13112838Sgabeblack@google.com// configuration hierarchy (the configNode member) for resolving 13212838Sgabeblack@google.com// Serializable references. SerializableBuilder is an abstract superclass; 13312838Sgabeblack@google.com// derived classes specialize the class for particular subclasses of 13412952Sgabeblack@google.com// Serializable (e.g., BaseCache). 13512838Sgabeblack@google.com// 13612952Sgabeblack@google.com// For typical usage, see the definition of 13712838Sgabeblack@google.com// SerializableClass::createObject(). 13812838Sgabeblack@google.com// 13912952Sgabeblack@google.comclass SerializableBuilder 14012952Sgabeblack@google.com{ 14112838Sgabeblack@google.com public: 14212952Sgabeblack@google.com 14312952Sgabeblack@google.com SerializableBuilder() {} 14412952Sgabeblack@google.com 14512838Sgabeblack@google.com virtual ~SerializableBuilder() {} 14612838Sgabeblack@google.com 14712838Sgabeblack@google.com // Create the actual Serializable corresponding to the parameter 14812838Sgabeblack@google.com // values in this context. This function is overridden in derived 14912838Sgabeblack@google.com // classes to call a specific constructor for a particular 15012838Sgabeblack@google.com // subclass of Serializable. 15112952Sgabeblack@google.com virtual Serializable *create() = 0; 15212838Sgabeblack@google.com}; 15312838Sgabeblack@google.com 15412838Sgabeblack@google.com// 15512838Sgabeblack@google.com// An instance of SerializableClass corresponds to a class derived from 15612838Sgabeblack@google.com// Serializable. The SerializableClass instance serves to bind the string 15712952Sgabeblack@google.com// name (found in the config file) to a function that creates an 15812838Sgabeblack@google.com// instance of the appropriate derived class. 15912838Sgabeblack@google.com// 16012838Sgabeblack@google.com// This would be much cleaner in Smalltalk or Objective-C, where types 16112838Sgabeblack@google.com// are first-class objects themselves. 16212838Sgabeblack@google.com// 16312952Sgabeblack@google.comclass SerializableClass 16412952Sgabeblack@google.com{ 16512838Sgabeblack@google.com public: 16612838Sgabeblack@google.com 16712838Sgabeblack@google.com // Type CreateFunc is a pointer to a function that creates a new 16812838Sgabeblack@google.com // simulation object builder based on a .ini-file parameter 16912838Sgabeblack@google.com // section (specified by the first string argument), a unique name 17012952Sgabeblack@google.com // for the object (specified by the second string argument), and 17112952Sgabeblack@google.com // an optional config hierarchy node (specified by the third 17212838Sgabeblack@google.com // argument). A pointer to the new SerializableBuilder is returned. 17312838Sgabeblack@google.com typedef Serializable *(*CreateFunc)(Checkpoint *cp, 17412838Sgabeblack@google.com const std::string §ion); 17512838Sgabeblack@google.com 17612838Sgabeblack@google.com static std::map<std::string,CreateFunc> *classMap; 17712952Sgabeblack@google.com 17812838Sgabeblack@google.com // Constructor. For example: 17912838Sgabeblack@google.com // 18012838Sgabeblack@google.com // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 18112838Sgabeblack@google.com // newBaseCacheSerializableBuilder); 18212838Sgabeblack@google.com // 18312952Sgabeblack@google.com SerializableClass(const std::string &className, CreateFunc createFunc); 18412838Sgabeblack@google.com 18512838Sgabeblack@google.com // create Serializable given name of class and pointer to 18612838Sgabeblack@google.com // configuration hierarchy node 18712838Sgabeblack@google.com static Serializable *createObject(Checkpoint *cp, 18812838Sgabeblack@google.com const std::string §ion); 18912952Sgabeblack@google.com}; 19012838Sgabeblack@google.com 19112838Sgabeblack@google.com// 19212838Sgabeblack@google.com// Macros to encapsulate the magic of declaring & defining 19312838Sgabeblack@google.com// SerializableBuilder and SerializableClass objects 19412838Sgabeblack@google.com// 19512952Sgabeblack@google.com 19612838Sgabeblack@google.com#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 19712838Sgabeblack@google.comSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 19812838Sgabeblack@google.com OBJ_CLASS::createForUnserialize); 19912838Sgabeblack@google.com 20012838Sgabeblack@google.comclass Checkpoint 20112952Sgabeblack@google.com{ 20212952Sgabeblack@google.com private: 20312838Sgabeblack@google.com 20412838Sgabeblack@google.com IniFile *db; 20512838Sgabeblack@google.com const std::string basePath; 20612838Sgabeblack@google.com const ConfigNode *configNode; 20712838Sgabeblack@google.com std::map<std::string, Serializable*> objMap; 20812838Sgabeblack@google.com 20912952Sgabeblack@google.com public: 21012952Sgabeblack@google.com Checkpoint(const std::string &filename, const std::string &path, 21112952Sgabeblack@google.com const ConfigNode *_configNode); 21212838Sgabeblack@google.com 21312838Sgabeblack@google.com bool find(const std::string §ion, const std::string &entry, 21412838Sgabeblack@google.com std::string &value); 21512838Sgabeblack@google.com 21612838Sgabeblack@google.com bool findObj(const std::string §ion, const std::string &entry, 21712952Sgabeblack@google.com Serializable *&value); 21812952Sgabeblack@google.com 21912952Sgabeblack@google.com bool sectionExists(const std::string §ion); 22012838Sgabeblack@google.com}; 22112838Sgabeblack@google.com 22212838Sgabeblack@google.com 22312838Sgabeblack@google.com// 22412838Sgabeblack@google.com// Export checkpoint filename param so other objects can derive 22512952Sgabeblack@google.com// filenames from it (e.g., memory). 22612952Sgabeblack@google.com// 22712952Sgabeblack@google.comstd::string CheckpointDir(); 22812838Sgabeblack@google.comvoid SetupCheckpoint(Tick when, Tick period = 0); 22912838Sgabeblack@google.com 23012838Sgabeblack@google.com#endif // __SERIALIZE_HH__ 23112838Sgabeblack@google.com