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

MJDatabaseUtil.hh

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------//
00002 //bb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nu//
00003 //                                                                           //
00004 //                         MAJORANA Simulation                               //
00005 //                                                                           //
00006 //      This code implementation is the intellectual property of the         //
00007 //      MAJORANA Collaboration. It is based on Geant4, an intellectual       //
00008 //      property of the RD44 GEANT4 collaboration.                           //
00009 //                                                                           //
00010 //                        *********************                              //
00011 //                                                                           //
00012 //    Neither the authors of this software system, nor their employing       //
00013 //    institutes, nor the agencies providing financial support for this      //
00014 //    work  make  any representation or  warranty, express or implied,       //
00015 //    regarding this software system or assume any liability for its use.    //
00016 //    By copying, distributing or modifying the Program (or any work based   //
00017 //    on on the Program) you indicate your acceptance of this statement,     //
00018 //    and all its terms.                                                     //
00019 //                                                                           //
00020 //bb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nu//
00021 //---------------------------------------------------------------------------//
00032 // Begin description of class here
00039 // End class description
00040 //
00045 // 
00046 // --------------------------------------------------------------------------//
00056 // --------------------------------------------------------------------------//
00057 
00058 #ifndef _MJDATABASEUTIL_HH
00059 #define _MJDATABASEUTIL_HH
00060 
00061 //---------------------------------------------------------------------------//
00062 
00063 #include <algorithm>
00064 #include <iostream>
00065 #include <string>
00066 using namespace std;
00067 
00068 #include <netinet/in.h>
00069 
00070 
00071 //---------------------------------------------------------------------------//
00072 
00073 #ifdef ByteSwap
00074 #undef ByteSwap
00075 #endif
00076 #ifdef byteSwap
00077 #undef byteSwap
00078 #endif
00079 
00080 //functions for byte swaping
00081 #define ByteSwap(x)  MJDatabaseUtil::byteswap((unsigned char*) &x, sizeof(x))
00082 #define byteSwap(x,n)  MJDatabaseUtil::byteswap((unsigned char*) &x,  n)
00083 
00084 class MJDatabaseUtil 
00085 {
00086 public:
00087 
00088   //default constructor
00089   MJDatabaseUtil();
00090 
00091   //copy constructor
00092   MJDatabaseUtil(const MJDatabaseUtil &);
00093 
00094   //destructor
00095   ~MJDatabaseUtil();
00096 
00097   //public interface
00098   // a helper function to get the number of elements in an array
00099   int getNoEle(char* m) { return c_Int(m + 3*sizeof(int));}
00100   
00101   //template function for most multi-byte data types
00102   template <class T> void fillArray(T *&, char*);
00103   
00104   //specialized template functions for char and string
00105   void fillArray(char *&, char*);
00106   void fillArray(char **&, char*);
00107 
00108   //methods for converting the basic types
00109   int c_Int(char* c){ int n = *(int*)c;ByteSwap(n);return n;}
00110   float c_Float(char *c){ float f = *(float*)c; ByteSwap(f);return f;}
00111   double c_Double(char *c){ double d = *(double*)c; ByteSwap(d);return d;}
00112   long long c_Time(char* c)
00113   { long long ll=*(long long*)c; ByteSwap(ll);return ll;}
00114 
00115   //protected members
00116 protected:
00117 
00118 
00119   //private  members
00120 private:
00121   void byteswap(unsigned char *b, int n);
00122   //get the start of memory space containing the data
00123   char* getstart(char* c){ return c+5*sizeof(int);}
00124   //get the size of the next element. 
00125   int getElementSize(){ return sizeof(int);}
00126 
00127 };
00128   template<class T> void
00129  MJDatabaseUtil:: fillArray(T *&ar, char* mem)
00130   {
00131     int nEle = getNoEle(mem);
00132     ar = new T[nEle];
00133     char* start = getstart(mem);
00134     int  elementsize = getElementSize();
00135     for(int i=0; i<nEle; i++){
00136       int size = c_Int(start);
00137       ar[i] = (*(T*)(start + elementsize));
00138       byteSwap(ar[i], size);
00139       start += size + elementsize;
00140     }
00141   }
00142 
00143 #endif

Generated on Mon Nov 29 16:58:49 2004 for Majorana Simulation by  doxygen 1.3.9.1