MPQC  3.0.0-alpha
src/lib/util/state/stateout.h
00001 //
00002 // stateout.h
00003 //
00004 // Copyright (C) 1998 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifndef _util_state_stateout_h
00029 #define _util_state_stateout_h
00030 
00031 #include <string>
00032 #include <map>
00033 
00034 #include <util/class/class.h>
00035 #include <util/state/state.h>
00036 
00037 namespace sc {
00038 
00039   namespace detail {
00041     template <typename T> struct ToStateOut;
00042   }
00043 
00044 class StateOutData {
00045   public:
00046     int num;
00047     int size;
00048     int type;
00049     int offset;
00050 
00051     StateOutData(): num(0), size(0), type(0), offset(0) {}
00052 };
00053 
00061 class StateOut: public DescribedClass {
00062     friend class SavableState;
00063     friend class TranslateDataOut;
00064   private:
00065     // do not allow copy constructor or assignment
00066     StateOut(const StateOut&);
00067     void operator=(const StateOut&);
00068     int have_cd_;
00069   protected:
00070     int dir_loc_loc_;
00071     TranslateDataOut *translate_;
00072     int copy_references_;
00073     int next_object_number_;
00074     std::map<Ref<SavableState>,StateOutData> ps_;
00075     std::map<ClassDescP,int> classidmap_;
00076     int nextclassid_;
00077     int node_to_node_;
00078     virtual int put_array_void(const void*,int);
00079     virtual int putparents(const ClassDesc*);
00080 
00081     void put_directory();
00082 
00083     // The following members are called by friend SavableState
00084 
00085     void have_classdesc() { have_cd_ = 1; }
00086     int need_classdesc() { int tmp = have_cd_; have_cd_ = 0; return !tmp; }
00087 
00092     virtual int putobject(const Ref<SavableState> &);
00093 
00095     virtual int put(const ClassDesc*);
00096   public:
00097     StateOut();
00098     virtual ~StateOut();
00099 
00101     virtual void put_header();
00102 
00105     virtual int putstring(const char*);
00106 
00109 
00110     virtual int put(const std::string &);
00111     virtual int put(char r);
00112     virtual int put(unsigned int r);
00113     virtual int put(int r);
00114     virtual int put(unsigned long r);
00115     virtual int put(long r);
00116     virtual int put(bool r);
00117     virtual int put(float r);
00118     virtual int put(double r);
00120 
00124     virtual int put(const char*,int);
00125     virtual int put(const unsigned int*,int);
00126     virtual int put(const int*,int);
00127     virtual int put(const unsigned long*,int);
00128     virtual int put(const long*,int);
00129     virtual int put(const float*,int);
00130     virtual int put(const double*,int);
00132 
00136     virtual int put_array_char(const char*p,int size);
00137     virtual int put_array_uint(const unsigned int*p,int size);
00138     virtual int put_array_int(const int*p,int size);
00139     virtual int put_array_ulong(const unsigned long*p,int size);
00140     virtual int put_array_long(const long*p,int size);
00141     virtual int put_array_float(const float*p,int size);
00142     virtual int put_array_double(const double*p,int size);
00144 
00150 
00151     template <template <typename, typename> class Container, class T, class A>
00152     int put(const Container<T,A> &v) {
00153       const size_t l = v.size();
00154       int r = put(l);
00155       for (typename Container<T,A>::const_iterator i=v.begin(); i!=v.end(); ++i)
00156         detail::ToStateOut<T>::put(*i,*this,r);
00157       return r;
00158     }
00159 
00161     template <typename Key, typename Compare, typename Alloc>
00162     int put(const std::set<Key,Compare,Alloc> &s) {
00163       const size_t l = s.size();
00164       int r = put(l);
00165       for (typename std::set<Key,Compare,Alloc>::const_iterator i=s.begin(); i!=s.end(); ++i)
00166         detail::ToStateOut<Key>::put(*i,*this,r);
00167       return r;
00168     }
00169 
00171     template <typename Key, typename Value>
00172     int put(const std::map<Key,Value>& map) {
00173       typedef std::map<Key,Value> Map;
00174       const size_t size = map.size();
00175       int r = put(size);
00176       if (size) {
00177         typedef typename Map::const_iterator citer;
00178         const citer end = map.end();
00179         for(citer i=map.begin(); i!=end; ++i) {
00180           r += put(*i);
00181         }
00182       }
00183       return r;
00184     }
00185 
00187     template <typename L, typename R>
00188     int put(const std::pair<L,R>& v) {
00189       int s = 0;
00190       detail::ToStateOut<L>::put(v.first,*this,s);
00191       detail::ToStateOut<R>::put(v.second,*this,s);
00192       return s;
00193     }
00195 
00199     void forget_references();
00204     void copy_references();
00205 
00207     virtual int use_directory();
00208 
00210     virtual void flush();
00211 
00216     int node_to_node() const { return node_to_node_; }
00217 
00220     virtual int tell();
00223     virtual void seek(int loc);
00226     virtual int seekable();
00227   };
00228 
00229   class RefSCVector;
00230   class RefSCMatrix;
00231   class RefSymmSCMatrix;
00232   class RefDiagSCMatrix;
00233 
00234   namespace detail {
00236     template <typename T> struct ToStateOut {
00237       static void put(const T& t, StateOut& so, int& count) {
00238         count += so.put(t);
00239       }
00240     };
00242     template <typename T> struct ToStateOut< sc::Ref<T> > {
00243       static void put(const Ref<T>& t, StateOut& so, int& count) {
00244         SavableState::save_state(t.pointer(),so);
00245       }
00246     };
00248     template <> struct ToStateOut<sc::RefSCVector> {
00249       static void put(const sc::RefSCVector& t, StateOut& so, int& count);
00250     };
00252     template <> struct ToStateOut<sc::RefSCMatrix> {
00253       static void put(const sc::RefSCMatrix& t, StateOut& so, int& count);
00254     };
00256     template <> struct ToStateOut<sc::RefSymmSCMatrix> {
00257       static void put(const sc::RefSymmSCMatrix& t, StateOut& so, int& count);
00258     };
00260     template <> struct ToStateOut<sc::RefDiagSCMatrix> {
00261       static void put(const sc::RefDiagSCMatrix& t, StateOut& so, int& count);
00262     };
00263   }
00264 
00265 }
00266 
00267 #endif
00268 
00269 // Local Variables:
00270 // mode: c++
00271 // c-file-style: "CLJ"
00272 // End:

Generated at Sat Jul 7 2012 11:52:49 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.0.