CVR-Lib last update 20 Sep 2009

cvrEndianness.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  * Instituto Tecnológico de Costa Rica, Costa Rica
00005  *
00006  *
00007  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00008  *
00009  * The CVR-Lib is free software; you can redistribute it and/or
00010  * modify it under the terms of the BSD License.
00011  *
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright notice,
00018  *    this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright notice,
00021  *    this list of conditions and the following disclaimer in the documentation
00022  *    and/or other materials provided with the distribution.
00023  *
00024  * 3. Neither the name of the authors nor the names of its contributors may be
00025  *    used to endorse or promote products derived from this software without
00026  *    specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  * POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 
00042 /**
00043  * \file   cvrEndianness.h
00044  *         Contains the class endianness, which allows to read/write data from
00045  *         a file written with a known endianness.
00046  * \author Pablo Alvarado
00047  * \date   11.01.2006
00048  *
00049  */
00050 
00051 #ifndef _CVR_ENDIANNESS_H_
00052 #define _CVR_ENDIANNESS_H_
00053 
00054 #include <iostream>
00055 #include "cvrTypes.h"
00056 #include "cvrConfig.h"
00057 
00058 namespace cvr {
00059 
00060   /**
00061    * Endianness naming constants
00062    */
00063   enum eEndiannessType {
00064     LittleEndian, /**< Little Endian (Like in Intel x86 Processors)*/
00065     BigEndian     /**< Big Endian (Like in IBM PowerPC Processors) */
00066   };
00067 
00068   /**
00069    * Endianness.
00070    *
00071    * This template class is used to read from or write data to a file that was
00072    * created with a known endianness.  For example, Windows BMP image files
00073    * have a header with some integers written with little endianness.
00074    *
00075    * To be more specific, the problem this class tries to solve is the
00076    * following:
00077    *
00078    * Let us assume we have a binary file (and not an ASCII file) with
00079    * following byte stream:
00080    *
00081    * 0x01 0x02 0x03 0x04 ...
00082    *
00083    * and we use following code to read an 32 bit integer (i.e. 4 bytes long):
00084    *
00085    * \code
00086    *   cvr::uint32 integer;             // hold the values been readed
00087    *   std::istream in("theFile.dat");  // the input stream
00088    *
00089    *  ...
00090    *  in.read(&integer,4);             // reads 4 bytes on "integer"
00091    * \endcode
00092    *
00093    * With a little endian system (for example Intel Pentium), the
00094    * contents of \c integer will be 0x04030201.
00095    *
00096    * With a big endian system (for example Motorola/IBM PowerPC), the
00097    * contents of \c integer will be 0x01020304.
00098    *
00099    * We therefore need a class, which allow us to load data from a file with
00100    * the correct endiannes!
00101    *
00102    * The detection of the system endianness will be done at configuration time
00103    * of the library.:
00104    *
00105    * - For Windows systems there is no problem if they run on Intel Processors
00106    *   (always little-endian).  If you need to change this (for example you
00107    *   have an Alpha processor), change the endianness on the file
00108    *   cvrWinConfig.h, ensuring that the symbol <code>WORDS_BIGENDIAN</code>
00109    *   is defined as 1.
00110    *
00111    * - For Unix systems the processor and endianness are determined
00112    *   automatically by the configure script.  If the file config.h exists,
00113    *   everything should be ok!!  In case you really need to change the
00114    *   detected configuration, in config.h the symbol
00115    *   <code>WORDS_BIGENDIAN</code> is defined only in big endian systems.
00116    *
00117    * Template behaviour.
00118    *
00119    * The class cvr::endianness is a template class of an enumerate that can be
00120    * LittleEndian or BigEndian, defined in the lti namespace.
00121    *
00122    * cvr::endianness<cvr::LittleEndian>
00123    * cvr::endianness<cvr::BigEndian>
00124    *
00125    * You can check for the endianness used as default by your system with
00126    * systemEndianness().
00127    *
00128    * Please note that all methods are static, so that you don't need an
00129    * instance of the class to use them.
00130    */
00131   template <eEndiannessType E>
00132   class endianness {
00133   public:
00134     /**
00135      * Default constructor.
00136      */
00137     endianness();
00138 
00139     /**
00140      * Destructor
00141      */
00142     ~endianness();
00143 
00144     /**
00145      * Read a byte from the stream
00146      * @param in input stream
00147      * @param data variable where the data should be stored
00148      * @return a reference to the variable with the readed data
00149      */
00150     static bool read(std::istream& in,byte& data);
00151 
00152     /**
00153      * Read an unsigned byte from the stream
00154      * @param in input stream
00155      * @param data variable where the data should be stored
00156      * @return a reference to the variable with the readed data
00157      */
00158     static bool read(std::istream& in,ubyte& data);
00159 
00160     /**
00161      * Read a int16 (2 bytes)
00162      * @param in input stream
00163      * @param data variable where the data should be stored
00164      * @return a reference to the variable with the readed data
00165      */
00166     static bool read(std::istream& in,int16& data);
00167 
00168     /**
00169      * Read a uint16 (2 bytes)
00170      * @param in input stream
00171      * @param data variable where the data should be stored
00172      * @return a reference to the variable with the readed data
00173      */
00174     static bool read(std::istream& in,uint16& data);
00175 
00176     /**
00177      * Read a int32
00178      * @param in input stream
00179      * @param data variable where the data should be stored
00180      * @return a reference to the variable with the readed data
00181      */
00182     static bool read(std::istream& in,int32& data);
00183 
00184     /**
00185      * Read a uint32
00186      * @param in input stream
00187      * @param data variable where the data should be stored
00188      * @return a reference to the variable with the readed data
00189      */
00190     static bool read(std::istream& in,uint32& data);
00191 
00192     /**
00193      * Write a byte
00194      * @param out output stream
00195      * @param data variable with the data to be stored on the file
00196      * @return a reference to the variable with the data
00197      */
00198     static bool write(std::ostream& out,const byte& data);
00199 
00200     /**
00201      * Write a ubyte
00202      * @param out output stream
00203      * @param data variable with the data to be stored on the file
00204      * @return a reference to the variable with the data
00205      */
00206     static bool write(std::ostream& out,const ubyte& data);
00207 
00208     /**
00209      * Write a int16 (2 bytes)
00210      * @param out output stream
00211      * @param data variable with the data to be stored on the file
00212      * @return a reference to the variable with the data
00213      */
00214     static bool write(std::ostream& out,const int16& data);
00215 
00216     /**
00217      * Write a uint16 (2 bytes)
00218      * @param out output stream
00219      * @param data variable with the data to be stored on the file
00220      * @return a reference to the variable with the data
00221      */
00222     static bool write(std::ostream& out,const uint16& data);
00223 
00224     /**
00225      * Write a int32
00226      * @param out output stream
00227      * @param data variable with the data to be stored on the file
00228      * @return a reference to the variable with the data
00229      */
00230     static bool write(std::ostream& out,const int32& data);
00231 
00232     /**
00233      * Write a uint32
00234      * @param out output stream
00235      * @param data variable with the data to be stored on the file
00236      * @return a reference to the variable with the data
00237      */
00238     static bool write(std::ostream& out,const uint32& data);
00239   };
00240 }
00241 
00242 #endif

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8