| [0b990d] | 1 | //
 | 
|---|
 | 2 | // statein.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_statein_h
 | 
|---|
 | 29 | #define _util_state_statein_h
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #ifdef __GNUC__
 | 
|---|
 | 32 | #pragma interface
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include <string>
 | 
|---|
 | 36 | #include <vector>
 | 
|---|
 | 37 | #include <map>
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include <util/state/state.h>
 | 
|---|
 | 40 | #include <util/keyval/keyval.h>
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | namespace sc {
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | class StateInData {
 | 
|---|
 | 45 |   public:
 | 
|---|
 | 46 |     Ref<SavableState> ptr;
 | 
|---|
 | 47 |     int size;
 | 
|---|
 | 48 |     int type;
 | 
|---|
 | 49 |     int offset;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |     StateInData(): size(0), type(0), offset(0) {}
 | 
|---|
 | 52 | };
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | class StateClassData {
 | 
|---|
 | 55 |   public:
 | 
|---|
 | 56 |     int version;
 | 
|---|
 | 57 |     char *name;
 | 
|---|
 | 58 |     const ClassDesc *classdesc;
 | 
|---|
 | 59 |     int ninstance;
 | 
|---|
 | 60 |   public:
 | 
|---|
 | 61 |     StateClassData(int v=-1, const ClassDesc *c=0, char *name=0):
 | 
|---|
 | 62 |       version(v), name(name), classdesc(c), ninstance(0) {}
 | 
|---|
 | 63 |     StateClassData(const StateClassData &d) { operator=(d); }
 | 
|---|
 | 64 |     ~StateClassData();
 | 
|---|
 | 65 |     StateClassData &operator=(const StateClassData &d);
 | 
|---|
 | 66 | };
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | /** Restores objects that derive from SavableState.
 | 
|---|
 | 69 |  */
 | 
|---|
 | 70 | class StateIn:  public DescribedClass {
 | 
|---|
 | 71 |     friend class SavableState;
 | 
|---|
 | 72 |     friend class TranslateDataIn;
 | 
|---|
 | 73 |   private:
 | 
|---|
 | 74 |     // do not allow copy constructor or assignment
 | 
|---|
 | 75 |     StateIn(const StateIn&);
 | 
|---|
 | 76 |     void operator=(const StateIn&);
 | 
|---|
 | 77 |     int have_cd_;
 | 
|---|
 | 78 |     int dir_loc_;
 | 
|---|
 | 79 |     char key_[KeyVal::MaxKeywordLength];
 | 
|---|
 | 80 |     int keylength_;
 | 
|---|
 | 81 |   protected:
 | 
|---|
 | 82 |     Ref<KeyVal> override_;
 | 
|---|
 | 83 |     TranslateDataIn *translate_;
 | 
|---|
 | 84 |     std::map<int,StateInData> ps_;
 | 
|---|
 | 85 |     int expected_object_num_;
 | 
|---|
 | 86 |     std::map<ClassDescP,int> classidmap_;
 | 
|---|
 | 87 |     std::map<int,StateClassData> classdatamap_;
 | 
|---|
 | 88 |     int nextclassid_;
 | 
|---|
 | 89 |     int node_to_node_;
 | 
|---|
 | 90 |     int version_;
 | 
|---|
 | 91 |     int date_;
 | 
|---|
 | 92 |     char userid_[9];
 | 
|---|
 | 93 |     char format_;
 | 
|---|
 | 94 |     virtual int get_array_void(void*,int);
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |     int push_key(const char *key);
 | 
|---|
 | 97 |     void pop_key(int n) { key_[n] = '\0'; keylength_ = n; }
 | 
|---|
 | 98 |     const char *key() { return key_; }
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |     void get_directory();
 | 
|---|
 | 101 |     int directory_location() const { return dir_loc_; }
 | 
|---|
 | 102 |     void find_and_get_directory();
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |     // The following members are called by friend SavableState
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |     /** This is used to restore an object.  It is called with the
 | 
|---|
 | 107 |         reference to the reference being restored.  If the data being
 | 
|---|
 | 108 |         restored has previously been restored, then the pointer
 | 
|---|
 | 109 |         being restored is set to a reference to the previously
 | 
|---|
 | 110 |         restored object. */
 | 
|---|
 | 111 |     virtual int getobject(Ref<SavableState> &);
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |     /// This restores objects that are listed in the directory.
 | 
|---|
 | 114 |     virtual int dir_getobject(Ref<SavableState> &, const char *name);
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |     /** When storage has been allocated during object restoration,
 | 
|---|
 | 117 |         this routine is called with the object reference number
 | 
|---|
 | 118 |         and the pointer to the new storage so getpointer
 | 
|---|
 | 119 |         can find the data if it is referenced again. */
 | 
|---|
 | 120 |     virtual void haveobject(int,const Ref<SavableState> &);
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |     /** A call to nextobject followed by havepointer(int) is equiv
 | 
|---|
 | 123 |         to havepointer(int,void**); */
 | 
|---|
 | 124 |     virtual void nextobject(int);
 | 
|---|
 | 125 |     virtual void haveobject(const Ref<SavableState> &);
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |     void have_classdesc() { have_cd_ = 1; }
 | 
|---|
 | 128 |     int need_classdesc() { int tmp = have_cd_; have_cd_ = 0; return !tmp; }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 |     /** This restores ClassDesc's.  It will set the
 | 
|---|
 | 131 |         pointer to the address of the static ClassDesc for
 | 
|---|
 | 132 |         the class which has the same name as the class that had
 | 
|---|
 | 133 |         the ClassDesc that was saved by put(const ClassDesc*). */
 | 
|---|
 | 134 |     virtual int get(const ClassDesc**);
 | 
|---|
 | 135 |   public:
 | 
|---|
 | 136 |     StateIn();
 | 
|---|
 | 137 |     virtual ~StateIn();
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |     /** Read in the header information.  Changes the translation
 | 
|---|
 | 140 |         scheme if necessary. */
 | 
|---|
 | 141 |     virtual void get_header();
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |     /** Returns the version of the ClassDesc in the persistent object
 | 
|---|
 | 144 |         or -1 if info on the ClassDesc doesn't exist. */
 | 
|---|
 | 145 |     virtual int version(const ClassDesc*);
 | 
|---|
 | 146 |     
 | 
|---|
 | 147 |     /// This restores strings saved with StateOut::putstring.
 | 
|---|
 | 148 |     virtual int getstring(char*&);
 | 
|---|
 | 149 |     
 | 
|---|
 | 150 |     /// This restores a std::string object.
 | 
|---|
 | 151 |     virtual int get(std::string&);
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |     /// These restore data saved with StateOut's put.  members.
 | 
|---|
 | 154 |     virtual int get(char&r, const char *keyword = 0);
 | 
|---|
 | 155 |     virtual int get(unsigned int&r, const char *keyword = 0);
 | 
|---|
 | 156 |     virtual int get(int&r, const char *keyword = 0);
 | 
|---|
 | 157 |     virtual int get(bool&r, const char *keyword = 0);
 | 
|---|
 | 158 |     virtual int get(float&r, const char *keyword = 0);
 | 
|---|
 | 159 |     virtual int get(double&r, const char *keyword = 0);
 | 
|---|
 | 160 |     /** These restore data saved with StateOut's put.
 | 
|---|
 | 161 |         members.  The data is allocated by StateIn. */
 | 
|---|
 | 162 |     virtual int get(char*&);
 | 
|---|
 | 163 |     virtual int get(unsigned int*&);
 | 
|---|
 | 164 |     virtual int get(int*&);
 | 
|---|
 | 165 |     virtual int get(float*&);
 | 
|---|
 | 166 |     virtual int get(double*&);
 | 
|---|
 | 167 |     /** These restore data saved with StateOut's put.
 | 
|---|
 | 168 |         members.  The data must be preallocated by the user. */
 | 
|---|
 | 169 |     virtual int get_array_char(char*p,int size);
 | 
|---|
 | 170 |     virtual int get_array_uint(unsigned int*p,int size);
 | 
|---|
 | 171 |     virtual int get_array_int(int*p,int size);
 | 
|---|
 | 172 |     virtual int get_array_float(float*p,int size);
 | 
|---|
 | 173 |     virtual int get_array_double(double*p,int size);
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 |     /// Read an STL vector of data.
 | 
|---|
 | 176 |     template <class T>
 | 
|---|
 | 177 |     int get(typename std::vector<T> &v) {
 | 
|---|
 | 178 |       int l;
 | 
|---|
 | 179 |       int r = get(l);
 | 
|---|
 | 180 |       if (l) { v.resize(l); for (int i=0; i<l; i++) r += get(v[i]); }
 | 
|---|
 | 181 |       return r;
 | 
|---|
 | 182 |     }
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 |     /** True if this is a node to node save/restore.  This is
 | 
|---|
 | 185 |         for classes that try to avoid saving databases
 | 
|---|
 | 186 |         to files that can otherwise be read in, but want to avoid
 | 
|---|
 | 187 |         reading the database from disk on all nodes. */
 | 
|---|
 | 188 |     int node_to_node() const { return node_to_node_; }
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |     /// Returns true of this object uses a directory.
 | 
|---|
 | 191 |     virtual int use_directory();
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |     /// Return the current position in the file.
 | 
|---|
 | 194 |     virtual int tell();
 | 
|---|
 | 195 |     /** Set the current position in the file.  The default implementation
 | 
|---|
 | 196 |         does nothing. */
 | 
|---|
 | 197 |     virtual void seek(int);
 | 
|---|
 | 198 |     /** Return non-zero if seek does anything sensible.  The
 | 
|---|
 | 199 |         default implementation returns 0. */
 | 
|---|
 | 200 |     virtual int seekable();
 | 
|---|
 | 201 |     int has_directory() const { return dir_loc_ != 0; }
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |     /** List all the objects to the stream.  Only StateIn
 | 
|---|
 | 204 |         specializations with directories can list objects. */
 | 
|---|
 | 205 |     virtual void list_objects(std::ostream& = ExEnv::out0());
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 |     /** Give this StateIn a KeyVal object
 | 
|---|
 | 208 |         that is used to override values. */
 | 
|---|
 | 209 |     void set_override(const Ref<KeyVal>&kv) { override_ = kv; }
 | 
|---|
 | 210 |     /** Return the KeyVal used to override values. */
 | 
|---|
 | 211 |     const Ref<KeyVal> &override() const { return override_; }
 | 
|---|
 | 212 |   };
 | 
|---|
 | 213 | 
 | 
|---|
 | 214 | }
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 | #endif
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 | // Local Variables:
 | 
|---|
 | 219 | // mode: c++
 | 
|---|
 | 220 | // c-file-style: "CLJ"
 | 
|---|
 | 221 | // End:
 | 
|---|