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 #if VXL_HAS_INT_64 && !VXL_INT_64_IS_LONG
00222 
00223 //: Write a block of signed longs to a vsl_b_ostream
00224 // This function is very speed efficient, but
00225 // temporarily allocates a block of memory the about 1.2 times
00226 // size of the block being read.
00227 VCL_DEFINE_SPECIALIZATION
00228 inline void vsl_block_binary_write(vsl_b_ostream &os, const vxl_int_64* begin, vcl_size_t nelems)
00229 {
00230   vsl_block_binary_write_int_impl(os, begin, nelems);
00231 }
00232 
00233 //: Read a block of signed longs from a vsl_b_istream
00234 // This function is very speed efficient, but
00235 // temporarily allocates a block of memory the about 1.2 times
00236 // size of the block being read.
00237 VCL_DEFINE_SPECIALIZATION
00238 inline void vsl_block_binary_read(vsl_b_istream &is, vxl_int_64* begin, vcl_size_t nelems)
00239 {
00240   vsl_block_binary_read_int_impl(is, begin, nelems);
00241 }
00242 
00243 
00244 /////////////////////////////////////////////////////////////////////////
00245 
00246 //: Write a block of unsigned longs to a vsl_b_ostream
00247 // This function is very speed efficient, but
00248 // temporarily allocates a block of memory the about 1.2 times
00249 // size of the block being read.
00250 VCL_DEFINE_SPECIALIZATION
00251 inline void vsl_block_binary_write(vsl_b_ostream &os, const vxl_uint_64* begin, vcl_size_t nelems)
00252 {
00253   vsl_block_binary_write_int_impl(os, begin, nelems);
00254 }
00255 
00256 //: Read a block of unsigned longs from a vsl_b_istream
00257 // This function is very speed efficient, but
00258 // temporarily allocates a block of memory the about 1.2 times
00259 // size of the block being read.
00260 VCL_DEFINE_SPECIALIZATION
00261 inline void vsl_block_binary_read(vsl_b_istream &is, vxl_uint_64* begin, vcl_size_t nelems)
00262 {
00263   vsl_block_binary_read_int_impl(is, begin, nelems);
00264 }
00265 #endif //VXL_HAS_INT_64 && !VXL_INT_64_IS_LONG
00266 
00267 /////////////////////////////////////////////////////////////////////////
00268 
00269 // Internal implementation
00270 template <class T>
00271   void vsl_block_binary_write_byte_impl(vsl_b_ostream &os, const T* begin, vcl_size_t nelems);
00272 
00273 // Internal implementation
00274 template <class T>
00275   void vsl_block_binary_read_byte_impl(vsl_b_istream &is, T* begin, vcl_size_t nelems);
00276 
00277   /////////////////////////////////////////////////////////////////////////
00278 
00279 //: Write a block of unsigned chars to a vsl_b_ostream
00280 // This function is very speed and space efficient.
00281 VCL_DEFINE_SPECIALIZATION
00282 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned char* begin, vcl_size_t nelems)
00283 {
00284   vsl_block_binary_write_byte_impl(os, begin, nelems);
00285 }
00286 
00287 //: Read a block of unsigned chars from a vsl_b_istream
00288 // This function is very speed and space efficient.
00289 VCL_DEFINE_SPECIALIZATION
00290 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned char* begin, vcl_size_t nelems)
00291 {
00292   vsl_block_binary_read_byte_impl(is, begin, nelems);
00293 }
00294 
00295 /////////////////////////////////////////////////////////////////////////
00296 
00297 //: Write a block of signed chars to a vsl_b_ostream
00298 // This function is very speed and space efficient.
00299 VCL_DEFINE_SPECIALIZATION
00300 inline void vsl_block_binary_write(vsl_b_ostream &os, const signed char* begin, vcl_size_t nelems)
00301 {
00302   vsl_block_binary_write_byte_impl(os, begin, nelems);
00303 }
00304 
00305 //: Read a block of signed chars from a vsl_b_istream
00306 // This function is very speed and space efficient.
00307 VCL_DEFINE_SPECIALIZATION
00308 inline void vsl_block_binary_read(vsl_b_istream &is, signed char* begin, vcl_size_t nelems)
00309 {
00310   vsl_block_binary_read_byte_impl(is, begin, nelems);
00311 }
00312 
00313 /////////////////////////////////////////////////////////////////////////
00314 
00315 
00316 //: Write a block of values to a vsl_b_ostream
00317 // If you want to output a block of fundamental data types very efficiently,
00318 // then just #include <vsl_binary_explicit_io.h>
00319 template <class T>
00320 inline void vsl_block_binary_write(vsl_b_ostream &os, const T* begin, vcl_size_t nelems)
00321 {
00322   vsl_b_write(os, false); // Error check that this is a specialised version
00323   while (nelems--)
00324     vsl_b_write(os, *(begin++));
00325 }
00326 
00327 //: Read a block of values from a vsl_b_istream
00328 // If you want to output a block of fundamental data types very efficiently,
00329 // then just #include <vsl_binary_explicit_io.h>
00330 template <class T>
00331 inline void vsl_block_binary_read(vsl_b_istream &is, T* begin, vcl_size_t nelems)
00332 {
00333   vsl_block_binary_read_confirm_specialisation(is, false);
00334   if (!is) return;
00335   while (nelems--)
00336     vsl_b_read(is, *(begin++));
00337 }
00338 
00339 #endif // vsl_block_binary_io_h_