Print

Print


Commit in lcio/src/cpp on MAIN
include/UTIL/CollectionParameterMap.h+68added 1.1
src/UTIL/CollectionParameterMap.cc+96added 1.1
+164
2 added files
new comvenient class for handling maps encoded in collection parameters

lcio/src/cpp/include/UTIL
CollectionParameterMap.h added at 1.1
diff -N CollectionParameterMap.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CollectionParameterMap.h	30 May 2008 13:26:16 -0000	1.1
@@ -0,0 +1,68 @@
+#ifndef CollectionParameterMap_h
+#define CollectionParameterMap_h 1
+
+#include "EVENT/LCCollection.h"
+
+#include <map>
+#include <string>
+
+using namespace EVENT;
+
+
+namespace UTIL{
+  
+  
+  /** Helper class for setting and retrieving an std::map<string,int> as collection parameters.
+   *  Depending on which constructor was used the collection parameters will be updated if
+   *  an object of this class goes out of scope. The corresponding collection parameters will
+   *  hold the current state of the map. If the a const LCCollection* was specified (read use case)
+   *  no parameters will be updated.
+   *
+   *  @see LCCollection::getParameters()
+   *  @see LCParameters
+   *  @author F.Gaede, DESY
+   *  @version $Id: CollectionParameterMap.h,v 1.1 2008/05/30 13:26:16 gaede Exp $
+   */
+  class CollectionParameterMap {
+
+  public:  
+    
+    typedef  std::map< std::string, int >  map_type ;
+
+    /** Create CollectionParameterMap for given collection - read the collection parameters
+     *  keyName and valueName if they exist.
+     */
+    CollectionParameterMap( const std::string& keyName ,  const std::string& valueName,  LCCollection* col ) ;
+    
+    /** Create CollectionParameterMap for given collection - read the collection parameters
+     *  keyName and valueName if they exist.
+     */
+    CollectionParameterMap( const std::string& keyName ,  const std::string& valueName,  const LCCollection* col ) ;
+
+    /** Update the collection parameters keyName and valueName when going out of scope.
+     */
+    ~CollectionParameterMap() ;
+
+    /** The std::map< std::string, int >.
+     */
+    map_type& map() { return _map ; }
+
+
+  protected:
+
+    CollectionParameterMap();
+    
+    void init( const LCCollection* col ) ; 
+ 
+    std::string _keyName ;
+    std::string _valueName ;
+    LCCollection* _col ;
+    map_type _map ;
+
+
+  } ; 
+  
+} // namespace
+#endif
+
+

lcio/src/cpp/src/UTIL
CollectionParameterMap.cc added at 1.1
diff -N CollectionParameterMap.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CollectionParameterMap.cc	30 May 2008 13:26:16 -0000	1.1
@@ -0,0 +1,96 @@
+#include "UTIL/CollectionParameterMap.h"
+#include "EVENT/LCParameters.h"
+#include<sstream>
+
+#include <iostream>
+
+
+namespace UTIL{
+
+  CollectionParameterMap::CollectionParameterMap( const std::string& keyName ,  
+						  const std::string& valueName,  
+						  LCCollection* col ) :
+    _keyName( keyName ) ,
+    _valueName( valueName ) ,
+    _col( col ) {
+
+    init( col ) ;
+
+  }
+
+  CollectionParameterMap::CollectionParameterMap( const std::string& keyName ,  
+						  const std::string& valueName,  
+						  const LCCollection* col ) :
+    _keyName( keyName ) ,
+    _valueName( valueName ) ,
+    _col( 0 ) { // if const we don't update
+
+    init( col ) ;
+  }
+
+  void CollectionParameterMap::init( const LCCollection* col ){
+    
+    StringVec keys ;
+    
+    col->getParameters().getStringVals( _keyName , keys ) ;
+    
+    IntVec values ;
+    
+    col->getParameters().getIntVals( _valueName , values ) ;
+    
+    
+    // ------ do some checks: -------------------------
+    if( keys.size() != values.size() ) {
+      
+      std::stringstream sstr ;
+      
+      sstr << " CollectionParameterMap::init() - inconsistent parameter sizes for "
+	   <<  _keyName  << " [" << keys.size() << "] and " 
+	   <<  _valueName << " [" <<  values.size() << "] " ;
+      
+      throw Exception( sstr.str() ) ;
+    }
+    
+    unsigned nKeys =  keys.size() ;
+    for(unsigned i=0;i<nKeys;++i){
+      
+      _map.insert( std::make_pair( keys[i] , values[i] ) ) ;
+    }
+    
+  }
+
+
+  CollectionParameterMap::~CollectionParameterMap() {
+
+
+    if( _col != 0 ){
+      
+      StringVec keys ;
+      IntVec values ;
+      
+      for(map_type::iterator it= _map.begin() ; it!=_map.end() ;++it){
+	
+	keys.push_back( it->first ) ;
+	values.push_back( it->second ) ;
+	
+	//       std::cout << "   CollectionParameterMap::~CollectionParameterMap  - " 
+	// 		<<    it->first << " mapped to : " << it->second
+	// 		<< std::endl ;
+      }
+      
+      _col->parameters().setValues( _keyName , keys ) ;
+      _col->parameters().setValues( _valueName , values ) ;
+      
+      
+      //       std::cout << "   CollectionParameterMap::~CollectionParameterMap  - " 
+      // 		<< " setValues(  " <<  _keyName << " ,...) and " << keys.size()
+      // 		<< " setValues(  " <<  _valueName << " ,...) "<< values.size()
+      // 		<< std::endl ;
+    }
+    
+  }
+
+
+
+
+}
CVSspam 0.2.8