00001 #ifndef ConditionsMap_h
00002 #define ConditionsMap_h
00003
00004 #include "lcio.h"
00005 #include "EVENT/LCCollection.h"
00006
00007 #include "lccd/IConditionsChangeListener.hh"
00008
00009 #include <map>
00010 #include <typeinfo>
00011 #include <sstream>
00012
00013 namespace lccd {
00014
00022 template <class KEY, class LCCONDOBJECT>
00023 class ConditionsMap : public lccd::IConditionsChangeListener {
00024
00025 public:
00026
00027 typedef typename std::map< KEY, LCCONDOBJECT >::iterator MapIter ;
00028
00029
00030 typedef KEY (LCCONDOBJECT::*PMF)() ;
00031
00032
00034 ConditionsMap( PMF pmf ) : _pmf(pmf) {
00035 }
00036
00037
00039 virtual ~ConditionsMap() {}
00040
00041
00043 const std::map< KEY, LCCONDOBJECT> & map() { return _map ; }
00044
00045
00048 LCCONDOBJECT& find( KEY key) {
00049
00050 MapIter it = _map.find( key ) ;
00051
00052 if( it == _map.end() ){
00053 std::stringstream err ;
00054 err << "ConditionsMap::find: no entry for key: " << key ;
00055 throw Exception( err.str() ) ;
00056 }
00057 else
00058 return it->second ;
00059 }
00060
00061
00064 void conditionsChanged( lcio::LCCollection* col ) {
00065
00066 _map.clear() ;
00067
00068
00069 for( int i=0; i< col->getNumberOfElements() ; i++ ){
00070
00071
00072 LCCONDOBJECT obj( col->getElementAt( i ) ) ;
00073
00074
00075 _map.insert( std::make_pair( (obj.*_pmf)() , obj) ) ;
00076
00077
00078 }
00079
00080
00081 }
00082
00087 void print( std::ostream& os ) {
00088
00089 os << "ConditionsMap"
00090
00091
00092 << std::endl ;
00093
00094
00095 for( MapIter it = _map.begin() ;
00096 it != _map.end() ;
00097 it++ ){
00098
00099 os << " key: " << it->first << " - [" << it->second.id() << "]"
00100 << " - " << typeid( it->second ).name()
00101 << std::endl ;
00102 }
00103
00104 }
00105
00106
00107 protected:
00108
00110 ConditionsMap() { }
00111
00112
00113
00114 std::map< KEY, LCCONDOBJECT > _map ;
00115 PMF _pmf ;
00116 };
00117
00118 } ;
00119
00120 #endif // ConditionsMap_h