inttypes_AMOS.hh

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #ifndef __inttypes_AMOS_HH
00011 #define __inttypes_AMOS_HH 1
00012 
00013 #if HAVE_CONFIG_H
00014 # include "config.h"
00015 #endif
00016 
00017 #include <inttypes.h>       
00018 #include <limits.h>
00019 #include <string>
00020 
00021 
00022 namespace AMOS {
00023 
00024 typedef uint32_t  ID_t;      
00025 typedef uint32_t  NCode_t;   
00026 typedef uint32_t  SD_t;      
00027 typedef int32_t   Size_t;    
00028 typedef Size_t    Pos_t;     
00029 typedef char      Status_t;  
00030 
00031 const uint8_t  NCODE_SIZE  =  3;         
00032 const NCode_t  NULL_NCODE  =  0;         
00033 const ID_t     NULL_ID     =  0;         
00034 const Status_t NULL_STATUS =  0;         
00035 const char     MIN_QUALITY = '0';        
00036 const char     MAX_QUALITY = '0' + 63;   
00037 const char     NULL_CHAR   = '\0';       
00038 const char     NL_CHAR     = '\n';       
00039 const std::string  NULL_STRING = &NULL_CHAR; 
00040 
00041 const ID_t     MAX_ID      = ~((uint32_t)0);        
00042 const Size_t   MAX_SIZE    = ~((uint32_t)0) >> 1;   
00043 const Pos_t    MAX_POS     = ~((uint32_t)0) >> 1;   
00044 
00045 
00046 //----------------------------------------------------- Qual2Char --------------
00052 inline char Qual2Char (uint8_t qual)
00053 {
00054   return qual + MIN_QUALITY;
00055 }
00056 
00057 
00058 //----------------------------------------------------- Char2Qual --------------
00064 inline uint8_t Char2Qual (char qual)
00065 {
00066   return qual - MIN_QUALITY;
00067 }
00068 
00069 
00070 //----------------------------------------------------- Decode -----------------
00076 inline std::string Decode (NCode_t ncode)
00077 {
00078   char buff[4] = {ncode & CHAR_MAX, ncode >> CHAR_BIT & CHAR_MAX,
00079                   ncode >> CHAR_BIT >> CHAR_BIT & CHAR_MAX, '\0'};
00080   return buff;
00081 }
00082 
00083 
00084 //----------------------------------------------------- Encode -----------------
00091 inline NCode_t Encode (const std::string & str)
00092 {
00093   return (((str[2] << CHAR_BIT) | str[1]) << CHAR_BIT) | str[0];
00094 }
00095 
00096 
00097 //================================================ BankFlags_t =================
00103 //==============================================================================
00104 struct BankFlags_t
00105 {
00106 //-- check which way we pack our bits, reverse order if machine is hi-to-lo
00107 //   we should also limit this to 1byte in size to avoid endian issues
00108 #ifdef BITFIELDS_HTOL
00109   uint8_t nibble      : 4;        
00110   uint8_t is_flagB    : 1;        
00111   uint8_t is_flagA    : 1;        
00112   uint8_t is_modified : 1;        
00113   uint8_t is_removed  : 1;        
00114 #else
00115   uint8_t is_removed  : 1;        
00116   uint8_t is_modified : 1;        
00117   uint8_t is_flagA    : 1;        
00118   uint8_t is_flagB    : 1;        
00119   uint8_t nibble      : 4;        
00120 #endif  
00121 
00122   //------------------------------------------------- BankFlags_t --------------
00127   BankFlags_t ( )
00128   {
00129     clear( );
00130   }
00131   
00132   
00133   //------------------------------------------------- ~BankFlags_t -------------
00136   ~BankFlags_t ( )
00137   {
00138     
00139   }
00140 
00141 
00142   //------------------------------------------------- clear --------------------
00145   void clear ( )
00146   {
00147     is_removed = is_modified = is_flagA = is_flagB = 0; nibble = 0;
00148   }
00149 };
00150 
00151 
00152 #define swap16(x) \
00153      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
00155 
00156 #define swap32(x) \
00157      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8)    \
00158       | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00160 
00161 #define swap64(x) \
00162      ((((x) & 0xff00000000000000ull) >> 56)    \
00163       | (((x) & 0x00ff000000000000ull) >> 40)  \
00164       | (((x) & 0x0000ff0000000000ull) >> 24)  \
00165       | (((x) & 0x000000ff00000000ull) >> 8)   \
00166       | (((x) & 0x00000000ff000000ull) << 8)   \
00167       | (((x) & 0x0000000000ff0000ull) << 24)  \
00168       | (((x) & 0x000000000000ff00ull) << 40)  \
00169       | (((x) & 0x00000000000000ffull) << 56))
00171 
00172 
00173 //-- check machine endian-ness, if host is big-endian we need to swap
00174 #ifdef WORDS_BIGENDIAN
00175 # define htol16(x)     swap16(x)    
00176 # define htol32(x)     swap32(x)    
00177 # define htol64(x)     swap64(x)    
00178 # define ltoh16(x)     swap16(x)    
00179 # define ltoh32(x)     swap32(x)    
00180 # define ltoh64(x)     swap64(x)    
00181 #else
00182 # define htol16(x)     (x)          
00183 # define htol32(x)     (x)          
00184 # define htol64(x)     (x)          
00185 # define ltoh16(x)     (x)          
00186 # define ltoh32(x)     (x)          
00187 # define ltoh64(x)     (x)          
00188 #endif
00189 
00190 //-- check machine endian-ness, if host is little-endian we need to swap
00191 #ifdef WORDS_BIGENDIAN
00192 # define htob16(x)     (x)          
00193 # define htob32(x)     (x)          
00194 # define htob64(x)     (x)          
00195 # define btoh16(x)     (x)          
00196 # define btoh32(x)     (x)          
00197 # define btoh64(x)     (x)          
00198 #else
00199 # define htob16(x)     swap16(x)    
00200 # define htob32(x)     swap32(x)    
00201 # define htob64(x)     swap64(x)    
00202 # define btoh16(x)     swap16(x)    
00203 # define btoh32(x)     swap32(x)    
00204 # define btoh64(x)     swap64(x)    
00205 #endif
00206 
00207 } // namespace AMOS
00208 
00209 #endif // #ifndef __inttypes_AMOS_HH

Generated on Mon Feb 22 17:36:27 2010 for libAMOS by  doxygen 1.4.7