core/vsl/vsl_block_binary.h
Go to the documentation of this file.
00001 // This is core/vsl/vsl_block_binary.h
00002 #ifndef vsl_block_binary_io_h_
00003 #define vsl_block_binary_io_h_
00004 //:
00005 // \file
00006 // \brief Set of functions to do binary IO on a block of values.
00007 // \author Ian Scott, ISBE Manchester, Feb 2003
00008 
00009 #include <vsl/vsl_binary_io.h>
00010 #include <vsl/vsl_binary_explicit_io.h>
00011 
00012 //: Error checking.
00013 void vsl_block_binary_read_confirm_specialisation(vsl_b_istream &is, bool specialised);
00014 
00015 //: Write a block of values to a vsl_b_ostream, potentially very efficiently for fundamental types.
00016 template <class T>
00017 inline void vsl_block_binary_write(vsl_b_ostream &os, const T* begin, vcl_size_t nelems);
00018 
00019 //: Read a block of values from a vsl_b_ostream, potentially very efficiently for fundamental types.
00020 template <class T>
00021 inline void vsl_block_binary_read(vsl_b_istream &is, T* begin, vcl_size_t nelems);
00022 
00023 
00024 /////////////////////////////////////////////////////////////////////////
00025 
00026 // Internal implementation
00027 template <class T>
00028   void vsl_block_binary_write_float_impl(vsl_b_ostream &os, const T* begin, vcl_size_t nelems);
00029 
00030 // Internal implementation
00031 template <class T>
00032   void vsl_block_binary_read_float_impl(vsl_b_istream &is, T* begin, vcl_size_t nelems);
00033 
00034 
00035 /////////////////////////////////////////////////////////////////////////
00036 //: Write a block of doubles to a vsl_b_ostream
00037 // This function is very speed efficient, but
00038 // temporarily allocates a block of memory the size of the
00039 // block being read.
00040 VCL_DEFINE_SPECIALIZATION
00041 inline void vsl_block_binary_write(vsl_b_ostream &os, const double* begin, vcl_size_t nelems)
00042 {
00043   vsl_block_binary_write_float_impl(os, begin, nelems);
00044 }
00045 
00046 //: Read a block of doubles from a vsl_b_istream
00047 // This function is very speed efficient.
00048 VCL_DEFINE_SPECIALIZATION
00049 inline void vsl_block_binary_read(vsl_b_istream &is, double* begin, vcl_size_t nelems)
00050 {
00051   vsl_block_binary_read_float_impl(is, begin, nelems);
00052 }
00053 
00054 /////////////////////////////////////////////////////////////////////////
00055 
00056 //: Write a block of floats to a vsl_b_ostream
00057 // This function is very speed efficient, but
00058 // temporarily allocates a block of memory the size of the
00059 // block being read.
00060 VCL_DEFINE_SPECIALIZATION
00061 inline void vsl_block_binary_write(vsl_b_ostream &os, const float* begin, vcl_size_t nelems)
00062 {
00063   vsl_block_binary_write_float_impl(os, begin, nelems);
00064 }
00065 
00066 //: Read a block of floats from a vsl_b_istream
00067 // This function is very speed efficient.
00068 VCL_DEFINE_SPECIALIZATION
00069 inline void vsl_block_binary_read(vsl_b_istream &is, float* begin, vcl_size_t nelems)
00070 {
00071   vsl_block_binary_read_float_impl(is, begin, nelems);
00072 }
00073 
00074 /////////////////////////////////////////////////////////////////////////
00075 
00076 // Internal implementation
00077 template <class T>
00078   void vsl_block_binary_write_int_impl(vsl_b_ostream &os, const T* begin, vcl_size_t nelems);
00079 
00080 // Internal implementation
00081 template <class T>
00082   void vsl_block_binary_read_int_impl(vsl_b_istream &is, T* begin, vcl_size_t nelems);
00083   
00084 /////////////////////////////////////////////////////////////////////////
00085 
00086 //: Write a block of signed ints to a vsl_b_ostream
00087 // This function is very speed efficient, but
00088 // temporarily allocates a block of memory the about 1.2 times
00089 // size of the block being read.
00090 VCL_DEFINE_SPECIALIZATION
00091 inline void vsl_block_binary_write(vsl_b_ostream &os, const int* begin, vcl_size_t nelems)
00092 {
00093   vsl_block_binary_write_int_impl(os, begin, nelems);
00094 }
00095 
00096 //: Read a block of signed ints from a vsl_b_istream
00097 // This function is very speed efficient, but
00098 // temporarily allocates a block of memory the about 1.2 times
00099 // size of the block being read.
00100 VCL_DEFINE_SPECIALIZATION
00101 inline void vsl_block_binary_read(vsl_b_istream &is, int* begin, vcl_size_t nelems)
00102 {
00103   vsl_block_binary_read_int_impl(is, begin, nelems);
00104 }
00105 
00106 /////////////////////////////////////////////////////////////////////////
00107 
00108 //: Write a block of unsigned ints to a vsl_b_ostream
00109 // This function is very speed efficient, but
00110 // temporarily allocates a block of memory the about 1.2 times
00111 // size of the block being read.
00112 VCL_DEFINE_SPECIALIZATION
00113 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned int* begin, vcl_size_t nelems)
00114 {
00115   vsl_block_binary_write_int_impl(os, begin, nelems);
00116 }
00117 
00118 //: Read a block of unsigned ints from a vsl_b_istream
00119 // This function is very speed efficient, but
00120 // temporarily allocates a block of memory the about 1.2 times
00121 // size of the block being read.
00122 VCL_DEFINE_SPECIALIZATION
00123 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned int* begin, vcl_size_t nelems)
00124 {
00125   vsl_block_binary_read_int_impl(is, begin, nelems);
00126 }
00127 
00128 
00129 /////////////////////////////////////////////////////////////////////////
00130 
00131 //: Write a block of signed shorts to a vsl_b_ostream
00132 // This function is very speed efficient, but
00133 // temporarily allocates a block of memory the about 1.2 times
00134 // size of the block being read.
00135 VCL_DEFINE_SPECIALIZATION
00136 inline void vsl_block_binary_write(vsl_b_ostream &os, const short* begin, vcl_size_t nelems)
00137 {
00138   vsl_block_binary_write_int_impl(os, begin, nelems);
00139 }
00140 
00141 //: Read a block of signed shorts from a vsl_b_istream
00142 // This function is very speed efficient, but
00143 // temporarily allocates a block of memory the about 1.2 times
00144 // size of the block being read.
00145 VCL_DEFINE_SPECIALIZATION
00146 inline void vsl_block_binary_read(vsl_b_istream &is, short* begin, vcl_size_t nelems)
00147 {
00148   vsl_block_binary_read_int_impl(is, begin, nelems);
00149 }
00150 
00151 
00152 /////////////////////////////////////////////////////////////////////////
00153 
00154 //: Write a block of unsigned shorts to a vsl_b_ostream
00155 // This function is very speed efficient, but
00156 // temporarily allocates a block of memory the about 1.2 times
00157 // size of the block being read.
00158 VCL_DEFINE_SPECIALIZATION
00159 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned short* begin, vcl_size_t nelems)
00160 {
00161   vsl_block_binary_write_int_impl(os, begin, nelems);
00162 }
00163 
00164 //: Read a block of unsigned shorts from a vsl_b_istream
00165 // This function is very speed efficient, but
00166 // temporarily allocates a block of memory the about 1.2 times
00167 // size of the block being read.
00168 VCL_DEFINE_SPECIALIZATION
00169 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned short* begin, vcl_size_t nelems)
00170 {
00171   vsl_block_binary_read_int_impl(is, begin, nelems);
00172 }
00173 
00174 
00175 /////////////////////////////////////////////////////////////////////////
00176 
00177 //: Write a block of signed longs to a vsl_b_ostream
00178 // This function is very speed efficient, but
00179 // temporarily allocates a block of memory the about 1.2 times
00180 // size of the block being read.
00181 VCL_DEFINE_SPECIALIZATION
00182 inline void vsl_block_binary_write(vsl_b_ostream &os, const long* begin, vcl_size_t nelems)
00183 {
00184   vsl_block_binary_write_int_impl(os, begin, nelems);
00185 }
00186 
00187 //: Read a block of signed longs from a vsl_b_istream
00188 // This function is very speed efficient, but
00189 // temporarily allocates a block of memory the about 1.2 times
00190 // size of the block being read.
00191 VCL_DEFINE_SPECIALIZATION
00192 inline void vsl_block_binary_read(vsl_b_istream &is, long* begin, vcl_size_t nelems)
00193 {
00194   vsl_block_binary_read_int_impl(is, begin, nelems);
00195 }
00196 
00197 
00198 /////////////////////////////////////////////////////////////////////////
00199 
00200 //: Write a block of unsigned longs to a vsl_b_ostream
00201 // This function is very speed efficient, but
00202 // temporarily allocates a block of memory the about 1.2 times
00203 // size of the block being read.
00204 VCL_DEFINE_SPECIALIZATION
00205 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned long* begin, vcl_size_t nelems)
00206 {
00207   vsl_block_binary_write_int_impl(os, begin, nelems);
00208 }
00209 
00210 //: Read a block of unsigned longs from a vsl_b_istream
00211 // This function is very speed efficient, but
00212 // temporarily allocates a block of memory the about 1.2 times
00213 // size of the block being read.
00214 VCL_DEFINE_SPECIALIZATION
00215 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned long* begin, vcl_size_t nelems)
00216 {
00217   vsl_block_binary_read_int_impl(is, begin, nelems);
00218 }
00219 
00220 /////////////////////////////////////////////////////////////////////////
00221 
00222 // Internal implementation
00223 template <class T>
00224   void vsl_block_binary_write_byte_impl(vsl_b_ostream &os, const T* begin, vcl_size_t nelems);
00225 
00226 // Internal implementation
00227 template <class T>
00228   void vsl_block_binary_read_byte_impl(vsl_b_istream &is, T* begin, vcl_size_t nelems);
00229 
00230   /////////////////////////////////////////////////////////////////////////
00231 
00232 //: Write a block of unsigned chars to a vsl_b_ostream
00233 // This function is very speed and space efficient.
00234 VCL_DEFINE_SPECIALIZATION
00235 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned char* begin, vcl_size_t nelems)
00236 {
00237   vsl_block_binary_write_byte_impl(os, begin, nelems);
00238 }
00239 
00240 //: Read a block of unsigned chars from a vsl_b_istream
00241 // This function is very speed and space efficient.
00242 VCL_DEFINE_SPECIALIZATION
00243 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned char* begin, vcl_size_t nelems)
00244 {
00245   vsl_block_binary_read_byte_impl(is, begin, nelems);
00246 }
00247 
00248 /////////////////////////////////////////////////////////////////////////
00249 
00250 //: Write a block of signed chars to a vsl_b_ostream
00251 // This function is very speed and space efficient.
00252 VCL_DEFINE_SPECIALIZATION
00253 inline void vsl_block_binary_write(vsl_b_ostream &os, const signed char* begin, vcl_size_t nelems)
00254 {
00255   vsl_block_binary_write_byte_impl(os, begin, nelems);
00256 }
00257 
00258 //: Read a block of signed chars from a vsl_b_istream
00259 // This function is very speed and space efficient.
00260 VCL_DEFINE_SPECIALIZATION
00261 inline void vsl_block_binary_read(vsl_b_istream &is, signed char* begin, vcl_size_t nelems)
00262 {
00263   vsl_block_binary_read_byte_impl(is, begin, nelems);
00264 }
00265 
00266 /////////////////////////////////////////////////////////////////////////
00267 
00268 #if 0
00269 // This test will be replaced with !VCL_PTRDIFF_T_IS_A_STANDARD_TYPE
00270 // When that macro is working.
00271 
00272 //: Write a block of vcl_ptrdiff_ts to a vsl_b_ostream
00273 // This function is very speed efficient, but
00274 // temporarily allocates a block of memory the about 1.2 times
00275 // size of the block being read.
00276 VCL_DEFINE_SPECIALIZATION
00277 inline void vsl_block_binary_write(vsl_b_ostream &os, const vcl_ptrdiff_t* begin, vcl_size_t nelems)
00278 {
00279   vsl_block_binary_write_int_impl(os, begin, nelems);
00280 }
00281 
00282 //: Read a block of vcl_ptrdiff_ts from a vsl_b_istream
00283 // This function is very speed efficient, but
00284 // temporarily allocates a block of memory the about 1.2 times
00285 // size of the block being read.
00286 VCL_DEFINE_SPECIALIZATION
00287 inline void vsl_block_binary_read(vsl_b_istream &is, vcl_ptrdiff_t* begin, vcl_size_t nelems)
00288 {
00289   vsl_block_binary_read_int_impl(is, begin, nelems);
00290 }
00291 
00292 
00293 /////////////////////////////////////////////////////////////////////////
00294 
00295 //: Write a block of vcl_size_ts to a vsl_b_ostream
00296 // This function is very speed efficient, but
00297 // temporarily allocates a block of memory the about 1.2 times
00298 // size of the block being read.
00299 VCL_DEFINE_SPECIALIZATION
00300 inline void vsl_block_binary_write(vsl_b_ostream &os, const vcl_size_t* begin, vcl_size_t nelems)
00301 {
00302   vsl_block_binary_write_int_impl(os, begin, nelems);
00303 }
00304 
00305 //: Read a block of vcl_size_ts from a vsl_b_istream
00306 // This function is very speed efficient, but
00307 // temporarily allocates a block of memory the about 1.2 times
00308 // size of the block being read.
00309 VCL_DEFINE_SPECIALIZATION
00310 inline void vsl_block_binary_read(vsl_b_istream &is, vcl_size_t* begin, vcl_size_t nelems)
00311 {
00312   vsl_block_binary_read_int_impl(is, begin, nelems);
00313 }
00314 
00315 #endif // 0
00316 
00317 
00318 //: Write a block of values to a vsl_b_ostream
00319 // If you want to output a block of fundamental data types very efficiently,
00320 // then just #include <vsl_binary_explicit_io.h>
00321 template <class T>
00322 inline void vsl_block_binary_write(vsl_b_ostream &os, const T* begin, vcl_size_t nelems)
00323 {
00324   vsl_b_write(os, false); // Error check that this is a specialised version
00325   while (nelems--)
00326     vsl_b_write(os, *(begin++));
00327 }
00328 
00329 //: Read a block of values from a vsl_b_istream
00330 // If you want to output a block of fundamental data types very efficiently,
00331 // then just #include <vsl_binary_explicit_io.h>
00332 template <class T>
00333 inline void vsl_block_binary_read(vsl_b_istream &is, T* begin, vcl_size_t nelems)
00334 {
00335   vsl_block_binary_read_confirm_specialisation(is, false);
00336   if (!is) return;
00337   while (nelems--)
00338     vsl_b_read(is, *(begin++));
00339 }
00340 
00341 #endif // vsl_block_binary_io_h_