Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

Xdr.hh

00001 #ifndef Xdr_h
00002 #define Xdr_h
00003 
00004 #ifdef USE_CONDDB
00005 
00006 // 
00007 #include "lccd.h"
00008 
00009 #include <typeinfo>
00010 
00011 namespace lccd {
00012   
00022   class Xdr {
00023     
00024   public:
00025     
00029     static void copy( unsigned const char* from, unsigned char* dest, 
00030                       const int size, const int count);
00031     
00032     
00033     
00037     template <class T>
00038     static void tostream( std::string& s , T* t, unsigned count ) {
00039       
00040       int bytlen, padlen;
00041       bytlen = count * sizeof( T ) ;
00042       padlen = (bytlen + 3) & 0xfffffffc ; 
00043       
00044       
00045       unsigned index = s.size() ;
00046       
00047       s.resize( s.size() + padlen ) ;
00048       
00049       unsigned char* dest = reinterpret_cast< unsigned char* > (  & s[ index ] ) ;
00050       unsigned char* from = reinterpret_cast< unsigned char* > ( t )  ;
00051 
00052 //        std::cout << " tostream: 0x" << std::hex << *t  << std::dec << std::endl ; 
00053       
00054       Xdr::copy( from , dest , sizeof( T ) , count  ) ;
00055       
00056     }
00057 
00063     template <class T>
00064     static unsigned fromstream( const std::string& s , unsigned position, T* t, unsigned count ) {
00065       
00066       if( count == 0 )
00067         return position ;
00068 
00069       unsigned bytlen, padlen;
00070       bytlen = count * sizeof( T ) ;
00071       padlen = (bytlen + 3) & 0xfffffffc ; 
00072       
00073 
00074       unsigned const char* from = reinterpret_cast< unsigned const char* > (  & s[ position ] ) ;
00075       unsigned char* dest = reinterpret_cast< unsigned char* > ( t )  ;
00076       
00077 
00078 
00079       Xdr::copy( from , dest , sizeof( T ) , count  ) ;
00080 
00081      
00082 //       std::cout << " fromstream: " ; 
00083 //       for( unsigned int i=0; i < count; i++ )
00084 //      std::cout       << std::hex << t[i]  << ", " << std::dec  ;
00085 //       std::cout      << "   - [" << position << "] - type:" 
00086 //              << typeid( *t ).name()  
00087 //              << std::endl ; 
00088       
00089       
00090       return position + padlen ;
00091     }
00092     
00093     
00094     
00097 //     template<>
00098     static void tostream( std::string& s , std::string* strVal, unsigned count ) {
00099       
00100       int  strSize = (int) strVal->size() ;
00101       
00102       int bytlen, padlen;
00103       bytlen = strSize + sizeof( int )  ;
00104       
00105       padlen = (bytlen + 3) & 0xfffffffc ; 
00106       
00107       unsigned index = s.size() ;
00108       
00109       s.resize( index + padlen ) ;
00110       
00111       
00112       // write the string's size first 
00113       
00114       unsigned char* dest = reinterpret_cast< unsigned char* > (  & s[ index ] ) ;
00115       unsigned char* from = reinterpret_cast< unsigned char* > ( & strSize )  ;
00116       
00117       Xdr::copy( from , dest , sizeof( int ) , 1  ) ;
00118       
00119       
00120       dest = reinterpret_cast< unsigned char* > (  & s[ index + sizeof( int ) ] ) ;
00121       from = reinterpret_cast< unsigned char* > ( & strVal->operator[](0) )  ;
00122       
00123       //     std::cout << " tostream: - " << *strVal 
00124       //              << " - lenght: " << strSize
00125       //              << std::endl ;
00126       
00127       Xdr::copy( from , dest , 1 , strSize  ) ;
00128       
00129     }
00130 
00131   
00135 //     template <>
00136     static unsigned  fromstream( const std::string& s, unsigned index, 
00137                                  std::string* strVal, unsigned count ) {
00138       
00139       if( count == 0 )
00140         return index ;
00141       
00142       
00143       // read the string length first 
00144       int strSize ;
00145       unsigned const char* from = reinterpret_cast< unsigned const char* > (  & s[ index ] ) ;
00146       unsigned char* dest = reinterpret_cast< unsigned char* > ( & strSize )  ;
00147       
00148       Xdr::copy( from , dest , sizeof( int ) , 1  ) ;
00149       
00150       index += sizeof( int ) ;
00151       
00152       
00153       unsigned bytlen, padlen;
00154       bytlen =  strSize ;
00155       padlen = (bytlen + 3) & 0xfffffffc ; 
00156       
00157       
00158       strVal->resize( strSize ) ;
00159       
00160       from = reinterpret_cast< unsigned const char* > (  & s[ index ] ) ;
00161       dest = reinterpret_cast< unsigned char* > ( & strVal->operator[](0)  )  ;
00162       
00163       Xdr::copy( from , dest , 1 , strSize  ) ;
00164       
00165       //     std::cout << " fromstream: - " << *strVal 
00166       //              << " - lenght: " << strSize
00167       //              << " - [" << index + padlen << "] " 
00168       //              << std::endl ;
00169       
00170       
00171       return index + padlen ;
00172     }
00173     
00174 
00175   };  // class Xdr
00176 }  //end namespace
00177 
00178 #endif // USE_CONDDB
00179 #endif // Xdr_h

Generated on Fri Sep 9 12:30:31 2005 for LCCD by doxygen 1.3.5