G4OrderedTable Class Reference

#include <G4OrderedTable.hh>


Public Member Functions

 G4OrderedTable ()
 G4OrderedTable (size_t cap)
virtual ~G4OrderedTable ()
void clearAndDestroy ()
G4bool Store (const G4String &filename, G4bool ascii=false)
G4bool Retrieve (const G4String &filename, G4bool ascii=false)

Friends

std::ostream & operator<< (std::ostream &out, G4OrderedTable &table)


Detailed Description

Definition at line 57 of file G4OrderedTable.hh.


Constructor & Destructor Documentation

G4OrderedTable::G4OrderedTable (  ) 

Definition at line 43 of file G4OrderedTable.cc.

00044   : std::vector<G4DataVector*>()
00045 {
00046 }

G4OrderedTable::G4OrderedTable ( size_t  cap  )  [explicit]

Definition at line 48 of file G4OrderedTable.cc.

00049   : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) )
00050 {
00051 }

G4OrderedTable::~G4OrderedTable (  )  [virtual]

Definition at line 53 of file G4OrderedTable.cc.

00054 {
00055 }


Member Function Documentation

void G4OrderedTable::clearAndDestroy (  )  [inline]

Definition at line 90 of file G4OrderedTable.hh.

Referenced by Retrieve(), and G4SandiaTable::~G4SandiaTable().

00091 {
00092   G4DataVector* a = 0;
00093   while (size()>0)
00094   {
00095     a = back();
00096     pop_back();
00097     for (iterator i=begin(); i!=end(); i++)
00098     {
00099       if (*i==a)
00100       {
00101         erase(i);
00102         i--;
00103       }
00104     } 
00105     if ( a ) { delete a; }
00106   } 
00107 }

G4bool G4OrderedTable::Retrieve ( const G4String filename,
G4bool  ascii = false 
)

Definition at line 110 of file G4OrderedTable.cc.

References clearAndDestroy(), G4cerr, G4endl, G4DataVector::Retrieve(), and G4DataVector::T_G4DataVector.

00112 {
00113   std::ifstream fIn;  
00114   // open input file //
00115   if (ascii)
00116     { fIn.open(fileName,std::ios::in|std::ios::binary); }
00117   else
00118     { fIn.open(fileName,std::ios::in); }
00119 
00120   // check if the file has been opened successfully 
00121   if (!fIn)
00122   {
00123 #ifdef G4VERBOSE  
00124     G4cerr << "G4OrderedTable::Retrieve():";
00125     G4cerr << " Cannot open file: " << fileName << G4endl;
00126 #endif
00127     fIn.close();
00128     return false;
00129   }
00130 
00131   // clear 
00132   clearAndDestroy();
00133   
00134   // Number of elements
00135   G4int tableSize=0; 
00136   if (!ascii)
00137   {
00138     fIn.read((char*)(&tableSize), sizeof tableSize); 
00139   }
00140   else
00141   {
00142     fIn >> tableSize;
00143   }
00144   if (tableSize<=0)
00145   {
00146 #ifdef G4VERBOSE  
00147     G4cerr << "G4OrderedTable::Retrieve():";
00148     G4cerr << " Invalid table size: " << tableSize << G4endl;
00149 #endif
00150     return false;
00151   }
00152   reserve(tableSize); 
00153 
00154   // Physics Vector
00155   for (G4int idx=0; idx<tableSize; ++idx)
00156   {
00157     G4int vType=0;
00158     if (!ascii)
00159     {
00160       fIn.read( (char*)(&vType), sizeof vType); 
00161     }
00162     else
00163     {
00164       fIn >>  vType;
00165     }
00166     if (vType != G4DataVector::T_G4DataVector)
00167     {
00168 #ifdef G4VERBOSE  
00169       G4cerr << "G4OrderedTable::Retrieve():";
00170       G4cerr << " Illegal Data Vector type: " << vType << " in  ";
00171       G4cerr << fileName << G4endl;
00172 #endif          
00173       fIn.close();
00174       return false;
00175     }
00176 
00177     G4DataVector* pVec = new G4DataVector;
00178 
00179     if (! (pVec->Retrieve(fIn,ascii)) )
00180     {
00181 #ifdef G4VERBOSE  
00182       G4cerr << "G4OrderedTable::Retrieve(): ";
00183       G4cerr << " Rrror in retreiving " << idx
00184              << "-th Physics Vector from file: ";
00185       G4cerr << fileName << G4endl;
00186 #endif          
00187       fIn.close();
00188       delete pVec;
00189       return false;
00190     }
00191 
00192     // add a PhysicsVector to this OrderedTable
00193     push_back(pVec);
00194   } 
00195   fIn.close();
00196   return true;
00197 }

G4bool G4OrderedTable::Store ( const G4String filename,
G4bool  ascii = false 
)

Definition at line 57 of file G4OrderedTable.cc.

References G4cerr, G4endl, and G4DataVector::T_G4DataVector.

00059 {
00060   std::ofstream fOut;  
00061   
00062   // open output file //
00063   if (!ascii)
00064     { fOut.open(fileName, std::ios::out|std::ios::binary); }
00065   else
00066     { fOut.open(fileName, std::ios::out); }
00067 
00068   // check if the file has been opened successfully 
00069   if (!fOut)
00070   {
00071 #ifdef G4VERBOSE  
00072     G4cerr << "G4OrderedTable::::Store():";
00073     G4cerr << " Cannot open file: " << fileName << G4endl;
00074 #endif
00075     fOut.close();
00076     return false;
00077   }
00078 
00079  // Number of elements
00080   size_t tableSize = size(); 
00081   if (!ascii)
00082   {
00083     fOut.write( (char*)(&tableSize), sizeof tableSize); 
00084   }
00085   else
00086   {
00087     fOut << tableSize << G4endl;
00088   }
00089 
00090   // Data Vector
00091   G4int vType = G4DataVector::T_G4DataVector;
00092   for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr)
00093   {
00094     if (!ascii)
00095     {
00096       fOut.write( (char*)(&vType), sizeof vType); 
00097     }
00098     else
00099     {
00100       fOut << vType << G4endl;
00101     }
00102     (*itr)->Store(fOut,ascii);
00103   }
00104   fOut.close();
00105   return true;
00106 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
G4OrderedTable table 
) [friend]

Definition at line 199 of file G4OrderedTable.cc.

00201 {
00202   // Printout Data Vector
00203   size_t i=0;
00204   for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr)
00205   {
00206     out << std::setw(8) << i << "-th Vector   ";
00207     out << ": Type    " << G4DataVector::T_G4DataVector << G4endl;
00208     out << *(*itr);
00209     i +=1;
00210   }
00211   out << G4endl;
00212   return out; 
00213 }


The documentation for this class was generated from the following files:
Generated on Mon May 27 17:52:48 2013 for Geant4 by  doxygen 1.4.7