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

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CVS $Id: SIO_streamManager.cc,v 1.1 2003/03/06 11:01:24 gaede Exp $
00003 // ----------------------------------------------------------------------------
00004 // => Manager for a list of SIO streams.                           
00005 // ----------------------------------------------------------------------------
00006 //
00007 // General Description:
00008 //
00009 // SIO_streamManager manages a list of SIO streams.                   
00010 //
00011 // ----------------------------------------------------------------------------
00012 
00013 #ifdef _MSC_VER
00014 #   pragma warning(disable:4786)        // >255 characters in debug information
00015 #endif
00016 
00017 #include <iostream>
00018 
00019 #include "SIO_streamManager.h"
00020 #include "SIO_stream.h"
00021 #include "SIO_functions.h"
00022 
00023 // ----------------------------------------------------------------------------
00024 // Initialize the private static variables.
00025 // ----------------------------------------------------------------------------
00026 streamMap_c*   SIO_streamManager::streamMap = NULL;
00027 SIO_verbosity  SIO_streamManager::verbosity = SIO_ERRORS;
00028 
00029 // ----------------------------------------------------------------------------
00030 // Add a stream of the given name (buffer reserve not provided).
00031 // ----------------------------------------------------------------------------
00032 SIO_stream* SIO_streamManager::add
00033 (
00034     const char*  i_name
00035 )
00036 { return add( i_name, 4 * SIO_KBYTE ); }
00037 
00038 // ----------------------------------------------------------------------------
00039 // Add a stream of the given name (buffer reserve provided).
00040 // ----------------------------------------------------------------------------
00041 SIO_stream* SIO_streamManager::add
00042 (
00043     const char*    i_name,
00044     unsigned int   i_reserve
00045 )
00046 {
00047 
00048 //
00049 // Local variables.
00050 //
00051 std::pair< streamMap_i, bool >
00052     status;
00053 
00054 std::string
00055     s_name = i_name;
00056 
00057 //
00058 // Check the name for validity.
00059 //
00060 if( !SIO_functions::validateName( i_name ) )
00061 {
00062     if( verbosity >= SIO_ERRORS )
00063     {
00064         std::cout << "SIO: [Stream Manager] "
00065                   << "Invalid stream name "
00066                   << i_name
00067                   << std::endl;
00068     }
00069     return( NULL );
00070 }
00071 
00072 //
00073 // Ensure a minimum 4 kByte buffer.
00074 //
00075 if( i_reserve < 4 * SIO_KBYTE )
00076     i_reserve = 4 * SIO_KBYTE;
00077 
00078 //
00079 // If the map's never been instantiated, do it now!
00080 //
00081 if( streamMap == NULL )
00082     streamMap = new streamMap_c;
00083 
00084 //
00085 // Initialize the map entry.
00086 //
00087 std::pair< std::string const, SIO_stream* >
00088     entry( s_name, NULL );
00089 
00090 //
00091 // Insert the entry.  This may or may not succeed depending on whether the
00092 // named stream pre-exists.  If it does pre-exist, print a warning and return
00093 // a NULL pointer (that should get the caller's attention).
00094 //
00095 status = streamMap->insert( entry );
00096 if( !status.second )
00097 {
00098     if( verbosity >= SIO_ERRORS )
00099     {
00100         std::cout << "SIO: [Stream Manager] Stream "
00101                   << i_name
00102                   << " not added (already exists)"
00103                   << std::endl;
00104     }
00105     return( NULL );
00106 }
00107 
00108 //
00109 // Create the stream to go with this map entry.
00110 //
00111 (status.first)->second = new SIO_stream( i_name, i_reserve, verbosity );
00112 
00113 if( verbosity >= SIO_ALL )
00114 {
00115     std::cout << "SIO: [Stream Manager] Added stream "
00116               << i_name
00117               << std::endl;
00118 }
00119 
00120 //
00121 // That's all folks!
00122 //
00123 return (status.first)->second;
00124 }
00125 
00126 // ----------------------------------------------------------------------------
00127 // Given its name, return a pointer to a stream.
00128 // ----------------------------------------------------------------------------
00129 SIO_stream* SIO_streamManager::get
00130 (
00131     const char*  i_name
00132 )
00133 {
00134 
00135 //
00136 // Search the map (if it exists yet!)
00137 //
00138 if( streamMap != NULL )
00139 {
00140     streamMap_i
00141         iter;
00142 
00143     std::string
00144         s_name = i_name;
00145     
00146     if( (iter = streamMap->find( s_name )) != streamMap->end() )
00147     {
00148         if( verbosity >= SIO_ALL )
00149         {
00150             std::cout << "SIO: [Stream Manager] Stream "
00151                       << i_name
00152                       << " is defined (pointer returned)"
00153                       << std::endl;
00154         }
00155         return( iter->second );
00156     }
00157 }
00158  
00159 if( verbosity >= SIO_ERRORS )
00160 {
00161     std::cout << "SIO: [Stream Manager] Stream "
00162               << i_name
00163               << " has not been defined"
00164               << std::endl;
00165 }
00166 
00167 //
00168 // That's all folks!
00169 //
00170 return( NULL );
00171 }
00172 
00173 // ----------------------------------------------------------------------------
00174 // Get the verbosity level.
00175 // ----------------------------------------------------------------------------
00176 SIO_verbosity SIO_streamManager::getVerbosity() { return( verbosity ); }
00177 
00178 // ----------------------------------------------------------------------------
00179 // Given its name, remove a stream.
00180 // ----------------------------------------------------------------------------
00181 unsigned int SIO_streamManager::remove
00182 (
00183     const char*  i_name
00184 )
00185 {
00186 
00187 //
00188 // Search the map (if it exists!)
00189 //
00190 if( streamMap != NULL )
00191 {
00192     streamMap_i
00193         iter;
00194 
00195     std::string
00196         s_name = i_name;
00197     
00198     if( (iter = streamMap->find( s_name )) != streamMap->end() )
00199     {
00200         delete iter->second;
00201         streamMap->erase( iter );
00202 
00203         if( verbosity >= SIO_ALL )
00204         {
00205             std::cout << "SIO: [Stream Manager] Removed stream "
00206                       << i_name
00207                       << std::endl;
00208         }
00209 
00210         if( streamMap->size() == 0 )
00211         {
00212             delete streamMap;
00213             streamMap = NULL;
00214         } 
00215         return( SIO_STREAM_SUCCESS );
00216     }
00217 }
00218 
00219 if( verbosity >= SIO_ERRORS )
00220 {
00221     std::cout << "SIO: [Stream Manager] Cannot remove stream "
00222               << i_name
00223               << " (it was never added)"
00224               << std::endl;
00225 }
00226 
00227 //
00228 // That's all folks!
00229 //
00230 return( SIO_STREAM_NOTFOUND );
00231 }
00232 
00233 // ----------------------------------------------------------------------------
00234 // Given its pointer, remove a stream.
00235 // ----------------------------------------------------------------------------
00236 unsigned int SIO_streamManager::remove
00237 (
00238     SIO_stream*  record
00239 )
00240 { return( remove( record->getName()->c_str() ) ); }
00241 
00242 // ----------------------------------------------------------------------------
00243 // Set the verbosity level.
00244 // ----------------------------------------------------------------------------
00245 SIO_verbosity SIO_streamManager::setVerbosity
00246 (
00247     SIO_verbosity   i_verb
00248 )
00249 { SIO_verbosity o_verb = verbosity; verbosity = i_verb; return( o_verb ); } 
00250 

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