/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/sio/src/SIO_blockManager.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CVS $Id: SIO_blockManager.cc,v 1.2 2004/04/05 13:33:11 gaede Exp $
00003 // ----------------------------------------------------------------------------
00004 // => Manager for a list of SIO blocks.                           
00005 // ----------------------------------------------------------------------------
00006 //
00007 // General Description:
00008 //
00009 // SIO_blockManager manages a list of SIO blocks.                   
00010 //
00011 // ----------------------------------------------------------------------------
00012 
00013 #ifdef _MSC_VER
00014 #   pragma warning(disable:4786)        // >255 characters in debug information
00015 #endif
00016 
00017 #include <iostream>
00018 #include <vector>
00019 
00020 #include "SIO_recordManager.h"
00021 #include "SIO_blockManager.h"
00022 #include "SIO_block.h"
00023 #include "SIO_functions.h"
00024 
00025 // ----------------------------------------------------------------------------
00026 // Initialize the private static variables.
00027 // ----------------------------------------------------------------------------
00028 blockMap_c*    SIO_blockManager::blockMap  = NULL;
00029 SIO_verbosity  SIO_blockManager::verbosity = SIO_SILENT;
00030 
00031 // ----------------------------------------------------------------------------
00032 // Add a block of the given name.
00033 // ----------------------------------------------------------------------------
00034 SIO_block* SIO_blockManager::add
00035 (
00036     SIO_block*     block
00037 )
00038 {
00039 
00040 //
00041 // Local variables.
00042 //
00043 std::string
00044    *s_name;
00045 
00046 std::pair< blockMap_i, bool >
00047     status;
00048 
00049 const char
00050    *i_name;
00051 
00052 //
00053 // Check the name for validity.
00054 //
00055 s_name = block->getName();
00056 i_name = s_name->c_str();
00057 if( !SIO_functions::validateName( i_name ) )
00058 {
00059     if( verbosity >= SIO_ERRORS )
00060     {
00061         std::cout << "SIO: [Block Manager] "
00062                   << "Invalid block name"
00063                   << i_name
00064                   << std::endl;
00065     }
00066     return( NULL );
00067 }
00068 
00069 //
00070 // If the map's never been instantiated, do it now!
00071 //
00072 if( blockMap == NULL )
00073     blockMap = new blockMap_c;
00074 
00075 //
00076 // Initialize the map entry.
00077 //
00078 std::pair< std::string const, SIO_block* >
00079     entry( *s_name, block );
00080 
00081 //
00082 // Insert the entry.  This may or may not succeed depending on whether the
00083 // named block pre-exists.  If it does pre-exist, print a warning and return
00084 // a NULL pointer (that should get the caller's attention).
00085 //
00086 status = blockMap->insert( entry );
00087 if( !status.second )
00088 {
00089     if( verbosity >= SIO_ERRORS )
00090     {
00091         std::cout << "SIO: [Block Manager] Block"
00092                   << i_name
00093                   << "not added (already exists)"
00094                   << std::endl;
00095     }
00096     return( NULL );
00097 }
00098 
00099 if( verbosity >= SIO_ALL )
00100 {
00101     std::cout << "SIO: [Block Manager] Added block "
00102               << i_name
00103               << std::endl;
00104 }
00105 
00106 //
00107 // That's all folks!
00108 //
00109 return (status.first)->second;
00110 }
00111 
00112 // ----------------------------------------------------------------------------
00113 // Given its name, return a pointer to a block.
00114 // ----------------------------------------------------------------------------
00115 SIO_block* SIO_blockManager::get
00116 (
00117     const char*  i_name
00118 )
00119 {
00120 
00121 //
00122 // Search the map (if it exists yet!)
00123 //
00124 if( blockMap != NULL )
00125 {
00126     blockMap_i
00127         iter;
00128 
00129     std::string
00130         s_name = i_name;
00131     
00132     if( (iter = blockMap->find( s_name )) != blockMap->end() )
00133     {
00134         if( verbosity >= SIO_ALL )
00135         {
00136             std::cout << "SIO: [Block Manager] Block "
00137                       << i_name
00138                       << " is defined (pointer returned)"
00139                       << std::endl;
00140         }
00141         return( iter->second );
00142     }
00143 }
00144  
00145 if( verbosity >= SIO_ALL )
00146 {
00147     std::cout << "SIO: [Block Manager] Block "
00148               << i_name
00149               << " has not been defined"
00150               << std::endl;
00151 }
00152 
00153 //
00154 // That's all folks!
00155 //
00156 return( NULL );
00157 }
00158 
00159 // ----------------------------------------------------------------------------
00160 // Get the verbosity level.
00161 // ----------------------------------------------------------------------------
00162 SIO_verbosity SIO_blockManager::getVerbosity() { return( verbosity ); }
00163 
00164 
00165 // ----------------------------------------------------------------------------
00166 //  remove and delete all known blocks
00167 // ----------------------------------------------------------------------------
00168 // FG 05042004: needed LCIO memory mgmt
00169 void SIO_blockManager::clear() {
00170 
00171   blockMap_i iter;
00172   std::vector<std::string> blockNames ;
00173     
00174   if( blockMap == 0 ) return ; // nothing to clear
00175 
00176   // as the d'tor of SIO_block removes the block from the map
00177   // using the map iterator is not safe, so get a list of keys first
00178   for(iter = blockMap->begin() ;  iter != blockMap->end() ; iter++) {
00179     blockNames.push_back( iter->first  ) ;
00180   }
00181 
00182   for(int i=0 ;i<blockNames.size() ;i++){
00183     delete (*blockMap)[ blockNames[i] ] ;
00184   }
00185   
00186   delete blockMap;
00187   blockMap = NULL;
00188 
00189 }
00190 
00191 // ----------------------------------------------------------------------------
00192 // Given its name, remove a block.
00193 // ----------------------------------------------------------------------------
00194 unsigned int SIO_blockManager::remove
00195 (
00196     const char*  i_name
00197 )
00198 {
00199 
00200 //
00201 // Search the map (if it exists!)
00202 //
00203 if( blockMap != NULL )
00204 {
00205     blockMap_i
00206         iter;
00207 
00208     std::string
00209         s_name = i_name;
00210     
00211     if( (iter = blockMap->find( s_name )) != blockMap->end() )
00212     {
00213 
00214         //
00215         // The block is in the list.  Before removing it, ensure that it
00216         // isn't connected to any records.
00217         //
00218         SIO_recordManager::disconnect( i_name );
00219 
00220 
00221         //
00222         // OK, remove it from management.
00223         //
00224         blockMap->erase( iter );
00225 
00226         if( verbosity >= SIO_ALL )
00227         {
00228             std::cout << "SIO: [Block Manager] Removed block "
00229                       << i_name
00230                       << std::endl;
00231         }
00232 
00233         // fg - done in clear
00234 //         if( blockMap->size() == 0 )
00235 //         {
00236 //             delete blockMap;
00237 //             blockMap = NULL;
00238 //         } 
00239         return( SIO_BLOCK_SUCCESS );
00240     }
00241 }
00242 
00243 if( verbosity >= SIO_ERRORS )
00244 {
00245     std::cout << "SIO: [Block Manager] Cannot remove block "
00246               << i_name
00247               << " (it was never added)"
00248               << std::endl;
00249 }
00250 
00251 //
00252 // That's all folks!
00253 //
00254 return( SIO_BLOCK_NOTFOUND );
00255 }
00256 
00257 // ----------------------------------------------------------------------------
00258 // Given its pointer, remove a block.
00259 // ----------------------------------------------------------------------------
00260 unsigned int SIO_blockManager::remove
00261 (
00262     SIO_block*   block
00263 )
00264 { return( remove( block->getName()->c_str() ) ); }
00265 
00266 // ----------------------------------------------------------------------------
00267 // Set the verbosity level.
00268 // ----------------------------------------------------------------------------
00269 SIO_verbosity SIO_blockManager::setVerbosity
00270 (
00271     SIO_verbosity   i_verb
00272 )
00273 { SIO_verbosity o_verb = verbosity; verbosity = i_verb; return( o_verb ); } 

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7