serialize.hh revision 217
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. 272665Ssaidi@eecs.umich.edu */ 282665Ssaidi@eecs.umich.edu 292623SN/A/* @file 302623SN/A * Serialization Interface Declarations 312623SN/A */ 322623SN/A 332623SN/A#ifndef __SERIALIZE_HH__ 342623SN/A#define __SERIALIZE_HH__ 356973Stjones1@inf.ed.ac.uk 365529Snate@binkert.org 375529Snate@binkert.org#include <list> 382623SN/A#include <iostream> 392623SN/A#include <map> 402623SN/A 412623SN/A#include "sim/host.hh" 425529Snate@binkert.org#include "sim/configfile.hh" 432623SN/A 442623SN/Aclass IniFile; 452623SN/A 462623SN/Atemplate <class T> 472623SN/Avoid paramOut(std::ostream &os, const std::string &name, const T& param); 482839Sktlim@umich.edu 492798Sktlim@umich.edutemplate <class T> 502623SN/Avoid paramIn(IniFile &db, const std::string §ion, 512623SN/A const std::string &name, T& param); 525728Sgblack@eecs.umich.edu 535728Sgblack@eecs.umich.edutemplate <class T> 545728Sgblack@eecs.umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 555728Sgblack@eecs.umich.edu const T *param, int size); 565728Sgblack@eecs.umich.edu 575728Sgblack@eecs.umich.edutemplate <class T> 585728Sgblack@eecs.umich.eduvoid arrayParamIn(IniFile &db, const std::string §ion, 595728Sgblack@eecs.umich.edu const std::string &name, T *param, int size); 605728Sgblack@eecs.umich.edu 615728Sgblack@eecs.umich.edu// 625728Sgblack@eecs.umich.edu// These macros are streamlined to use in serialize/unserialize 635728Sgblack@eecs.umich.edu// functions. It's assumed that serialize() has a parameter 'os' for 645728Sgblack@eecs.umich.edu// the ostream, and unserialize() has parameters 'db' and 'section'. 655728Sgblack@eecs.umich.edu#define SERIALIZE_MEMBER(member) paramOut(os, #member, member) 665728Sgblack@eecs.umich.edu 675728Sgblack@eecs.umich.edu#define UNSERIALIZE_MEMBER(member) paramIn(db, section, #member, member) 685728Sgblack@eecs.umich.edu 695728Sgblack@eecs.umich.edu#define SERIALIZE_ARRAY(member, size) \ 705728Sgblack@eecs.umich.edu arrayParamOut(os, #member, member, size) 715728Sgblack@eecs.umich.edu 725728Sgblack@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 735728Sgblack@eecs.umich.edu arrayParamIn(db, section, #member, member, size) 745728Sgblack@eecs.umich.edu 755728Sgblack@eecs.umich.edu/* 765728Sgblack@eecs.umich.edu * Basic support for object serialization. 775728Sgblack@eecs.umich.edu */ 785728Sgblack@eecs.umich.educlass Serializeable 795728Sgblack@eecs.umich.edu{ 805728Sgblack@eecs.umich.edu public: 815728Sgblack@eecs.umich.edu 825728Sgblack@eecs.umich.edu friend class Serializer; 835728Sgblack@eecs.umich.edu 845728Sgblack@eecs.umich.edu protected: 855728Sgblack@eecs.umich.edu // object name: should be unique 865728Sgblack@eecs.umich.edu std::string objName; 875728Sgblack@eecs.umich.edu 885728Sgblack@eecs.umich.edu bool serialized; 895728Sgblack@eecs.umich.edu static Serializer *serializer; 905728Sgblack@eecs.umich.edu 915728Sgblack@eecs.umich.edu void mark(); 925728Sgblack@eecs.umich.edu void nameOut(std::ostream& os); 935728Sgblack@eecs.umich.edu void nameOut(std::ostream& os, const std::string &_name); 945728Sgblack@eecs.umich.edu 955728Sgblack@eecs.umich.edu public: 965728Sgblack@eecs.umich.edu Serializeable(const std::string &n); 975728Sgblack@eecs.umich.edu virtual ~Serializeable(); 985728Sgblack@eecs.umich.edu 995894Sgblack@eecs.umich.edu void setName(const std::string &name); 1005894Sgblack@eecs.umich.edu 1015894Sgblack@eecs.umich.edu // return name 1025894Sgblack@eecs.umich.edu const std::string &name() const { return objName; } 1035894Sgblack@eecs.umich.edu 1045894Sgblack@eecs.umich.edu virtual void nameChildren() {} 1056023Snate@binkert.org virtual void serialize(std::ostream& os) {} 1066023Snate@binkert.org virtual void unserialize(IniFile &db, const std::string §ion) {} 1075894Sgblack@eecs.umich.edu}; 1085894Sgblack@eecs.umich.edu 1096023Snate@binkert.orgclass Serializer 1107944SGiacomo.Gabrielli@arm.com{ 1117945SAli.Saidi@ARM.com friend class Serializeable; 1127945SAli.Saidi@ARM.com 1137945SAli.Saidi@ARM.com protected: 1147945SAli.Saidi@ARM.com typedef std::list<Serializeable *> serlist_t; 1157944SGiacomo.Gabrielli@arm.com serlist_t objects; 1167944SGiacomo.Gabrielli@arm.com std::string file; 1176023Snate@binkert.org std::ostream *output; 1186023Snate@binkert.org std::ostream &out() const; 1195894Sgblack@eecs.umich.edu 1205894Sgblack@eecs.umich.edu public: 1215894Sgblack@eecs.umich.edu Serializer(); 1225894Sgblack@eecs.umich.edu virtual ~Serializer(); 1235894Sgblack@eecs.umich.edu 1245894Sgblack@eecs.umich.edu private: 1256973Stjones1@inf.ed.ac.uk void add_object(Serializeable *obj); 1266973Stjones1@inf.ed.ac.uk void add_objects(); 1276973Stjones1@inf.ed.ac.uk 1285894Sgblack@eecs.umich.edu public: 1295894Sgblack@eecs.umich.edu void serialize(const std::string &file); 1305894Sgblack@eecs.umich.edu const std::string &filename() const { return file; } 1315894Sgblack@eecs.umich.edu}; 1325894Sgblack@eecs.umich.edu 1335894Sgblack@eecs.umich.edu// 1345894Sgblack@eecs.umich.edu// A SerializeableBuilder serves as an evaluation context for a set of 1355744Sgblack@eecs.umich.edu// parameters that describe a specific instance of a Serializeable. This 1365728Sgblack@eecs.umich.edu// evaluation context corresponds to a section in the .ini file (as 1375728Sgblack@eecs.umich.edu// with the base ParamContext) plus an optional node in the 1385728Sgblack@eecs.umich.edu// configuration hierarchy (the configNode member) for resolving 1395728Sgblack@eecs.umich.edu// Serializeable references. SerializeableBuilder is an abstract superclass; 1408707Sandreas.hansson@arm.com// derived classes specialize the class for particular subclasses of 1418707Sandreas.hansson@arm.com// Serializeable (e.g., BaseCache). 1428707Sandreas.hansson@arm.com// 1438707Sandreas.hansson@arm.com// For typical usage, see the definition of 1448707Sandreas.hansson@arm.com// SerializeableClass::createObject(). 1458707Sandreas.hansson@arm.com// 1468707Sandreas.hansson@arm.comclass SerializeableBuilder 1472623SN/A{ 1482623SN/A public: 1492623SN/A 1508707Sandreas.hansson@arm.com SerializeableBuilder() {} 1518707Sandreas.hansson@arm.com 1522623SN/A virtual ~SerializeableBuilder() {} 1532623SN/A 1542623SN/A // Create the actual Serializeable corresponding to the parameter 1552623SN/A // values in this context. This function is overridden in derived 1568948Sandreas.hansson@arm.com // classes to call a specific constructor for a particular 1578948Sandreas.hansson@arm.com // subclass of Serializeable. 1588948Sandreas.hansson@arm.com virtual Serializeable *create() = 0; 1598975Sandreas.hansson@arm.com}; 1608948Sandreas.hansson@arm.com 1618707Sandreas.hansson@arm.com// 1622948Ssaidi@eecs.umich.edu// An instance of SerializeableClass corresponds to a class derived from 1632948Ssaidi@eecs.umich.edu// Serializeable. The SerializeableClass instance serves to bind the string 1642948Ssaidi@eecs.umich.edu// name (found in the config file) to a function that creates an 1653349Sbinkertn@umich.edu// instance of the appropriate derived class. 1662948Ssaidi@eecs.umich.edu// 1677745SAli.Saidi@ARM.com// This would be much cleaner in Smalltalk or Objective-C, where types 1682948Ssaidi@eecs.umich.edu// are first-class objects themselves. 1698707Sandreas.hansson@arm.com// 1705336Shines@cs.fsu.educlass SerializeableClass 1713349Sbinkertn@umich.edu{ 1722948Ssaidi@eecs.umich.edu public: 1732948Ssaidi@eecs.umich.edu 1747745SAli.Saidi@ARM.com // Type CreateFunc is a pointer to a function that creates a new 1752623SN/A // simulation object builder based on a .ini-file parameter 1762623SN/A // section (specified by the first string argument), a unique name 1778707Sandreas.hansson@arm.com // for the object (specified by the second string argument), and 1782623SN/A // an optional config hierarchy node (specified by the third 1792623SN/A // argument). A pointer to the new SerializeableBuilder is returned. 1802623SN/A typedef SerializeableBuilder *(*CreateFunc)(); 1818707Sandreas.hansson@arm.com 1828707Sandreas.hansson@arm.com static std::map<std::string,CreateFunc> *classMap; 1838707Sandreas.hansson@arm.com 1842623SN/A // Constructor. For example: 1852623SN/A // 1862623SN/A // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable", 1872623SN/A // newBaseCacheSerializeableBuilder); 1888975Sandreas.hansson@arm.com // 1892623SN/A SerializeableClass(const std::string &className, CreateFunc createFunc); 1902657Ssaidi@eecs.umich.edu 1912948Ssaidi@eecs.umich.edu // create Serializeable given name of class and pointer to 1922948Ssaidi@eecs.umich.edu // configuration hierarchy node 1932948Ssaidi@eecs.umich.edu static Serializeable *createObject(IniFile &configDB, 1942948Ssaidi@eecs.umich.edu const std::string &configClassName); 1952948Ssaidi@eecs.umich.edu 1962948Ssaidi@eecs.umich.edu}; 1972948Ssaidi@eecs.umich.edu 1985336Shines@cs.fsu.edu// 1992948Ssaidi@eecs.umich.edu// Macros to encapsulate the magic of declaring & defining 2002948Ssaidi@eecs.umich.edu// SerializeableBuilder and SerializeableClass objects 2012948Ssaidi@eecs.umich.edu// 2022948Ssaidi@eecs.umich.edu 2032623SN/A#define CREATE_SERIALIZEABLE(OBJ_CLASS) \ 2042623SN/AOBJ_CLASS *OBJ_CLASS##Builder::create() 2058707Sandreas.hansson@arm.com 2062623SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2072623SN/Aclass OBJ_CLASS##Builder : public SerializeableBuilder \ 2082623SN/A{ \ 2098707Sandreas.hansson@arm.com public: \ 2108707Sandreas.hansson@arm.com \ 2112623SN/A OBJ_CLASS##Builder() {} \ 2122623SN/A virtual ~OBJ_CLASS##Builder() {} \ 2132623SN/A \ 2142623SN/A OBJ_CLASS *create(); \ 2158975Sandreas.hansson@arm.com}; \ 2162623SN/A \ 2172657Ssaidi@eecs.umich.edu \ 2182948Ssaidi@eecs.umich.eduSerializeableBuilder * \ 2192948Ssaidi@eecs.umich.edunew##OBJ_CLASS##Builder() \ 2202948Ssaidi@eecs.umich.edu{ \ 2212948Ssaidi@eecs.umich.edu return new OBJ_CLASS##Builder(); \ 2222948Ssaidi@eecs.umich.edu} \ 2232948Ssaidi@eecs.umich.edu \ 2245336Shines@cs.fsu.eduSerializeableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 2252948Ssaidi@eecs.umich.edu new##OBJ_CLASS##Builder); 2262948Ssaidi@eecs.umich.edu 2272948Ssaidi@eecs.umich.edu 2282948Ssaidi@eecs.umich.edu 2292623SN/A#endif // __SERIALIZE_HH__ 2302623SN/A