00001 00012 #ifdef HAVE_CONFIG_H 00013 #include "config.h" 00014 #endif 00015 00016 // for truncation warning in debug mode 00017 #ifdef _MSC_VER 00018 #include "msdevstudio/MSconfig.h" 00019 #endif 00020 00021 #include "DataSourceController.h" 00022 00023 #include "DataSource.h" 00024 #include "DataSourceException.h" 00025 00026 #include <algorithm> 00027 #include <fstream> 00028 00029 #ifdef SSTREAM_DEFECT 00030 #include <strstream> 00031 using std::ostrstream; 00032 #else 00033 #include <sstream> 00034 using std::ostringstream; 00035 #endif 00036 00037 #include <utility> 00038 #include <cassert> 00039 00040 using std::map; 00041 using std::string; 00042 using std::vector; 00043 00044 DataSourceController * DataSourceController::s_instance = 0; 00045 00046 DataSourceController::DataSourceController ( ) 00047 : m_base_name ( "in-memory" ), 00048 m_suffix ( 0 ) 00049 { 00050 } 00051 00054 DataSourceController::DataSourceController ( const DataSourceController & ) 00055 { 00056 assert ( false ); 00057 } 00058 00059 DataSourceController * DataSourceController::instance ( ) 00060 { 00061 if ( s_instance == 0 ) { 00062 s_instance = new DataSourceController ( ); 00063 } 00064 return s_instance; 00065 } 00066 00067 void 00068 DataSourceController:: 00069 registerDataSourceFile ( DataSource * ds ) 00070 { 00071 m_ds_files.push_back ( ds ); 00072 } 00073 00074 DataSource * 00075 DataSourceController:: 00076 findDataSource ( const std::string & name ) const 00077 throw ( DataSourceException ) 00078 { 00079 DataSource * source = getDataSource ( name ); 00080 00081 if ( source == 0 ) { 00082 string what ( "DataSourceController: No NTuple with name `" ); 00083 what += name; 00084 what += "' has been registered"; 00085 throw DataSourceException ( what ); 00086 } 00087 00088 return source; 00089 } 00090 00091 int 00092 DataSourceController:: 00093 indexOfDataSource ( const std::string & name ) const 00094 { 00095 int index = -1; 00096 DataSourceList_t::size_type size = m_tuples.size (); 00097 DataSourceList_t::size_type i = 0; 00098 00099 for ( ; i < size; i++ ) { 00100 DataSource * ds = m_tuples [ i ]; 00101 const string & ds_name = ds -> getName (); 00102 if ( ds_name == name ) { 00103 index = i; 00104 break; 00105 } 00106 } 00107 return index; 00108 } 00109 00110 DataSource * 00111 DataSourceController:: 00112 getDataSource ( const std::string & name ) const 00113 { 00114 DataSource * source = 0; 00115 00116 DataSourceList_t::size_type size = m_tuples.size (), i = 0; 00117 for ( ; i < size; i++ ) { 00118 DataSource * ds = m_tuples [ i ]; 00119 const string & ds_name = ds -> getName (); 00120 if ( ds_name == name ) { 00121 source = ds; 00122 break; 00123 } 00124 } 00125 00126 return source; 00127 } 00128 00129 void 00130 DataSourceController:: 00131 changeName ( const std::string & old_name, 00132 const std::string & new_name ) 00133 { 00134 DataSource * ntuple = getDataSource ( old_name ); 00135 if ( ntuple != 0 ) { 00136 ntuple -> setName ( new_name ); 00137 } 00138 } 00139 00140 const vector < DataSource * > & 00141 DataSourceController:: 00142 getDataSources ( bool all ) const 00143 { 00144 return m_tuples; 00145 } 00146 00147 const vector < string > & DataSourceController::getNTupleNames () const 00148 { 00149 m_names.clear (); 00150 00151 DataSourceList_t::const_iterator i = m_tuples.begin (); 00152 while ( i != m_tuples.end() ) { 00153 const DataSource * source = *i++; 00154 const string & name = source -> getName (); 00155 m_names.push_back ( name ); 00156 } 00157 00158 return m_names; 00159 } 00160 00161 string 00162 DataSourceController:: 00163 registerNTuple ( DataSource * ds ) 00164 { 00165 00166 string text = ds ->getName (); 00167 if ( text.empty () ) { 00168 #ifdef SSTREAM_DEFECT 00169 ostrstream strm_text; 00170 #else 00171 ostringstream strm_text; 00172 #endif 00173 strm_text << "<" << m_base_name << ++m_suffix << ">" << std::ends; 00174 text = strm_text.str(); 00175 } 00176 00177 ds->setName ( text ); 00178 registerNTuple ( text, ds ); 00179 00180 return text; 00181 } 00182 00183 void 00184 DataSourceController:: 00185 registerNTuple ( const std::string & key, DataSource * ntuple ) 00186 { 00187 DataSource * source = getDataSource ( key ); 00188 if ( source != 0 ) { 00189 string what ( "DataSourceController: NTuple with name:\n"); 00190 what += key; 00191 what += "\nhas already been registered"; 00192 throw DataSourceException ( what ); 00193 } 00194 ntuple -> addObserver ( this ); 00195 00196 m_tuples.push_back ( ntuple ); 00197 } 00198 00199 void 00200 DataSourceController:: 00201 unregisterNTuple ( const DataSource * ntuple ) 00202 { 00203 DataSourceList_t::iterator i 00204 = std::find ( m_tuples.begin(), m_tuples.end(), ntuple ); 00205 if ( i != m_tuples.end () ) { 00206 m_tuples.erase ( i ); 00207 } 00208 } 00209 00210 void 00211 DataSourceController:: 00212 update ( const Observable * obs ) 00213 { 00214 } 00215 00216 void 00217 DataSourceController:: 00218 willDelete ( const Observable * observee ) 00219 { 00220 const DataSource * ntuple 00221 = dynamic_cast < const DataSource * > ( observee ); 00222 if ( ntuple == 0 ) return; // not NTuple 00223 00224 unregisterNTuple ( ntuple ); 00225 } 00226 00227 bool 00228 DataSourceController:: 00229 isSaved ( const std::string & name ) 00230 { 00231 string::size_type i = name.find ( m_base_name ); 00232 bool not_saved = i != string::npos; 00233 00234 return ! not_saved; 00235 }