| [0b990d] | 1 | //
 | 
|---|
 | 2 | // state.cc
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Copyright (C) 1996 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 | #ifdef __GNUC__
 | 
|---|
 | 29 | #pragma implementation
 | 
|---|
 | 30 | #endif
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | #include <stdlib.h>
 | 
|---|
 | 33 | #include <sys/time.h>
 | 
|---|
 | 34 | #include <unistd.h>
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include <util/misc/formio.h>
 | 
|---|
 | 37 | #include <util/class/class.h>
 | 
|---|
 | 38 | #include <util/state/state.h>
 | 
|---|
 | 39 | #include <util/state/stateio.h>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | using namespace std;
 | 
|---|
 | 42 | using namespace sc;
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | #define DEBUG 0
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | /////////////////////////////////////////////////////////////////
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | static ClassDesc SavableState_cd(
 | 
|---|
 | 49 |     typeid(SavableState),"SavableState",1,"public DescribedClass");
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | SavableState::SavableState()
 | 
|---|
 | 52 | {
 | 
|---|
 | 53 | }
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | SavableState::SavableState(const SavableState&)
 | 
|---|
 | 56 | {
 | 
|---|
 | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | SavableState& SavableState::operator=(const SavableState&)
 | 
|---|
 | 60 | {
 | 
|---|
 | 61 |   return *this;
 | 
|---|
 | 62 | }
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | SavableState::SavableState(StateIn&si)
 | 
|---|
 | 65 | {
 | 
|---|
 | 66 |   // In case si is looking for the next pointer, let it know i
 | 
|---|
 | 67 |   // have one.
 | 
|---|
 | 68 |   reference();
 | 
|---|
 | 69 |   Ref<SavableState> th(this);
 | 
|---|
 | 70 |   si.haveobject(th);
 | 
|---|
 | 71 |   th = 0;
 | 
|---|
 | 72 |   dereference();
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   // The following gets the version of this class and all of the
 | 
|---|
 | 75 |   // parent classes.  This is only needed for restoring objects
 | 
|---|
 | 76 |   // that were saved with save_object_state and don't necessarily
 | 
|---|
 | 77 |   // have all of their version information already restored.
 | 
|---|
 | 78 |   if (si.need_classdesc()) {
 | 
|---|
 | 79 |       const ClassDesc* tcd;
 | 
|---|
 | 80 |       si.get(&tcd);
 | 
|---|
 | 81 |     }
 | 
|---|
 | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | SavableState::~SavableState()
 | 
|---|
 | 85 | {
 | 
|---|
 | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | void
 | 
|---|
 | 89 | SavableState::save_state(StateOut&so)
 | 
|---|
 | 90 | {
 | 
|---|
 | 91 |   save_state(this,so);
 | 
|---|
 | 92 | }
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 | void
 | 
|---|
 | 95 | SavableState::save_state(SavableState*th,StateOut&so)
 | 
|---|
 | 96 | {
 | 
|---|
 | 97 |   so.putobject(th);
 | 
|---|
 | 98 | }
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | SavableState*
 | 
|---|
 | 101 | SavableState::restore_state(StateIn& si)
 | 
|---|
 | 102 | {
 | 
|---|
 | 103 |   return dir_restore_state(si,0,0);
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | SavableState*
 | 
|---|
 | 107 | SavableState::key_restore_state(StateIn& si,
 | 
|---|
 | 108 |                                 const char *keyword)
 | 
|---|
 | 109 | {
 | 
|---|
 | 110 |   return dir_restore_state(si,0,keyword);
 | 
|---|
 | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | SavableState*
 | 
|---|
 | 114 | SavableState::dir_restore_state(StateIn&si, const char *objectname,
 | 
|---|
 | 115 |                                 const char *keyword)
 | 
|---|
 | 116 | {
 | 
|---|
 | 117 |   Ref<KeyVal> old_override;
 | 
|---|
 | 118 |   Ref<SavableState> overriding_value;
 | 
|---|
 | 119 |   int p = si.push_key(keyword);
 | 
|---|
 | 120 |   const int can_override_objects = 0;
 | 
|---|
 | 121 |   if (can_override_objects && keyword && si.override().nonnull()) {
 | 
|---|
 | 122 |       overriding_value << si.override()->describedclassvalue(si.key());
 | 
|---|
 | 123 |       old_override = si.override();
 | 
|---|
 | 124 |       if (overriding_value.nonnull()) {
 | 
|---|
 | 125 |           si.set_override(0);
 | 
|---|
 | 126 |         }
 | 
|---|
 | 127 |     }
 | 
|---|
 | 128 |   // restore the pointer
 | 
|---|
 | 129 |   Ref<SavableState> ss;
 | 
|---|
 | 130 |   if (objectname) si.dir_getobject(ss, objectname);
 | 
|---|
 | 131 |   else si.getobject(ss);
 | 
|---|
 | 132 |   if (overriding_value.nonnull()) {
 | 
|---|
 | 133 |       ExEnv::out0() << indent
 | 
|---|
 | 134 |            << "overriding \"" << si.key() << "\": object of type ";
 | 
|---|
 | 135 |       if (ss.null()) ExEnv::out0() << "(null)";
 | 
|---|
 | 136 |       else ExEnv::out0() << ss->class_name();
 | 
|---|
 | 137 |       ExEnv::out0() << " -> object of type "
 | 
|---|
 | 138 |            << overriding_value->class_name()
 | 
|---|
 | 139 |            << endl;
 | 
|---|
 | 140 |       ss = overriding_value;
 | 
|---|
 | 141 |     }
 | 
|---|
 | 142 |   SavableState *ret = ss.pointer();
 | 
|---|
 | 143 |   if (ret) {
 | 
|---|
 | 144 |       ret->reference();
 | 
|---|
 | 145 |       ss = 0;
 | 
|---|
 | 146 |       ret->dereference();
 | 
|---|
 | 147 |     }
 | 
|---|
 | 148 |   if (old_override.nonnull()) {
 | 
|---|
 | 149 |       si.set_override(old_override);
 | 
|---|
 | 150 |     }
 | 
|---|
 | 151 |   si.pop_key(p);
 | 
|---|
 | 152 |   return ret;
 | 
|---|
 | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | void
 | 
|---|
 | 156 | SavableState::save_object_state(StateOut&so)
 | 
|---|
 | 157 | {
 | 
|---|
 | 158 |   save_vbase_state(so);
 | 
|---|
 | 159 |   save_data_state(so);
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | void
 | 
|---|
 | 163 | SavableState::save_vbase_state(StateOut&so)
 | 
|---|
 | 164 | {
 | 
|---|
 | 165 |   SavableState::save_data_state(so);
 | 
|---|
 | 166 | }
 | 
|---|
 | 167 | void
 | 
|---|
 | 168 | SavableState::save_data_state(StateOut& so)
 | 
|---|
 | 169 | {
 | 
|---|
 | 170 |   if (so.need_classdesc()) so.put(class_desc());
 | 
|---|
 | 171 | }
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | // Local Variables:
 | 
|---|
 | 176 | // mode: c++
 | 
|---|
 | 177 | // c-file-style: "CLJ"
 | 
|---|
 | 178 | // End:
 | 
|---|