TF++, Miscellaneous classes and modules in C++:
blitzfortranio.h
Go to the documentation of this file.
1 
35 // include guard
36 #ifndef TF_BLITZFORTRANIO_H_VERSION
37 
38 #define TF_BLITZFORTRANIO_H_VERSION \
39  "TF_BLITZFORTRANIO_H V1.0 "
40 
41 #include<fstream>
42 #include<blitz/array.h>
43 #include<tfxx/fortranio.h>
44 #include<tfxx/misc.h>
45 #include<tfxx/error.h>
46 
47 namespace tfxx {
48 namespace fortranio {
49 
61 template<class T, int N>
62 struct blitz_magic {
63 }; // blitz_magic
64 
66 template<> struct blitz_magic<float, 1> { static const char magic[]="ZZf1"; };
67 #ifndef DOXYGEN_MUST_SKIP_THIS
68 template<> struct blitz_magic<float, 2> { static const char magic[]="ZZf2"; };
69 template<> struct blitz_magic<float, 3> { static const char magic[]="ZZf3"; };
70 template<> struct blitz_magic<float, 4> { static const char magic[]="ZZf4"; };
71 template<> struct blitz_magic<float, 5> { static const char magic[]="ZZf5"; };
72 template<> struct blitz_magic<double, 1> { static const char magic[]="ZZd1"; };
73 template<> struct blitz_magic<double, 2> { static const char magic[]="ZZd2"; };
74 template<> struct blitz_magic<double, 3> { static const char magic[]="ZZd3"; };
75 template<> struct blitz_magic<double, 4> { static const char magic[]="ZZd4"; };
76 template<> struct blitz_magic<double, 5> { static const char magic[]="ZZd5"; };
77 template<> struct blitz_magic<int, 1> { static const char magic[]="ZZi1"; };
78 template<> struct blitz_magic<int, 2> { static const char magic[]="ZZi2"; };
79 template<> struct blitz_magic<int, 3> { static const char magic[]="ZZi3"; };
80 template<> struct blitz_magic<int, 4> { static const char magic[]="ZZi4"; };
81 template<> struct blitz_magic<int, 5> { static const char magic[]="ZZi5"; };
82 #ifdef TF_COMPLEX_ARRAY
83 #include<complex>
84 template<> struct blitz_magic<std::complex<float>, 1>
85 { static const char magic[]="ZZc1"; };
86 template<> struct blitz_magic<std::complex<float>, 2>
87 { static const char magic[]="ZZc2"; };
88 template<> struct blitz_magic<std::complex<float>, 3>
89 { static const char magic[]="ZZc3"; };
90 template<> struct blitz_magic<std::complex<float>, 4>
91 { static const char magic[]="ZZc4"; };
92 template<> struct blitz_magic<std::complex<float>, 5>
93 { static const char magic[]="ZZc5"; };
94 template<> struct blitz_magic<std::complex<double>, 1>
95 { static const char magic[]="ZZz1"; };
96 template<> struct blitz_magic<std::complex<double>, 2>
97 { static const char magic[]="ZZz2"; };
98 template<> struct blitz_magic<std::complex<double>, 3>
99 { static const char magic[]="ZZz3"; };
100 template<> struct blitz_magic<std::complex<double>, 4>
101 { static const char magic[]="ZZz4"; };
102 template<> struct blitz_magic<std::complex<double>, 5>
103 { static const char magic[]="ZZz5"; };
104 #endif
105 #endif
106 
107 /*----------------------------------------------------------------------*/
108 
118 template<class T, int N>
121  const blitz::Array<T, N>& a)
122 {
124  fo.put(N);
125  fo.end_block();
126  for (int i=0; i<N; i++) fo.put(a.lbound(i));
127  fo.end_block();
128  for (int i=0; i<N; i++) fo.put(a.extent(i));
129  fo.end_block();
130  for (blitz::Array<T, N>::const_iterator i=a.begin(); i!=a.end(); ++i)
131  { fo.put(*i); }
132  fo.end_block();
133  return(fo);
134 }
135 
136 /*----------------------------------------------------------------------*/
137 
147 template<class T, int N>
150  blitz::Array<T, N>& a)
151 {
153  "ERROR (blitz array operator>>): input has no matching magic number!");
154  int n;
155  fi.get(n);
156  TFXX_assert((N==n), "ERROR (blitz array operator>>): illegal rank!");
157  blitz::TinyVector<int, N> lbound, extent;
158  for (int i=0; i<N; i++) fi.get(lbound[i]);
159  for (int i=0; i<N; i++) fi.get(extent[i]);
160  blitz::Array<T, N> result(lbound, extent, blitz::fortranArray);
161  for (blitz::Array<T, N>::iterator i=result.begin(); i!=result.end(); ++i)
162  { fi.get(*i); }
163  a.reference(result);
164  return(fi);
165 }
166 
167 
168 } // namespace fortranio
169 } // namespace tfxx
170 
171 #endif // TF_BLITZFORTRANIO_H_VERSION (includeguard)
172 
173 /* ----- END OF blitzfortranio.h ----- */
write FORTRAN binary data ,This class writes FORTRAN binary data. FORTRAN binary data is always writt...
Definition: fortranio.h:200
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
magic numbers for blitz array I/OPlease define TF_COMPLEX_ARRAY to activate I/O for arrays of complex...
int magic(const char *const cmagic)
Create a magic number from a character string. ,If represents the input character sequence cmagic an...
Definition: ioswap.cc:59
void end_block()
finish data block
Definition: fortranio.cc:155
STL namespace.
tfxx::fortranio::FortranBinOutput & operator<<(tfxx::fortranio::FortranBinOutput &fo, const blitz::Array< T, N > &a)
Output operator template for class FortranBinOutput and blitz.
tfxx::fortranio::FortranBinInput & operator>>(tfxx::fortranio::FortranBinInput &fi, blitz::Array< T, N > &a)
Input operator template for class FortranBinInput and blitz.
read FORTRAN binary data ,This class reads FORTRAN binary data. FORTRAN binary data is always written...
Definition: fortranio.h:137
void write_magic(const char *cmagic)
write magic number (to separate block)
Definition: fortranio.cc:184
bool match_magic(const char *cmagic)
expect magic number and adjust swapping flag
Definition: fortranio.cc:137
void put(const T &value)
put a value to the output
Definition: fortranio.h:253
Namespace containing all code of library libtfxx.