MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/XPCEndian.h
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS CORE Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00029 //   The XPC classes in MOOS are modified versions of the source provided 
00030 //   in "Making UNIX and Windows NT Talk" by Mark Nadelson and Thomas Haga 
00031 //
00033 #ifndef _XPCEndian
00034 #define _XPCEndian
00035 
00036 template<class OrigType> class XPCEndian
00037 {
00038 private:
00039     union EndianType
00040     {
00041         OrigType EndianValue;            // Endian value of original type
00042         char cCharList[sizeof(OrigType)];    // Byte representation of original type
00043 
00044         // Swap the bytes to derive the endian value
00045         void vEndianSwap()
00046         {
00047             char cTemp;
00048             int iSwap2 = 0;
00049             for (int iSwap1 = sizeof(OrigType)-1; iSwap1 >= sizeof(OrigType)/2; iSwap1--)
00050             {
00051                 cTemp = cCharList[iSwap1];
00052                 cCharList[iSwap1] = cCharList[iSwap2];
00053                 cCharList[iSwap2] = cTemp;
00054                 iSwap2++;
00055             }
00056         }
00057     };
00058 
00059     OrigType OriginalVal;    // Stores the data type in its original format
00060     EndianType EndianVal;     // Stores the data type in its endian format
00061 public:
00062     // Overladed = operator used to store the value in its original and endian format
00063     XPCEndian &operator=(OrigType &_Val)
00064     {
00065         OriginalVal = _Val;
00066         EndianVal.EndianValue = _Val;
00067         EndianVal.vEndianSwap();
00068         return *this;
00069     }    
00070 
00071     // Sets the Value of the endian object and stores the value in its original and endia
00072     // format
00073     void vSetValue(OrigType _Val)
00074     {
00075         OriginalVal = _Val;
00076                 EndianVal.EndianValue = _Val;
00077                 EndianVal.vEndianSwap();
00078     }
00079 
00080     // Return the endian value
00081     OrigType getEndianValue() 
00082     { 
00083         return EndianVal.EndianValue;
00084     }
00085 
00086     // Return the original value
00087     OrigType getOriginalValue()
00088     {
00089         return OriginalVal;
00090     }
00091 };
00092 
00093 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines