casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Conversion.h
Go to the documentation of this file.
00001 //# Conversion.h: A class with general conversion definitions
00002 //# Copyright (C) 1996,1999,2001,2002,2003
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: Conversion.h 21111 2011-07-20 13:10:57Z gervandiepen $
00027 
00028 #ifndef CASA_CONVERSION_H
00029 #define CASA_CONVERSION_H
00030 
00031 //# Includes
00032 #include <casa/string.h>                       // needed for memcpy
00033 
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // <summary>
00038 // A class with general conversion definitions
00039 // </summary>
00040 
00041 // <use visibility=export>
00042 
00043 // <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tConversion" demos="">
00044 // </reviewed>
00045 
00046 // <synopsis>
00047 // This class contains the general definitions for the Conversion classes.
00048 // <ul>
00049 // <li>
00050 // It defines the signature for the functions converting from input
00051 // to output format (e.g. from local to canonical format). There is
00052 // a version where the number of values is given and a version where
00053 // the number of bytes is given. The latter is there to be able to use
00054 // memcpy as a conversion function (which will often be the case).
00055 // Note that the signatures only differ in return value.
00056 // <li>
00057 // It defines functions to convert Bools to bits and vice-versa.
00058 // These are used elsewhere to store Bools as space efficient as possible.
00059 // Note that these functions are machine independent (they work on little
00060 // and big endian machines).
00061 // <li>
00062 // It defines a private version of memcpy for compilers having a
00063 // different signature for memcpy (e.g. ObjectCenter and DEC-Alpha).
00064 // </ul>
00065 // Static functions in the classes
00066 // <linkto class=CanonicalConversion>CanonicalConversion</linkto>,
00067 // <linkto class=VAXConversion>VAXConversion</linkto>, and
00068 // <linkto class=IBMConversion>IBMConversion</linkto> convert data
00069 // from/to canonical, VAX, and IBM/360 format, resp..
00070 // <br>Classes derived from
00071 // <linkto class=DataConversion>DataConversion</linkto>
00072 // provide the same functionality in a polymorphic way.
00073 // </synopsis>
00074 
00075 // <motivation>
00076 // This provides a common place for definitions used elsewhere.
00077 // It also provides a uniform interface to memcpy.
00078 // </motivation>
00079 
00080 //# <todo asof="$DATE$">
00081 //# </todo>
00082 
00083 
00084 class Conversion
00085 {
00086 public:
00087     // Define the signature of a function converting <src>nvalues</src>
00088     // values from internal to external format or vice-versa.
00089     // These functions are used in the <linkto class=TypeIO>IO framework
00090     // </linkto>, but are also used in the table system.
00091     // Examples of such conversions are:
00092     // <br>- local <-> canonical    (when storing in canonical format)
00093     // <br>- local <-> local        (when storing in local format)
00094     // <br>- binary <-> ASCII
00095     // <br>It returns the number of bytes in <em>external</em> format.
00096     // (For example the ToLocal/FromLocal functions in class
00097     // <linkto class=CanonicalConversion>CanonicalConversion</linkto>
00098     // return the number of bytes in canonical format).
00099     typedef unsigned int ValueFunction (void* to, const void* from,
00100                                         unsigned int nvalues);
00101 
00102     // Define the signature of a function converting from one
00103     // format to another providing the number of bytes.
00104     // It returns the <src>to</src> pointer (similar to memcpy).
00105     // (For example the byteTo/FromLocalXXX functions in class
00106     // <linkto class=CanonicalConversion>CanonicalConversion</linkto>.
00107     typedef void* ByteFunction (void* to, const void* from,
00108                                 unsigned int nbytes);
00109 
00110     // Convert a stream of Bools to output format (as bits).
00111     // The variable <src>startBit</src> (0-relative) indicates
00112     // where to start in the <src>to</src> buffer.
00113     // <group>
00114     static unsigned int boolToBit (void* to, const void* from,
00115                                    unsigned int nvalues);
00116     static void boolToBit (void* to, const void* from,
00117                            unsigned int startBit,
00118                            unsigned int nvalues);
00119     // </group>
00120 
00121     // Convert a stream of Bools to output format (as bits).
00122     // The variable <src>startBit</src> (0-relative) indicates
00123     // where to start in the <src>from</src> buffer.
00124     // <group>
00125     static unsigned int bitToBool (void* to, const void* from,
00126                                    unsigned int nvalues);
00127     static void bitToBool (void* to, const void* from,
00128                            unsigned int startBit,
00129                            unsigned int nvalues);
00130     // </group>
00131 
00132     // Copy a value using memcpy.
00133     // It differs from memcpy in the return value.
00134     // <note> This version has the <src>ValueFunction</src> signature,
00135     // but it expects as input the number of bytes.
00136     // </note>
00137     static unsigned int valueCopy (void* to, const void* from,
00138                                    unsigned int nbytes);
00139 
00140     // Get a pointer to the memcpy function.
00141     static ByteFunction* getmemcpy();
00142 
00143     // A placeholder for the ObjectCenter or DEC-alpha memcpy or 64bit SGI
00144     // Also added this for HPUX11 (2b provided in the makedefs)
00145     // (because they do not use an unsigned int for nbytes).
00146     static void* mymemcpy (void* to, const void* from, unsigned int nbytes);
00147 
00148 private:
00149     // Copy bits to Bool in an unoptimized way needed when 'to' is not
00150     // aligned properly.
00151     static unsigned int bitToBool_ (void* to, const void* from,
00152                                     unsigned int nvalues);
00153 };
00154 
00155 
00156 inline Conversion::ByteFunction* Conversion::getmemcpy()
00157 {
00158 #if defined(__CLCC__)  ||  defined(AIPS_64B) || defined(HPUX11) || defined(AIPS_DARWIN) || defined(AIPS_CRAY_PGI)
00159     return mymemcpy;
00160 #else
00161     return memcpy;
00162 #endif
00163 }
00164 
00165 
00166 
00167 } //# NAMESPACE CASA - END
00168 
00169 #endif