Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members

stream.h

00001 /* -*- c++ -*- */
00002 /*
00003  * Expresso, a C++ Array template class library
00004  * Copyright (C) 1998,2004 Oskar Enoksson (enok@lysator.liu.se)
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA  02111-1307, USA.
00020  */
00021 #ifndef EXPRESSO_STREAM_HDR
00022 #define EXPRESSO_STREAM_HDR
00023 
00024 #include <iosfwd>
00025 
00026 namespace esso {
00027 
00028   template<int K>
00029   class DoStream {
00030   public:
00031     template<int N, class Type>
00032     static void DumpArray(std::ostream &stream, 
00033                           Array<N,Type> &src, 
00034                           const Ix<N> &len) {
00035       for(IXTYPE i=0; i<len[K-1]; i++) {
00036         DoStream<K-1>::DumpArray(stream,src,len);
00037         src.Step(K-1);
00038       }
00039       src.Step(K-1,-len[K-1]);
00040     }
00041     template<int N, class Type>
00042     static void ReadArray(std::istream &stream, 
00043                           Array<N,Type> &dst, 
00044                           const Ix<N> &len) {
00045       for(IXTYPE i=0; i<len[K-1]; i++) {
00046         DoStream<K-1>::ReadArray(stream,dst,len);
00047         dst.Step(K-1);
00048       }
00049       dst.Step(K-1,-len[K-1]);
00050     }
00051     template<int N, class Type>
00052     static void DumpArrayBin(std::ostream &stream, 
00053                              Array<N,Type> &src, 
00054                              const Ix<N> &len) {
00055       for(IXTYPE i=0; i<len[K-1]; i++) {
00056         DoStream<K-1>::DumpArrayBin(stream,src,len);
00057         src.Step(K-1);
00058       }
00059       src.Step(K-1,-len[K-1]);
00060     }
00061     template<int N, class Type>
00062     static void ReadArrayBin(std::istream &stream, 
00063                              Array<N,Type> &dst, 
00064                              const Ix<N> &len) {
00065       for(IXTYPE i=0; i<len[K-1]; i++) {
00066         DoStream<K-1>::ReadArrayBin(stream,dst,len);
00067         dst.Step(K-1);
00068       }
00069       dst.Step(K-1,-len[K-1]);
00070     }
00071   };
00072   template<>
00073   class DoStream<1> {
00074   public:
00075     template<int N, class Type>
00076     static void DumpArray(std::ostream &stream, 
00077                           Array<N,Type> &src, 
00078                           const Ix<N> &len) {
00079       for(IXTYPE i=0; i<len[0]; i++)
00080         stream<<src.CGet(i)<<' ';
00081       stream<<std::endl;
00082     }
00083     template<int N, class Type>
00084     static void ReadArray(std::istream &stream, 
00085                           Array<N,Type> &src, 
00086                           const Ix<N> &len) {
00087       for(IXTYPE i=0; i<len[0]; i++)
00088         stream>>src.Get(i);
00089     }
00090     template<int N, class Type>
00091     static void DumpArrayBin(std::ostream &stream, 
00092                              Array<N,Type> &src, 
00093                              const Ix<N> &len) {
00094       for(IXTYPE i=0; i<len[0]; i++)
00095         stream.write(reinterpret_cast<const char *>(&src.Get(i)),
00096                      sizeof(typename Array<N,Type>::T)/sizeof(char));
00097     }
00098     template<int N, class Type>
00099     static void ReadArrayBin(std::istream &stream, 
00100                              Array<N,Type> &src, 
00101                              const Ix<N> &len) {
00102       for(IXTYPE i=0; i<len[0]; i++)
00103         stream.read(reinterpret_cast<char *>(&src.Get(i)),
00104                     sizeof(typename Array<N,Type>::T)/sizeof(char));
00105     }
00106   };
00107 
00109 
00110 
00119   template<int N, class Type>
00120   std::ostream & WriteUfmt(std::ostream &stream, const Array<N,Type> &src) {
00121     Ix<N> len;
00122     typename Array<N,Type>::IteratorType srcit(src);
00123     BeginAssign(srcit,len);
00124     DoStream<N>::DumpArrayBin(stream,srcit,len);
00125     return stream;
00126   }
00136   template<int N, class Type>
00137   std::ostream & WriteFmt(std::ostream &stream, const Array<N,Type> &src) {
00138     Ix<N> len;
00139     typename Array<N,Type>::IteratorType srcit(src);
00140     BeginAssign(srcit,len);
00141     DoStream<N>::DumpArray(stream,srcit,len);
00142     return stream;
00143   }
00153   template<int N, class Type>
00154   std::istream & ReadUfmt(std::istream &stream, const Array<N,Type> &dst) {
00155     Ix<N> len;
00156     
00157     typename Array<N,Type>::IteratorType dstit(dst);
00158     BeginAssign(dstit,len);
00159     DoStream<N>::ReadArrayBin(stream,dstit,len);
00160     return stream;
00161   }
00171   template<int N, class Type>
00172   std::istream & ReadFmt(std::istream &stream, const Array<N,Type> &dst) {
00173     Ix<N> len;
00174     
00175     typename Array<N,Type>::IteratorType dstit(dst);
00176     BeginAssign(dstit,len);
00177     DoStream<N>::ReadArray(stream,dstit,len);
00178     return stream;
00179   }
00181   template<int N, class T>
00182   std::istream & ReadFmt(std::istream &stream, Ix<N,T> &dst) {
00183     for(int n=0; n<N; n++)
00184       stream >>dst[n];
00185     return stream;
00186   }
00188   template<int N, class T>
00189   std::istream & ReadUfmt(std::istream &stream, Ix<N,T> &dst) {
00190     stream.read(reinterpret_cast<char *>(&dst),sizeof(dst));
00191     return stream;
00192   }
00194   template<int N, class T>
00195   std::ostream & WriteUfmt(std::ostream &stream, const Ix<N,T> &src) {
00196     for(int n=0; n<N; n++)
00197       stream <<src[n]<<' ';
00198     return stream;
00199   }
00201   template<int N, class T>
00202   std::ostream & WriteFmt(std::ostream &stream, const Ix<N,T> &src) {
00203     stream.write(reinterpret_cast<const char *>(&src),sizeof(src));
00204     return stream;
00205   }
00226   template<int N, class Type>
00227   std::ostream & operator <<(std::ostream &stream, const Array<N,Type> &src) {
00228     stream<<N<<std::endl;
00229     for(int n=0; n<N; n++) 
00230       stream<<src.Len(n)<<' '<<src.L(n)<<endl;
00231     return WriteFmt(stream,src);
00232   }
00246   template<int N, class Type>
00247   std::istream & operator >>(std::istream &stream, const Array<N,Type> &dst) {
00248     do {
00249       int n;
00250       stream>>n;
00251       if(n!=N)
00252         break;
00253 
00254       Ix<N> len,l;
00255       for(n=0; n<N; n++) {
00256         stream>>len[n];
00257         if(len[n] != dst.L(n)-dst.U(n))
00258           break;
00259         stream>>l[n];
00260         if(l[n] != dst.L(n))
00261           break;
00262       }
00263       if(n!=N)
00264         break;
00265       return ReadFmt(stream,dst);
00266     } while(0);
00267 
00268     // If we get here we failed
00269     stream.setstate(std::istream::failbit);
00270     return stream;
00271   }
00281   template<int N, class Type>
00282   bool Clone(std::istream &stream, Array<N,Type> &dst) {
00283     int n;
00284     stream>>n;
00285     if(n!=N)
00286       return false;
00287     
00288     Ix<N> len,l;
00289     for(n=0; n<N; n++) {
00290       stream>>len[n];
00291       stream>>l[n];
00292     }
00293     if(stream.fail())
00294       return false;
00295     if(!dst.Realloc(len,l))
00296       return false;
00297     return !ReadFmt(stream,dst).fail();
00298   }
00299 }
00300 
00301 #endif

Generated on Wed May 12 13:34:34 2004 for Expresso by doxygen 1.3.3