| [5d30c1] | 1 | // | 
|---|
|  | 2 | // stateout.h | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // Copyright (C) 1998 Limit Point Systems, Inc. | 
|---|
|  | 5 | // | 
|---|
|  | 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
|  | 7 | // Maintainer: LPS | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // This file is part of the SC Toolkit. | 
|---|
|  | 10 | // | 
|---|
|  | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
|  | 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
|  | 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 14 | // any later version. | 
|---|
|  | 15 | // | 
|---|
|  | 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
|  | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 19 | // GNU Library General Public License for more details. | 
|---|
|  | 20 | // | 
|---|
|  | 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
|  | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
|  | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
|  | 24 | // | 
|---|
|  | 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
|  | 26 | // | 
|---|
|  | 27 |  | 
|---|
|  | 28 | #ifndef _util_state_stateout_h | 
|---|
|  | 29 | #define _util_state_stateout_h | 
|---|
|  | 30 |  | 
|---|
|  | 31 | #ifdef __GNUC__ | 
|---|
|  | 32 | #pragma interface | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #include <string> | 
|---|
|  | 36 | #include <vector> | 
|---|
|  | 37 | #include <map> | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #include <util/class/class.h> | 
|---|
|  | 40 | #include <util/state/state.h> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | namespace sc { | 
|---|
|  | 43 |  | 
|---|
|  | 44 | class StateOutData { | 
|---|
|  | 45 | public: | 
|---|
|  | 46 | int num; | 
|---|
|  | 47 | int size; | 
|---|
|  | 48 | int type; | 
|---|
|  | 49 | int offset; | 
|---|
|  | 50 |  | 
|---|
|  | 51 | StateOutData(): num(0), size(0), type(0), offset(0) {} | 
|---|
|  | 52 | }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | /** Serializes objects that derive from SavableState. | 
|---|
|  | 55 |  | 
|---|
|  | 56 | StateOut keeps track | 
|---|
|  | 57 | of pointers to data so that two references to the same | 
|---|
|  | 58 | piece of data do not result in that data being sent to the | 
|---|
|  | 59 | output device two times. | 
|---|
|  | 60 | */ | 
|---|
|  | 61 | class StateOut: public DescribedClass { | 
|---|
|  | 62 | friend class SavableState; | 
|---|
|  | 63 | friend class TranslateDataOut; | 
|---|
|  | 64 | private: | 
|---|
|  | 65 | // do not allow copy constructor or assignment | 
|---|
|  | 66 | StateOut(const StateOut&); | 
|---|
|  | 67 | void operator=(const StateOut&); | 
|---|
|  | 68 | int have_cd_; | 
|---|
|  | 69 | protected: | 
|---|
|  | 70 | int dir_loc_loc_; | 
|---|
|  | 71 | TranslateDataOut *translate_; | 
|---|
|  | 72 | int copy_references_; | 
|---|
|  | 73 | int next_object_number_; | 
|---|
|  | 74 | std::map<Ref<SavableState>,StateOutData> ps_; | 
|---|
|  | 75 | std::map<ClassDescP,int> classidmap_; | 
|---|
|  | 76 | int nextclassid_; | 
|---|
|  | 77 | int node_to_node_; | 
|---|
|  | 78 | virtual int put_array_void(const void*,int); | 
|---|
|  | 79 | virtual int putparents(const ClassDesc*); | 
|---|
|  | 80 |  | 
|---|
|  | 81 | void put_directory(); | 
|---|
|  | 82 |  | 
|---|
|  | 83 | // The following members are called by friend SavableState | 
|---|
|  | 84 |  | 
|---|
|  | 85 | void have_classdesc() { have_cd_ = 1; } | 
|---|
|  | 86 | int need_classdesc() { int tmp = have_cd_; have_cd_ = 0; return !tmp; } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | /** This will prepare StateOut to output a pointer to data.  It first | 
|---|
|  | 89 | checks to see if the data has already been saved.  If it has, then | 
|---|
|  | 90 | a reference to this data is saved.  Otherwise the object is written | 
|---|
|  | 91 | out. */ | 
|---|
|  | 92 | virtual int putobject(const Ref<SavableState> &); | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /// Write out information about the given ClassDesc. | 
|---|
|  | 95 | virtual int put(const ClassDesc*); | 
|---|
|  | 96 | public: | 
|---|
|  | 97 | StateOut(); | 
|---|
|  | 98 | virtual ~StateOut(); | 
|---|
|  | 99 |  | 
|---|
|  | 100 | /// Write out header information. | 
|---|
|  | 101 | virtual void put_header(); | 
|---|
|  | 102 |  | 
|---|
|  | 103 | /** This is like put except the length of the char array is determined | 
|---|
|  | 104 | by interpreting the character array as a character string. */ | 
|---|
|  | 105 | virtual int putstring(const char*); | 
|---|
|  | 106 |  | 
|---|
|  | 107 | /// Write out a std::string object | 
|---|
|  | 108 | virtual int put(const std::string &); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | /// Write the given datum. | 
|---|
|  | 111 | virtual int put(char r); | 
|---|
|  | 112 | virtual int put(unsigned int r); | 
|---|
|  | 113 | virtual int put(int r); | 
|---|
|  | 114 | virtual int put(bool r); | 
|---|
|  | 115 | virtual int put(unsigned long r); | 
|---|
|  | 116 | virtual int put(float r); | 
|---|
|  | 117 | virtual int put(double r); | 
|---|
|  | 118 | /** Write the given array data.  Size information is also saved.  The | 
|---|
|  | 119 | data is allocated and read by the get(T*&) routines. */ | 
|---|
|  | 120 | virtual int put(const char*,int); | 
|---|
|  | 121 | virtual int put(const unsigned int*,int); | 
|---|
|  | 122 | virtual int put(const int*,int); | 
|---|
|  | 123 | virtual int put(const float*,int); | 
|---|
|  | 124 | virtual int put(const double*,int); | 
|---|
|  | 125 | /** Put arrays of data.  No size information is stored.  This | 
|---|
|  | 126 | data is read by the get_array_T routines. */ | 
|---|
|  | 127 | virtual int put_array_char(const char*p,int size); | 
|---|
|  | 128 | virtual int put_array_uint(const unsigned int*p,int size); | 
|---|
|  | 129 | virtual int put_array_int(const int*p,int size); | 
|---|
|  | 130 | virtual int put_array_float(const float*p,int size); | 
|---|
|  | 131 | virtual int put_array_double(const double*p,int size); | 
|---|
|  | 132 |  | 
|---|
|  | 133 | /// Write an STL vector of data. | 
|---|
|  | 134 | template <class T> | 
|---|
|  | 135 | int put(typename std::vector<T> &v) { | 
|---|
|  | 136 | int l = v.size(); | 
|---|
|  | 137 | int r = put(l); | 
|---|
|  | 138 | if (l) { for (int i=0; i<l; i++) r += put(v[i]); } | 
|---|
|  | 139 | return r; | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
|  | 142 | /** Don't keep track of pointers to objects.  Calling this | 
|---|
|  | 143 | causes duplicated references to objects to be copied. | 
|---|
|  | 144 | The directory will not contain the forgotten objects. */ | 
|---|
|  | 145 | void forget_references(); | 
|---|
|  | 146 | /** If a reference to an object that has already been written | 
|---|
|  | 147 | is encountered, copy it instead of generating a reference | 
|---|
|  | 148 | to the first object. | 
|---|
|  | 149 | The directory will not be updated with new objects. */ | 
|---|
|  | 150 | void copy_references(); | 
|---|
|  | 151 |  | 
|---|
|  | 152 | /// Returns true if this object uses a directory. | 
|---|
|  | 153 | virtual int use_directory(); | 
|---|
|  | 154 |  | 
|---|
|  | 155 | /// Flush out any remaining data. | 
|---|
|  | 156 | virtual void flush(); | 
|---|
|  | 157 |  | 
|---|
|  | 158 | /** True if this is a node to node save/restore.  This is | 
|---|
|  | 159 | necessary for classes that try to avoid saving databases | 
|---|
|  | 160 | to files that can otherwise be read in, but want to avoid | 
|---|
|  | 161 | reading the database from disk on all nodes. */ | 
|---|
|  | 162 | int node_to_node() const { return node_to_node_; } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | /** Returns the current position in the file.  The default | 
|---|
|  | 165 | implementation returns 0. */ | 
|---|
|  | 166 | virtual int tell(); | 
|---|
|  | 167 | /** Set the current position in the file.  The default implementation | 
|---|
|  | 168 | does nothing. */ | 
|---|
|  | 169 | virtual void seek(int loc); | 
|---|
|  | 170 | /** Return non-zero if tell and seek do anything sensible.  The | 
|---|
|  | 171 | default implementation returns 0. */ | 
|---|
|  | 172 | virtual int seekable(); | 
|---|
|  | 173 | }; | 
|---|
|  | 174 |  | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 | #endif | 
|---|
|  | 178 |  | 
|---|
|  | 179 | // Local Variables: | 
|---|
|  | 180 | // mode: c++ | 
|---|
|  | 181 | // c-file-style: "CLJ" | 
|---|
|  | 182 | // End: | 
|---|