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