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

CdbNTupleBase.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbNTupleBase.cc,v 1.4 2004/08/06 05:54:41 bartoldu Exp $
00003 
00004 /// The implementation of the CdbNTupleBase class.
00005 /**
00006   * @see CdbNTupleBase
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbTable/CdbNTupleBase.hh"
00012 
00013 #include <iostream>
00014 #include <assert.h>
00015 #include <stdio.h>
00016 using std::cout;
00017 using std::endl;
00018 
00019 namespace {
00020 
00021     std::string default_name_for_column( unsigned int theNumber )
00022     {
00023         char str[20];
00024         sprintf( str, "%u", theNumber );
00025         return std::string( "#" ) + std::string( str );
00026     }
00027 
00028   // This function wil always resize the original size of the passed array
00029   // to specified length.
00030 
00031     void init_default_column_names( std::vector<std::string>& theColumns,
00032                                     unsigned int              theNumberOfColumns )
00033     {
00034         theColumns.resize( theNumberOfColumns );
00035         for( unsigned int i = 0; i < theNumberOfColumns; ++i ) {
00036             theColumns[i] = default_name_for_column( i );
00037         }
00038     }
00039 };
00040 
00041 CdbNTupleBase::~CdbNTupleBase( )
00042 { }
00043 
00044 CdbNTupleBase::CdbNTupleBase( unsigned int       theNumberOfColumns,
00045                               const std::string& theName,
00046                               const std::string& theDescription ) :
00047     _ncol(theNumberOfColumns),
00048     _name(theName),
00049     _description(theDescription)
00050 {
00051     ::init_default_column_names( _columns,
00052                                  _ncol );
00053 }
00054 
00055 CdbNTupleBase::CdbNTupleBase( unsigned int                    theNumberOfColumns,
00056                               const std::vector<std::string>& theCollumnNames,
00057                               const std::string&              theName,
00058                               const std::string&              theDescription ) :
00059     _ncol(theNumberOfColumns),
00060     _name(theName),
00061     _description(theDescription)
00062 {
00063     const char* fatalStr = "CdbNTupleBase::CdbNTupleBase() -- FATAL ERROR IN THE CONSTRUCTOR.";
00064 
00065   // Initialize column names with defaults
00066 
00067     ::init_default_column_names( _columns,
00068                                  _ncol );
00069 
00070   // Verify a vector with column names passed as a parameter to see if
00071   // these names satisfy the naming rules described at the contract of the current
00072   // class.
00073   //
00074   // @see CdbNTupleBase::CdbNTupleBase
00075 
00076     unsigned int num = theCollumnNames.size( );
00077 
00078     if( _ncol < num ) num = _ncol;  // Ignore the rest of the vector if it's
00079                                     // longer than the number of actual columns.
00080 
00081     for( unsigned int i = 0; i < num; ++i ) {
00082 
00083       // Check if a "system" style name is being attempted to use. Note, that
00084       // "system" names begin with the '#' symbols. The use of these names is
00085       // reserved to the implementations of the tuples. The only exception
00086       // allowed by the naming rules is when default system names are specified
00087       // in the right positions.
00088 
00089         if( theCollumnNames[i][0] == '#' ) {
00090 
00091             if( theCollumnNames[i] == _columns[i] ) continue;
00092 
00093             cout << fatalStr << endl
00094                  << "    An attemp to use a reserved name of the column \"" << theCollumnNames[i].c_str( ) << " has been detected" << endl
00095                  << "    while constructing an object of the current class. See the contract of the class" << endl
00096                  << "    for further details on how the columns should be named." << endl
00097                  << endl
00098                  << "    Here is a list of all names passed to the constructor:" << endl
00099                  << endl;
00100 
00101             for( unsigned int j = 0; j < num; ++j ) cout << "        " << j << " : \"" << theCollumnNames[j].c_str( ) << "\"" << endl;
00102 
00103             assert( 0 );
00104             ::exit( 1 );
00105         }
00106 
00107       // Check if our caller is not trying to duplicate the names which are
00108       // already stored at the begining.
00109 
00110         for( unsigned int j = 0; j < _ncol; ++j ) {
00111             if( theCollumnNames[i] == _columns[j] ) {
00112 
00113                 cout << fatalStr << endl
00114                      << "    A vector containing duplicate column names passed to the current constructor." << endl
00115                      << "    See the contract of the class for further details on how the columns should be named." << endl;
00116 
00117                 assert( 0 );
00118                 ::exit( 1 );
00119             }
00120         }
00121         _columns[i] = theCollumnNames[i];
00122     }
00123 }
00124 
00125 CdbNTupleBase::CdbNTupleBase( const CdbNTupleBase& theOther ) :
00126     _ncol       (theOther._ncol),
00127     _name       (theOther._name),
00128     _description(theOther._description),
00129     _columns    (theOther._columns)
00130 { }
00131 
00132 CdbNTupleBase&
00133 CdbNTupleBase::operator=( const CdbNTupleBase& theOther )
00134 {
00135     if( &theOther != this ) {
00136         _ncol        = theOther._ncol;
00137         _name        = theOther._name;
00138         _description = theOther._description;
00139         _columns     = theOther._columns;
00140     }
00141     return *this;
00142 }
00143 
00144 unsigned int
00145 CdbNTupleBase::columns( ) const
00146 {
00147     return _ncol;
00148 };
00149 
00150 std::string
00151 CdbNTupleBase::name( ) const
00152 {
00153     return _name;
00154 }
00155 
00156 void
00157 CdbNTupleBase::set_name( const std::string& theName )
00158 {
00159     _name = theName;
00160 }
00161 
00162 std::string
00163 CdbNTupleBase::description( ) const
00164 {
00165     return _description;
00166 }
00167 
00168 void
00169 CdbNTupleBase::set_description( const std::string& theDescription )
00170 {
00171     _description = theDescription;
00172 }
00173 
00174 void
00175 CdbNTupleBase::column_names( std::vector<std::string>& theVectorOfNames ) const
00176 {
00177     theVectorOfNames = _columns;
00178 }
00179 
00180 
00181 CdbStatus
00182 CdbNTupleBase::column_name( unsigned int theNumber,
00183                             std::string& theName ) const
00184 {
00185     if( theNumber >= _ncol ) return CdbStatus::NotFound;
00186     theName = _columns[theNumber];
00187     return CdbStatus::Success;
00188 }
00189 
00190 CdbStatus
00191 CdbNTupleBase::column_number( const std::string& theName,
00192                               unsigned int&      theNumber ) const
00193 {
00194     for( unsigned int i = 0; i < _ncol; ++i ) {
00195         if( theName == _columns[theNumber] ) {
00196             theNumber = i;
00197             return CdbStatus::Success;
00198         }
00199     }
00200     return CdbStatus::NotFound;
00201 }
00202 
00203 CdbStatus
00204 CdbNTupleBase::set_column_name( unsigned int       theNumber,
00205                                 const std::string& theName )
00206 {
00207     if( theNumber >= _ncol ) return CdbStatus::NotFound;
00208      _columns[theNumber] = theName;
00209     return CdbStatus::Success;
00210 }
00211 
00212 bool
00213 CdbNTupleBase::is_system_name( const std::string& theName )
00214 {
00215     return '#' == theName.c_str( )[0];
00216 }
00217 
00218 std::string
00219 CdbNTupleBase::default_column_name( unsigned int theNumber )
00220 {
00221     return ::default_name_for_column( theNumber );
00222 }
00223 
00224 /////////////////
00225 // End Of File //
00226 /////////////////

Generated on Mon Dec 5 18:22:07 2005 for CDB by doxygen1.3-rc3