MPQC  3.0.0-alpha
stateout.h
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 #include <string>
32 #include <map>
33 #include <vector>
34 
35 #include <util/class/class.h>
36 #include <util/state/state.h>
37 
38 namespace sc {
39 
51  template <typename T> void ToStateOut(const T& t, StateOut& so, int& count);
52 
53 class StateOutData {
54  public:
55  int num;
56  int size;
57  int type;
58  int offset;
59 
60  StateOutData(): num(0), size(0), type(0), offset(0) {}
61 };
62 
71 class StateOut: public DescribedClass {
72  friend class SavableState;
73  friend class TranslateDataOut;
74  private:
75  // do not allow copy constructor or assignment
76  StateOut(const StateOut&);
77  void operator=(const StateOut&);
78  int have_cd_;
79  protected:
80  int dir_loc_loc_;
81  TranslateDataOut *translate_;
82  int copy_references_;
83  int next_object_number_;
84  std::map<Ref<SavableState>,StateOutData> ps_;
85  std::map<ClassDescP,int> classidmap_;
86  int nextclassid_;
87  int node_to_node_;
88  virtual int put_array_void(const void*,int);
89  virtual int putparents(const ClassDesc*);
90 
91  void put_directory();
92 
93  // The following members are called by friend SavableState
94 
95  void have_classdesc() { have_cd_ = 1; }
96  int need_classdesc() { int tmp = have_cd_; have_cd_ = 0; return !tmp; }
97 
102  virtual int putobject(const Ref<SavableState> &);
103 
105  virtual int put(const ClassDesc*);
106  public:
107  StateOut();
108  virtual ~StateOut();
109 
111  virtual void put_header();
112 
115  virtual int putstring(const char*);
116 
119 
120  virtual int put(const std::string &);
121  virtual int put(char r);
122  virtual int put(unsigned int r);
123  virtual int put(int r);
124  virtual int put(unsigned long r);
125  virtual int put(long r);
126  virtual int put(bool r);
127  virtual int put(float r);
128  virtual int put(double r);
129 
131 
135  virtual int put(const char*,int);
136  virtual int put(const unsigned int*,int);
137  virtual int put(const int*,int);
138  virtual int put(const unsigned long*,int);
139  virtual int put(const long*,int);
140  virtual int put(const float*,int);
141  virtual int put(const double*,int);
143 
147  virtual int put_array_char(const char*p,int size);
148  virtual int put_array_uint(const unsigned int*p,int size);
149  virtual int put_array_int(const int*p,int size);
150  virtual int put_array_ulong(const unsigned long*p,int size);
151  virtual int put_array_long(const long*p,int size);
152  virtual int put_array_float(const float*p,int size);
153  virtual int put_array_double(const double*p,int size);
155 
161  template <template <typename, typename> class Container, class T, class A>
163  int put(const Container<T,A> &v) {
164  const size_t l = v.size();
165  int r = put(l);
166  for (typename Container<T,A>::const_iterator i=v.begin(); i!=v.end(); ++i)
167  ToStateOut(*i,*this,r);
168  return r;
169  }
170 
172  template <class T, class A>
173  int put(const std::vector<T,A> &v) {
174  const size_t l = v.size();
175  int r = put(l);
176  for (typename std::vector<T,A>::const_iterator i=v.begin(); i!=v.end(); ++i)
177  ToStateOut(*i,*this,r);
178  return r;
179  }
180 
182  template <typename Key, typename Compare, typename Alloc>
183  int put(const std::set<Key,Compare,Alloc> &s) {
184  const size_t l = s.size();
185  int r = put(l);
186  for (typename std::set<Key,Compare,Alloc>::const_iterator i=s.begin(); i!=s.end(); ++i)
187  ToStateOut(*i,*this,r);
188  return r;
189  }
190 
192  template <typename Key, typename Value>
193  int put(const std::map<Key,Value>& map) {
194  typedef std::map<Key,Value> Map;
195  const size_t size = map.size();
196  int r = put(size);
197  if (size) {
198  typedef typename Map::const_iterator citer;
199  const citer end = map.end();
200  for(citer i=map.begin(); i!=end; ++i) {
201  r += put(*i);
202  }
203  }
204  return r;
205  }
206 
208  template <typename L, typename R>
209  int put(const std::pair<L,R>& v) {
210  int s = 0;
211  ToStateOut(v.first,*this,s);
212  ToStateOut(v.second,*this,s);
213  return s;
214  }
216 
220  void forget_references();
225  void copy_references();
226 
228  virtual int use_directory();
229 
231  virtual void flush();
232 
237  int node_to_node() const { return node_to_node_; }
238 
241  virtual int tell();
244  virtual void seek(int loc);
247  virtual int seekable();
248  };
249 
253  template <typename T> void ToStateOut(const T& t, StateOut& so, int& count){
254  count += so.put(t);
255  }
256 
258  template <typename T> void ToStateOut(const Ref<T>& t, StateOut& so, int& count) {
260  }
262 
263 } // namespace sc
264 
265 #endif
266 
267 // Local Variables:
268 // mode: c++
269 // c-file-style: "CLJ"
270 // End:
sc::StateOut::copy_references
void copy_references()
If a reference to an object that has already been written is encountered, copy it instead of generati...
sc::StateOut::put
int put(const std::map< Key, Value > &map)
Write an std::map. This also works if Key or Value is a Ref to a SavableState.
Definition: stateout.h:193
sc::ToStateOut
void ToStateOut(const Atom &a, StateOut &so, int &count)
writes Atom to sc::StateOut
sc::StateOut::use_directory
virtual int use_directory()
Returns true if this object uses a directory.
sc::StateOutData
Definition: stateout.h:53
sc::Ref
A template class that maintains references counts.
Definition: ref.h:361
sc::StateOut::seekable
virtual int seekable()
Return non-zero if tell and seek do anything sensible.
sc::StateOut::putobject
virtual int putobject(const Ref< SavableState > &)
This will prepare StateOut to output a pointer to data.
sc::Ref::pointer
T * pointer() const
Returns a pointer the reference counted object.
Definition: ref.h:413
sc::StateOut::tell
virtual int tell()
Returns the current position in the file.
sc::StateOut::flush
virtual void flush()
Flush out any remaining data.
sc::ClassDesc
This class is used to contain information about classes.
Definition: class.h:147
sc::StateOut::put
int put(const std::pair< L, R > &v)
Write an std::pair.
Definition: stateout.h:209
sc::StateOut::seek
virtual void seek(int loc)
Set the current position in the file.
sc::map
std::vector< int > map(const GaussianBasisSet &B, const GaussianBasisSet &A)
same as operator<<, except A does not have to be contained in B, map[a] = -1 if function a is not in ...
sc::StateOut::put
int put(const std::vector< T, A > &v)
"Specialization" of the above put() to std::vector.
Definition: stateout.h:173
sc::TranslateDataOut
Convert data to other formats.
Definition: translate.h:162
sc::StateOut
Definition: stateout.h:71
sc::SavableState::save_state
void save_state(StateOut &)
Save the state of the object as specified by the StateOut object.
sc::DescribedClass
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:233
sc::StateOut::forget_references
void forget_references()
Don't keep track of pointers to objects.
sc::StateOut::put
int put(const Container< T, A > &v)
Write a Container that could be a standard (non-associative) C++ container such as std::vector or std...
Definition: stateout.h:163
sc::StateOut::put
virtual int put(const ClassDesc *)
Write out information about the given ClassDesc.
sc::StateOut::put_header
virtual void put_header()
Write out header information.
sc::SavableState
Base class for objects that can save/restore state.
Definition: state.h:45
sc::StateOut::putstring
virtual int putstring(const char *)
This is like put except the length of the char array is determined by interpreting the character arra...
sc::StateOut::node_to_node
int node_to_node() const
True if this is a node to node save/restore.
Definition: stateout.h:237
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
sc::StateOut::put
int put(const std::set< Key, Compare, Alloc > &s)
Write an std::set. This also works if Key or Value is a Ref to a SavableState.
Definition: stateout.h:183

Generated at Sun Jan 26 2020 23:24:02 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.