CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondCore/ORA/src/Selection.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Selection.h"
00002 #include "CondCore/ORA/interface/Exception.h"
00003 //
00004 #include <sstream>
00005 //
00006 #include "CoralBase/AttributeList.h"
00007 #include "CoralBase/Attribute.h"
00008 
00009 std::vector<std::string>& ora::Selection::selectionTypes(){
00010   static std::vector<std::string> types;
00011   types.push_back("=");
00012   types.push_back("!=");
00013   types.push_back(">");
00014   types.push_back(">=");
00015   types.push_back("<");
00016   types.push_back("<=");
00017   return types;
00018 }
00019 
00020 std::string  ora::Selection::variableNameFromUniqueString(const std::string& uniqueString)
00021 {
00022   size_t ind = uniqueString.rfind("_");
00023   return uniqueString.substr(0,ind);
00024 }
00025 
00026 std::string ora::Selection::indexVariable(){
00027   static std::string s_var("ora::ContainerIndex");
00028   return s_var;
00029 }
00030 
00031 ora::Selection::Selection():m_items(),m_data( new coral::AttributeList ){
00032 }
00033 
00034 ora::Selection::~Selection(){
00035 }
00036 
00037 ora::Selection::Selection( const ora::Selection& rhs ):
00038   m_items( rhs.m_items ),
00039   m_data( new coral::AttributeList( *rhs.m_data )){
00040 }
00041 
00042 ora::Selection& ora::Selection::operator=( const ora::Selection& rhs ){
00043   m_items = rhs.m_items;
00044   m_data.reset( new coral::AttributeList( *rhs.m_data ) );
00045   return *this;
00046 }
00047 
00048 std::string
00049 ora::Selection::uniqueVariableName(const std::string& varName) const {
00050   std::stringstream uniqueVarName;
00051   unsigned int i = 0;
00052   bool notUnique = true;
00053   while(notUnique){
00054     bool found = false;
00055     uniqueVarName.str("");
00056     uniqueVarName << varName;
00057     uniqueVarName << "_" << i;
00058     for(coral::AttributeList::const_iterator iAttr = m_data->begin();
00059         iAttr!=m_data->end() && !found; ++iAttr){
00060       if( iAttr->specification().name() == uniqueVarName.str() ) found = true;
00061     }
00062     notUnique = found;
00063     i++;
00064   }
00065   return uniqueVarName.str();
00066 }
00067 
00068 void ora::Selection::addIndexItem( int startIndex, 
00069                                    int endIndex ){
00070   if(endIndex<startIndex && endIndex>=0) {
00071     throwException("Cannot select with endIndex<startIndex.",
00072                    "Selection::addIndexItem");
00073   } else if( startIndex==endIndex && endIndex>=0){
00074     std::string varName = uniqueVariableName( indexVariable() );
00075     SelectionItemType selType = ora::EQ;
00076     m_items.push_back(std::make_pair(varName,selectionTypes()[selType]));
00077     m_data->extend<int>(varName);
00078     (*m_data)[varName].data<int>() = startIndex;    
00079   } else {
00080     if(startIndex>0){
00081       std::string varName0 = uniqueVariableName( indexVariable() );
00082       SelectionItemType firstType = ora::GE;
00083       m_items.push_back(std::make_pair(varName0,selectionTypes()[firstType]));
00084       m_data->extend<int>(varName0);
00085       (*m_data)[varName0].data<int>() = startIndex;
00086     }
00087     if(endIndex>0){
00088       std::string varName1 = uniqueVariableName( indexVariable() );
00089       SelectionItemType secondType = ora::LE;
00090       m_items.push_back(std::make_pair(varName1,selectionTypes()[secondType]));
00091       m_data->extend<int>(varName1);
00092       (*m_data)[varName1].data<int>() = endIndex;
00093     }
00094   }
00095 }
00096 
00097 void ora::Selection::addUntypedDataItem( const std::string& dataMemberName, 
00098                                           SelectionItemType stype, 
00099                                           const std::type_info& primitiveType, 
00100                                           void* data ){
00101   std::string varName = uniqueVariableName( dataMemberName );
00102   m_items.push_back(std::make_pair(varName,selectionTypes()[stype]));
00103   m_data->extend( varName, primitiveType );
00104   (*m_data)[varName].setValueFromAddress( data );
00105 }
00106 
00107 bool
00108 ora::Selection::isEmpty() const {
00109   return m_items.empty();
00110 }
00111 
00112 const std::vector<std::pair<std::string,std::string> >&
00113 ora::Selection::items() const {
00114   return m_items;
00115 }
00116 
00117 const coral::AttributeList&
00118 ora::Selection::data() const {
00119   return *m_data;
00120 }