// $Id: IDbiSimFlagAssociation.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $ //////////////////////////////////////////////////////////////////////// // COMET::IDbiSimFlagAssociation // // // // Package: COMET::IDbi (Database Interface). // // // // N. West 08/2003 // // // // Concept: Association of a particular SimFlag type with a list of // // SimFlag types. // // // // Purpose: To allow the DBI to choose an alternative SimFlag type // // when attempting to satisfy queries. For example, allow // // MC data to use Data constants. // // // //////////////////////////////////////////////////////////////////////// #include #include using std::ostringstream; #include using std::string; #include using std::vector; #include "IDbiSimFlagAssociation.hxx" #include #include using std::endl; #include "IDbiRegistry.hxx" #include "UtilString.hxx" ClassImp(COMET::IDbiSimFlagAssociation) // Definition of static data members // ********************************* const COMET::IDbiSimFlagAssociation* COMET::IDbiSimFlagAssociation::fgInstance = 0; // Definition of global functions (alphabetical order) // *************************************************** ostream& operator<<(ostream& s, const COMET::IDbiSimFlagAssociation& simFlagAss) { simFlagAss.Print(s); return s; } // Definition of member functions (alphabetical order) // *************************************************** //..................................................................... COMET::IDbiSimFlagAssociation::IDbiSimFlagAssociation() { // // // Purpose: Default constructor // // Contact: N. West // COMETTrace( "Creating COMET::IDbiSimFlagAssociation" << " "); // Connect to global pointer; fgInstance = this; } //..................................................................... COMET::IDbiSimFlagAssociation::~IDbiSimFlagAssociation() { // // // Purpose: Destructor // // Contact: N. West // COMETTrace( "Destroying COMET::IDbiSimFlagAssociation" << " "); // Disconnect from global pointer; if ( fgInstance == this ) fgInstance = 0; } //..................................................................... COMET::IDbiSimFlagAssociation::SimList_t COMET::IDbiSimFlagAssociation::Get(const COMET::DbiSimFlag::SimFlag_t value)const { // // // Purpose: Return associated list // or just list containing value if none. // SimMap_t::const_iterator itr = fAssociations.find(value); if ( itr != fAssociations.end() ) return itr->second; SimList_t l; l.push_back(value); return l; } //..................................................................... const COMET::IDbiSimFlagAssociation& COMET::IDbiSimFlagAssociation::Instance() { // // // Purpose: Get access to the one and only instance. // // Program Notes:- // ============= // If necessary, creates a COMET::IDbiSimFlagAssociation, but once // IDbiDatabaseManager.hxxas been created, it's owned version // will supersede it and orginal will be lost (leak). // In practice this should never happen; COMET::IDbiDatabaseManager is // the first significant object to be created. if ( ! fgInstance ) new COMET::IDbiSimFlagAssociation; // The act of creation will set fgInstance. return *fgInstance; } //..................................................................... void COMET::IDbiSimFlagAssociation::Print(ostream& s)const { // // // Purpose: Print self. s << "\n\nSimFlag Association Status: "; if ( fAssociations.size() == 0 ) s <<"Not enabled" << endl; else { s << endl; SimMap_t::const_iterator mapItr = fAssociations.begin(); SimMap_t::const_iterator mapItrEnd = fAssociations.end(); while ( mapItr != mapItrEnd ) { COMET::DbiSimFlag::SimFlag_t value = mapItr->first; string name = COMET::DbiSimFlag::AsString(value); ostringstream buff; buff << name << "(" << value << ")"; name = buff.str(); if ( name.size() < 20 ) name.append(20-name.size(),' '); s << name << "maps to: "; SimList_t l = mapItr->second; SimList_t::const_iterator listItr = l.begin(); SimList_t::const_iterator listItrEnd = l.end(); while ( listItr != listItrEnd ) { COMET::DbiSimFlag::SimFlag_t v = *listItr; string n = COMET::DbiSimFlag::AsString(v); s << n << "(" << v << ")"; ++listItr; if ( listItr != listItrEnd ) s << ", "; } s << endl; ++mapItr; } } } //..................................................................... void COMET::IDbiSimFlagAssociation::Set(IDbiRegistry& reg) { // // // Purpose: Extract SimFlag association lists from IDbiRegistry. // // Arguments: // reg in IDbiRegistry containing "SimFlagAssociation:..." keys. // out Updated IDbiRegistry with these keys removed. // // Contact: N. West // // Specification:- // ============= // // o Extract SimFlag association lists from IDbiRegistry. IDbiRegistry::IDbiRegistryKey keyItr(®); Bool_t hasChanged = kFALSE; const char* key = keyItr(); while ( key ) { const char* nextKey = keyItr(); if ( ! strncmp("SimFlagAssociation:",key,19) ) { // Found a SimFlagAssociation key, extract its value. string Name = key+19; COMET::DbiSimFlag::SimFlag_t value = COMET::DbiSimFlag::StringToEnum(Name.c_str()); const char* listChars = 0; bool ok = reg.Get(key,listChars) && (value != COMET::DbiSimFlag::kUnknown); // Collect the associated list SimList_t lv; if ( ok ) { vector ls; COMET::UtilString::StringTok(ls,listChars,","); vector::iterator itr = ls.begin(); vector::iterator itrEnd = ls.end(); for (; itr != itrEnd; ++itr ) { COMET::DbiSimFlag::SimFlag_t v = COMET::DbiSimFlag::StringToEnum(itr->c_str()); if ( v == COMET::DbiSimFlag::kUnknown) ok = false; lv.push_back(v); } } if ( ok ) { this->Set(value,lv); hasChanged = true; } else COMETWarn( "Illegal SimFlagAssociation registry item: " << key << " = " << listChars << " "); reg.RemoveKey(key); } key = nextKey; } if ( hasChanged ) this->Show(); } //..................................................................... void COMET::IDbiSimFlagAssociation::Show() { // // // Purpose: COMETInfo( *this << " "); } /* Template for New Member Function //..................................................................... COMET::IDbiSimFlagAssociation:: { // // // Purpose: // // Arguments: // xxxxxxxxx in yyyyyy // // Return: // // Contact: N. West // // Specification:- // ============= // // o // Program Notes:- // ============= // None. } */