casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ModcompConversion.h
Go to the documentation of this file.
00001 //# ModCompConversion.h: A class with static functions to convert ModComp format
00002 //# Copyright (C) 1998,1999,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ModcompConversion.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_MODCOMPCONVERSION_H
00029 #define CASA_MODCOMPCONVERSION_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Utilities/Assert.h>
00033 #include <casa/OS/CanonicalConversion.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // Define the canonical sizes of the built-in data types.
00038 // These are the same for all machine architectures.
00039 
00040 #define SIZE_MODCOMP_CHAR     1
00041 #define SIZE_MODCOMP_UCHAR    1
00042 #define SIZE_MODCOMP_SHORT    2
00043 #define SIZE_MODCOMP_USHORT   2
00044 #define SIZE_MODCOMP_INT      4
00045 #define SIZE_MODCOMP_UINT     4
00046 #define SIZE_MODCOMP_INT64    4
00047 #define SIZE_MODCOMP_UINT64   4
00048 #define SIZE_MODCOMP_FLOAT    4
00049 #define SIZE_MODCOMP_DOUBLE   8
00050 
00051 // Define for each data format if a conversion is needed from the ModComp
00052 // format to the local format (or vice-versa).  This allows for optimizations
00053 // in, for example, AipsIO.  
00054 
00055 // The ModComp format is ASCII for strings, a proprietary floating point format
00056 // and 2-complement for integers (all most significant bit first) with the
00057 // lengths as shown above.
00058 
00059 // Change the definitions below if new architectures (whith different lengths
00060 // for these integer types) are being used.
00061 #define CONVERT_MODCOMP_CHAR     0
00062 #define CONVERT_MODCOMP_UCHAR    0
00063 // Conversion is needed for little endian architectures (like DEC and Intel),
00064 // because the bytes have to be swapped (thus not for data with length 1).
00065 #if defined(AIPS_LITTLE_ENDIAN)
00066 #define CONVERT_MODCOMP_SHORT    1
00067 #define CONVERT_MODCOMP_USHORT   1
00068 #define CONVERT_MODCOMP_INT      1
00069 #define CONVERT_MODCOMP_UINT     1
00070 #define CONVERT_MODCOMP_INT64    1
00071 #define CONVERT_MODCOMP_UINT64   1
00072 #else
00073 // Conversion is not needed for integers if the local lengths for Shorts and
00074 // Ints is 2,4 respectively.
00075 #define CONVERT_MODCOMP_SHORT    0
00076 #define CONVERT_MODCOMP_USHORT   0
00077 #define CONVERT_MODCOMP_INT      0
00078 #define CONVERT_MODCOMP_UINT     0
00079 #define CONVERT_MODCOMP_INT64    1
00080 #define CONVERT_MODCOMP_UINT64   1
00081 #endif
00082 // Conversion is always needed for floating point data.
00083 #define CONVERT_MODCOMP_FLOAT    1
00084 #define CONVERT_MODCOMP_DOUBLE   1
00085 
00086 // <summary>Static functions to convert Modcomp numeric formats</summary>
00087 
00088 // <use visibility=export>
00089 
00090 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tModcompConversion" demos="">
00091 // </reviewed>
00092 
00093 // <synopsis>
00094 // This class contains static toLocal functions to convert data from Modcomp
00095 // format to local format and vice-versa. 
00096 // 
00097 // The functions work on both big-endian and little-endian machines. They
00098 // convert between Modcomp 2-byte integers and Shorts (or uShorts) and Modcomp
00099 // 4-byte integers and Ints (or uInts, Int64s or uInt64s).
00100 //
00101 // It is currently not possible to convert floating point numbers from local
00102 // format to Modcomp. Attempting to do so will throw an exception (AipsError).
00103 // </synopsis>
00104 
00105 // <motivation>
00106 // The VLA data is stored using Modcomp numeric data formats and needs to be
00107 // converted to the local format to be manipulated.
00108 // </motivation>
00109 
00110 // <todo asof="$DATE$">
00111 //  <li> Support conversion of floating point data to Modcomp format
00112 //  <li> Support data type long double.
00113 // </todo>
00114 
00115 
00116 class ModcompConversion
00117 {
00118 public:
00119   // Convert one value from Modcomp format to local format.
00120   // The from and to buffer should not overlap.
00121   // <group>
00122   static uInt toLocal(Char&   to, const void* from);
00123   static uInt toLocal(uChar&  to, const void* from);
00124   static uInt toLocal(Short&  to, const void* from);
00125   static uInt toLocal(uShort& to, const void* from);
00126   static uInt toLocal(Int&    to, const void* from);
00127   static uInt toLocal(uInt&   to, const void* from);
00128   static uInt toLocal(Int64&  to, const void* from);
00129   static uInt toLocal(uInt64& to, const void* from);
00130   static uInt toLocal(Float&  to, const void* from);
00131   static uInt toLocal(Double& to, const void* from);
00132   // </group>
00133     
00134   // Convert nr values from Modcomp format to local format.
00135   // The from and to buffer should not overlap.
00136   // <group>
00137   static uInt toLocal(Char*   to, const void* from, uInt nr);
00138   static uInt toLocal(uChar*  to, const void* from, uInt nr);
00139   static uInt toLocal(Short*  to, const void* from, uInt nr);
00140   static uInt toLocal(uShort* to, const void* from, uInt nr);
00141   static uInt toLocal(Int*    to, const void* from, uInt nr);
00142   static uInt toLocal(uInt*   to, const void* from, uInt nr);
00143   static uInt toLocal(Int64*  to, const void* from, uInt nr);
00144   static uInt toLocal(uInt64* to, const void* from, uInt nr);
00145   static uInt toLocal(Float*  to, const void* from, uInt nr);
00146   static uInt toLocal(Double* to, const void* from, uInt nr);
00147   // </group>
00148 
00149   // Convert one value from local format to Modcomp format.  The from and to
00150   // buffer should not overlap. The floating point functions will throw
00151   // exceptions as they are not implemented yet.
00152   // <group>
00153   static uInt fromLocal(void* to, Char   from);
00154   static uInt fromLocal(void* to, uChar  from);
00155   static uInt fromLocal(void* to, Short  from);
00156   static uInt fromLocal(void* to, uShort from);
00157   static uInt fromLocal(void* to, Int    from);
00158   static uInt fromLocal(void* to, uInt   from);
00159   static uInt fromLocal(void* to, Int64  from);
00160   static uInt fromLocal(void* to, uInt64 from);
00161   static uInt fromLocal(void* to, Float  from);
00162   static uInt fromLocal(void* to, Double from);
00163   // </group>
00164     
00165   // Convert nr values from local format to Modcomp format.  The from and to
00166   // buffer should not overlap. The floating point functions will throw
00167   // exceptions as they are not implemented yet.  
00168   // <group>
00169   static uInt fromLocal(void* to, const Char*   from, uInt nr);
00170   static uInt fromLocal(void* to, const uChar*  from, uInt nr);
00171   static uInt fromLocal(void* to, const Short*  from, uInt nr);
00172   static uInt fromLocal(void* to, const uShort* from, uInt nr);
00173   static uInt fromLocal(void* to, const Int*    from, uInt nr);
00174   static uInt fromLocal(void* to, const uInt*   from, uInt nr);
00175   static uInt fromLocal(void* to, const Int64*  from, uInt nr);
00176   static uInt fromLocal(void* to, const uInt64* from, uInt nr);
00177   static uInt fromLocal(void* to, const Float*  from, uInt nr);
00178   static uInt fromLocal(void* to, const Double* from, uInt nr);
00179   // </group>
00180     
00181 private:
00182   // This class should not be constructed
00183   // (so declare the constructor private).
00184   ModcompConversion();
00185 };
00186 
00187 inline uInt ModcompConversion::toLocal(Char& to, const void* from) {
00188   return CanonicalConversion::toLocal(to, from);
00189 }
00190 
00191 inline uInt ModcompConversion::toLocal(uChar& to, const void* from) {
00192   return CanonicalConversion::toLocal(to, from);
00193 }
00194 
00195 inline uInt ModcompConversion::toLocal(Short& to, const void* from) {
00196   return CanonicalConversion::toLocal(to, from);
00197 }
00198 
00199 inline uInt ModcompConversion::toLocal(uShort& to, const void* from) {
00200   return CanonicalConversion::toLocal(to, from);
00201 }
00202 
00203 inline uInt ModcompConversion::toLocal(Int& to, const void* from) {
00204   return CanonicalConversion::toLocal(to, from);
00205 }
00206 
00207 inline uInt ModcompConversion::toLocal(uInt& to, const void* from) {
00208   return CanonicalConversion::toLocal(to, from);
00209 }
00210 
00211 inline uInt ModcompConversion::toLocal(Int64& to, const void* from) {
00212     Int tmp;
00213     uInt res = toLocal (tmp, from);
00214     to = tmp;
00215     return res;
00216 }
00217 
00218 inline uInt ModcompConversion::toLocal(uInt64& to, const void* from) {
00219     uInt tmp;
00220     uInt res = toLocal (tmp, from);
00221     to = tmp;
00222     return res;
00223 }
00224 
00225 inline uInt ModcompConversion::toLocal(Float& to, const void* from) {
00226   return ModcompConversion::toLocal(&to, from, 1u);
00227 }
00228 
00229 inline uInt ModcompConversion::toLocal(Double& to, const void* from) {
00230   return ModcompConversion::toLocal(&to, from, 1u);
00231 }
00232 
00233 inline uInt ModcompConversion::toLocal(Char* to, const void* from, uInt nr) {
00234   return CanonicalConversion::toLocalChar(to, from, nr);
00235 }
00236 
00237 inline uInt ModcompConversion::toLocal(uChar* to, const void* from, uInt nr) {
00238   return CanonicalConversion::toLocalUChar(to, from, nr);
00239 }
00240 
00241 inline uInt ModcompConversion::toLocal(Short* to, const void* from, uInt nr) {
00242   return CanonicalConversion::toLocalShort(to, from, nr);
00243 }
00244 
00245 inline uInt ModcompConversion::toLocal(uShort* to, const void* from, uInt nr) {
00246   return CanonicalConversion::toLocalUShort(to, from, nr);
00247 }
00248 
00249 inline uInt ModcompConversion::toLocal(Int* to, const void* from, uInt nr) {
00250   return CanonicalConversion::toLocalInt(to, from, nr);
00251 }
00252 
00253 inline uInt ModcompConversion::toLocal(uInt* to, const void* from, uInt nr) {
00254   return CanonicalConversion::toLocalUInt(to, from, nr);
00255 }
00256 
00257 inline uInt ModcompConversion::fromLocal(void* to, Char from) {
00258   return CanonicalConversion::fromLocal(to, from);
00259 }
00260 
00261 inline uInt ModcompConversion::fromLocal(void* to, uChar from) {
00262   return CanonicalConversion::fromLocal(to, from);
00263 }
00264 
00265 inline uInt ModcompConversion::fromLocal(void* to, Short from) {
00266   return CanonicalConversion::fromLocal (to, from);
00267 }
00268 
00269 inline uInt ModcompConversion::fromLocal(void* to, uShort from) {
00270   return CanonicalConversion::fromLocal(to, from);
00271 }
00272 
00273 inline uInt ModcompConversion::fromLocal(void* to, Int from) {
00274   return CanonicalConversion::fromLocal (to, from);
00275 }
00276 
00277 inline uInt ModcompConversion::fromLocal(void* to, uInt from) {
00278   return CanonicalConversion::fromLocal (to, from);
00279 }
00280 
00281 inline uInt ModcompConversion::fromLocal(void* to, Int64 from) {
00282   return CanonicalConversion::fromLocal (to, (Int) from);
00283 }
00284 
00285 inline uInt ModcompConversion::fromLocal(void* to, uInt64 from) {
00286   return CanonicalConversion::fromLocal (to, (uInt) from);
00287 }
00288 
00289 inline uInt ModcompConversion::fromLocal(void* to, Float from) {
00290   return ModcompConversion::fromLocal(to, &from, 1u);
00291 }
00292 
00293 inline uInt ModcompConversion::fromLocal(void* to, Double from) {
00294   return ModcompConversion::fromLocal(to, &from, 1u);
00295 }
00296 
00297 inline uInt ModcompConversion::fromLocal(void* to, const Char* from, uInt nr) {
00298   return CanonicalConversion::fromLocalChar(to, from, nr);
00299 }
00300 
00301 inline uInt ModcompConversion::fromLocal(void* to, const uChar* from, uInt nr){
00302   return CanonicalConversion::fromLocalUChar(to, from, nr);
00303 }
00304 
00305 inline uInt ModcompConversion::fromLocal(void* to, const Short* from, uInt nr){
00306   return CanonicalConversion::fromLocalShort(to, from, nr);
00307 }
00308 
00309 inline uInt ModcompConversion::fromLocal(void* to, const uShort* from,uInt nr){
00310   return CanonicalConversion::fromLocalUShort(to, from, nr);
00311 }
00312 
00313 inline uInt ModcompConversion::fromLocal(void* to, const Int* from, uInt nr) {
00314   return CanonicalConversion::fromLocalInt(to, from, nr);
00315 }
00316 
00317 inline uInt ModcompConversion::fromLocal(void* to, const uInt* from, uInt nr) {
00318   return CanonicalConversion::fromLocalUInt(to, from, nr);
00319 }
00320 
00321 
00322 
00323 } //# NAMESPACE CASA - END
00324 
00325 #endif