DataSourceController.cxx
Go to the documentation of this file.
1 
12 // for truncation warning in debug mode
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "DataSourceController.h"
18 
19 #include "DataSource.h"
20 
21 #include "pattern/string_convert.h"
22 
23 #include <algorithm>
24 #include <fstream>
25 #include <utility>
26 
27 #include <cassert>
28 
29 using std::runtime_error;
30 using std::string;
31 using std::vector;
32 
33 using namespace hippodraw;
34 
36 
38  : m_base_name ( "in-memory" ),
39  m_suffix ( 0 ),
40  m_current_index ( -1 )
41 {
42 }
43 
48  : Observer ()
49 {
50  assert ( false );
51 }
52 
54 {
55  if ( s_instance == 0 ) {
57  }
58  return s_instance;
59 }
60 
61 void
64 {
65  m_ds_files.push_back ( ds );
66 }
67 
68 DataSource *
70 findDataSource ( const std::string & name ) const
71 // throw ( DataSourceException )
72  throw ( runtime_error )
73 {
74  DataSource * source = getDataSource ( name );
75 
76  if ( source == 0 ) {
77  string what ( "DataSourceController: No NTuple with name `" );
78  what += name;
79  what += "' has been registered";
80  throw runtime_error ( what );
81  }
82 
83  return source;
84 }
85 
86 int
88 indexOfDataSource ( const std::string & name ) const
89 {
90  int index = -1;
91  DataSourceList_t::size_type size = m_sources.size ();
92  DataSourceList_t::size_type i = 0;
93 
94  for ( ; i < size; i++ ) {
95  DataSource * ds = m_sources [ i ];
96  const string & ds_name = ds -> getName ();
97  if ( ds_name == name ) {
98  index = i;
99  break;
100  }
101  }
102 
103  return index;
104 }
105 
106 DataSource *
108 getDataSource ( const std::string & name ) const
109 {
110  DataSource * source = 0;
111 
112  int index = indexOfDataSource ( name );
113  if ( index >= 0 ) {
114  source = m_sources [ index ];
115  }
116 
117  return source;
118 }
119 
120 void
122 changeName ( const std::string & old_name,
123  const std::string & new_name )
124 {
125  DataSource * ntuple = getDataSource ( old_name );
126  if ( ntuple != 0 ) {
127  ntuple -> setName ( new_name );
128  }
129 }
130 
131 bool
133 isFromFile ( const DataSource * source ) const
134 {
135  DataSourceList_t::const_iterator first
136  = find ( m_ds_files.begin (), m_ds_files.end (), source );
137 
138  return first != m_ds_files.end ();
139 }
140 
141 void
143 getDataSources ( std::vector < DataSource * > & sources,
144  bool all ) const
145 {
146  sources.clear();
147  unsigned int size = m_sources.size ();
148 
149  for ( unsigned int i = 0; i < size; i++ ) {
150  DataSource * ds = m_sources [ i ];
151  if ( all == false ) {
152  bool yes = isFromFile ( ds );
153  if ( yes ) continue;
154  }
155  sources.push_back ( ds );
156  }
157 }
158 
159 
160 const vector < string > &
163 {
164  m_names.clear ();
165 
166  DataSourceList_t::const_iterator i = m_sources.begin ();
167  while ( i != m_sources.end() ) {
168  const DataSource * source = *i++;
169  const string & name = source -> getName ();
170  m_names.push_back ( name );
171  }
172 
173  return m_names;
174 }
175 
176 string
179 {
180  string text = ds ->getName ();
181  if ( text.empty () ) {
182  text += "<";
183  text += m_base_name;
184  text += String::convert ( ++m_suffix );
185  text += ">";
186  }
187 
188  ds->setName ( text );
189  registerNTuple ( text, ds );
190 
191  return text;
192 }
193 
194 void
196 registerNTuple ( const std::string & key, DataSource * ntuple )
197 {
198  ntuple -> setName ( key );
199  ntuple -> addObserver ( this );
200  m_sources.push_back ( ntuple );
201  m_current_index = m_sources.size () - 1;
202 }
203 
204 void
206 unregisterNTuple ( const DataSource * ntuple )
207 {
208  DataSourceList_t::iterator i
209  = std::find ( m_sources.begin(), m_sources.end(), ntuple );
210  if ( i != m_sources.end () ) {
211  m_sources.erase ( i );
212  }
213  int size = static_cast < int > ( m_sources.size() );
214  if ( m_current_index >= size ) {
215  m_current_index = m_sources.size () -1;
216  }
217 }
218 
219 void
221 update ( const Observable * )
222 {
223 }
224 
225 void
227 willDelete ( const Observable * observee )
228 {
229  const DataSource * ntuple
230  = dynamic_cast < const DataSource * > ( observee );
231  if ( ntuple == 0 ) return; // not NTuple
232 
233  unregisterNTuple ( ntuple );
234 }
235 
236 DataSource *
238 getCurrent () const
239 {
240  DataSource * source = 0;
241  if ( m_current_index >= 0 ) {
242  source = m_sources [ m_current_index ];
243  }
244 
245  return source;
246 }
247 
248 void
251 {
252  m_current_index = i;
253 }

Generated for HippoDraw Class Library by doxygen