/home/aherms/Code/git/awds-routing/src/awds/AbstractId.h

00001 #ifndef __ABSTRACTID_H_
00002 #define __ABSTRACTID_H_
00003 
00004 #include <cstring>
00005 #include <inttypes.h>
00006 
00007 
00013 template<unsigned Size>
00014 class AbstractID {
00015 
00016 public:
00022     static const size_t size = Size; 
00023     
00025     unsigned char id[size];
00026     
00027     AbstractID(){
00028         for (unsigned short i=0; i < Size; i++)
00029             this->id[i] = 0;
00030     }
00031     
00032     AbstractID(const AbstractID<Size>& a) {
00033         for (unsigned short i=0; i < Size; i++)
00034             this->id[i] = a.id[i];
00035     }
00036     
00037     AbstractID(unsigned num) {
00038         for (short i = Size - 1 ; i >= 0; i--) {
00039             id[i] = (char)(num & 0xff);
00040             num >>= 8;
00041         }
00042     }
00043 
00044     const AbstractID<Size>& operator =(const AbstractID<Size>& a) {
00045         for (unsigned short i=0; i < Size; i++)
00046             this->id[i] = a.id[i];
00047         return *this;
00048     }
00049     
00052     AbstractID<Size>& fromArray(const char *data) {
00053         memcpy(this->id, data, size );
00054         return *this;
00055     }
00056     
00057 
00060     AbstractID<Size>& toArray( char *data) const {
00061         memcpy(data, this->id, size );
00062         return *this;
00063     }
00064 
00065  
00067 #define LEXI_BOOL_OP(op) bool operator op (const AbstractID<Size>& a) const { \
00068         for(unsigned short i = 0; i < Size; i++) {                                      \
00069             if (this->id[i] == a.id[i]) continue;                       \
00070             if ( !( this->id[i] op a.id[i] ) ) return false;            \
00071             if (this->id[i] op a.id[i]) return true;                    \
00072         }                                                               \
00073         return (0 op 0);                                                \
00074     }
00075     
00076     LEXI_BOOL_OP(==);
00077     LEXI_BOOL_OP(!=);
00078     LEXI_BOOL_OP(<);
00079     LEXI_BOOL_OP(>);
00080     LEXI_BOOL_OP(<=);
00081     LEXI_BOOL_OP(>=);
00082   
00083 #undef LEXI_BOOL_OP  
00084 };
00085 
00086 
00089 template <>
00090 class AbstractID<6> {
00091     
00092 public:
00093     unsigned long long d;
00094     
00095     static const size_t size = 6;
00096         
00097     AbstractID() : d(0) {}
00098     
00099     AbstractID(const AbstractID<6>& a) : d(a.d) {   }
00100     
00101     explicit AbstractID(unsigned num) {
00102         d = num;
00103     }
00104     
00105     inline const AbstractID<6>& operator =(const AbstractID<6>& a) {
00106         d = a.d;
00107         return *this;
00108     }
00109 #if defined(__GNUC__) && defined(__i386)
00110 
00111 #include <byteswap.h>
00112     inline void fromArray(const char *data) {
00113         unsigned short s = bswap_16(*(unsigned short *)data);
00114         d = (unsigned long long)s * 0x100000000ULL;
00115         unsigned long  l = bswap_32( *(unsigned long *)(data + 2) );
00116         d |= l;
00117     }
00118     
00119     inline void toArray( char *data) const {
00120         *reinterpret_cast<unsigned short *>(data) = 
00121             bswap_16( static_cast<unsigned short>(d / 0x100000000ULL) );
00122         *reinterpret_cast<unsigned long *>(data +2) =
00123             bswap_32( static_cast<unsigned long>(d) );
00124     }
00125     
00126 #else 
00127     
00128     void fromArray(const char *data) {
00129         d = 0;
00130         for (size_t i = 0; i < 6; ++i) {
00131             d *= 0x0100ULL;
00132             d |= (unsigned char)data[i];
00133         }
00134     }
00135     
00136     void toArray( char *data) const {
00137         unsigned long long dd = d;
00138         for (size_t i=0; i < 6; ++i) {
00139             data[5-i] = (char)(unsigned char)dd; 
00140             dd /= 0x0100ULL;
00141         }
00142     }
00143 #endif 
00144     
00145     operator unsigned long() const {
00146         return (unsigned long)d;
00147     }
00148     
00150 #define LEXI_BOOL_OP(op) inline bool operator op (const AbstractID<6>& a) const { return d op a.d;  }
00151     
00152     LEXI_BOOL_OP(==);
00153     LEXI_BOOL_OP(!=);
00154     LEXI_BOOL_OP(<);
00155     LEXI_BOOL_OP(>);
00156     LEXI_BOOL_OP(<=);
00157     LEXI_BOOL_OP(>=);
00158     
00159 #undef LEXI_BOOL_OP  
00160 };
00161 
00162 
00163 
00164 #define ID_IO 1
00165 #if ID_IO
00166 #include <iostream>
00167 
00171 template <unsigned S> 
00172 std::ostream& operator <<(std::ostream& s, const AbstractID<S>& aid) {
00173     for (unsigned short i = 0; i < S;i++) {
00174         static const char *hexnum = "0123456789ABCDEF";
00175         s << hexnum[aid.id[i] / 0x10] 
00176           << hexnum[aid.id[i] % 0x10]; 
00177     }
00178     return s;
00179 }
00180    
00181 inline std::ostream& operator <<(std::ostream& s, const AbstractID<6u> aid) {
00182     static const char *hexnum = "0123456789ABCDEF";
00183     char buf[13];  
00184     unsigned long long dd = aid.d;
00185     for (size_t i=0; i < 6; ++i) {
00186         unsigned char x = (unsigned char)dd;
00187         buf[11 - (2*i)] = hexnum[x % 0x10];
00188         buf[10 - (2*i)] = hexnum[x / 0x10];
00189         dd /= 0x0100ULL;
00190     }
00191     buf[12] = '\0';
00192     return s << buf;
00193 }
00194 
00195 #endif // ID_IO
00196 
00197 #endif // __ABSTRACTID_H_
00198 
00199 /* This stuff is for emacs
00200  * Local variables:
00201  * mode:c++
00202  * c-basic-offset: 4
00203  * End:
00204  */

Generated on Tue Dec 11 17:58:47 2007 for AWDS by  doxygen 1.5.3-20071008