TDbiSimFlagAssociation.cxx

Go to the documentation of this file.
00001 // $Id: TDbiSimFlagAssociation.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $
00002 ////////////////////////////////////////////////////////////////////////
00003 // TDbiSimFlagAssociation                                              //
00004 //                                                                    //
00005 // Package: TDbi (Database Interface).                                 //
00006 //                                                                    //
00007 // N. West 08/2003                                                    //
00008 //                                                                    //
00009 // Concept: Association of a particular SimFlag type with a list of   //
00010 //          SimFlag types.                                            //
00011 //                                                                    //
00012 // Purpose: To allow the DBI to choose an alternative SimFlag type    //
00013 //          when attempting to satisfy queries.  For example, allow   //
00014 //          MC data to use Data constants.                            //
00015 //                                                                    //
00016 ////////////////////////////////////////////////////////////////////////
00017 
00018 #include <iostream>
00019 #include <sstream>
00020 using std::ostringstream;
00021 
00022 #include <string>
00023 using std::string;
00024 #include <vector>
00025 using std::vector;
00026 
00027 #include "TDbiSimFlagAssociation.hxx"
00028 #include <TSK_DBI_Log.hxx>
00029 #include <MsgFormat.h>
00030 using std::endl;
00031 #include "TDbiRegistry.hxx"
00032 #include "UtilString.hxx"
00033 
00034 ClassImp(TDbiSimFlagAssociation)
00035 
00036 
00037 //   Definition of static data members
00038 //   *********************************
00039 
00040 
00041 const TDbiSimFlagAssociation* TDbiSimFlagAssociation::fgInstance = 0;
00042 
00043 // Definition of global functions (alphabetical order)
00044 // ***************************************************
00045 
00046 ostream& operator<<(ostream& s, const TDbiSimFlagAssociation& simFlagAss) {
00047   simFlagAss.Print(s);
00048   return s;
00049 }
00050 
00051 // Definition of member functions (alphabetical order)
00052 // ***************************************************
00053 
00054 //.....................................................................
00055 
00056 TDbiSimFlagAssociation::TDbiSimFlagAssociation() {
00057 //
00058 //
00059 //  Purpose:  Default constructor
00060 //
00061 //  Contact:   N. West
00062 //
00063 
00064 
00065   SK_DBI_Trace( "Creating TDbiSimFlagAssociation" << "  ");
00066 
00067   // Connect to global pointer;
00068   fgInstance = this;
00069 
00070 }
00071 
00072 
00073 //.....................................................................
00074 
00075 TDbiSimFlagAssociation::~TDbiSimFlagAssociation() {
00076 //
00077 //
00078 //  Purpose: Destructor
00079 //
00080 //  Contact:   N. West
00081 //
00082 
00083 
00084   SK_DBI_Trace( "Destroying TDbiSimFlagAssociation" << "  ");
00085 
00086   // Disconnect from global pointer;
00087   if ( fgInstance == this ) fgInstance = 0;
00088 
00089 }
00090 //.....................................................................
00091 
00092 TDbiSimFlagAssociation::SimList_t
00093      TDbiSimFlagAssociation::Get(const DbiSimFlag::SimFlag_t value)const {
00094 //
00095 //
00096 //  Purpose:  Return associated list
00097 //            or just list containing value if none.
00098 //
00099 
00100   SimMap_t::const_iterator itr = fAssociations.find(value);
00101   if ( itr != fAssociations.end() ) return itr->second;
00102   SimList_t l;
00103   l.push_back(value);
00104   return l;
00105 
00106 }
00107 
00108 //.....................................................................
00109 
00110 const TDbiSimFlagAssociation& TDbiSimFlagAssociation::Instance() {
00111 //
00112 //
00113 //  Purpose:  Get access to the one and only instance.
00114 //
00115 
00116 //  Program Notes:-
00117 //  =============
00118 
00119 //  If necessary, creates a TDbiSimFlagAssociation, but once
00120 //  TDbiDatabaseManager.hxxas been created, it's owned version
00121 //  will supersede it and orginal will be lost (leak).
00122 //  In practice this should never happen; TDbiDatabaseManager is
00123 //  the first significant object to be created.
00124 
00125   if ( ! fgInstance ) new TDbiSimFlagAssociation;
00126   // The act of creation will set fgInstance.
00127   return *fgInstance;
00128 
00129 }
00130 //.....................................................................
00131 
00132 void TDbiSimFlagAssociation::Print(ostream& s)const {
00133 //
00134 //
00135 //  Purpose:  Print self.
00136 
00137   s << "\n\nSimFlag Association Status:  ";
00138   if ( fAssociations.size() == 0 ) s <<"Not enabled" << endl;
00139   else {
00140     s << endl;
00141 
00142     SimMap_t::const_iterator mapItr    = fAssociations.begin();
00143     SimMap_t::const_iterator mapItrEnd = fAssociations.end();
00144     while ( mapItr != mapItrEnd ) {
00145 
00146       DbiSimFlag::SimFlag_t value = mapItr->first;
00147       string name = DbiSimFlag::AsString(value);
00148       ostringstream buff;
00149       buff << name << "(" << value << ")";
00150       name = buff.str();
00151       if ( name.size() < 20 ) name.append(20-name.size(),' ');
00152       s << name << "maps to: ";
00153 
00154       SimList_t l = mapItr->second;
00155       SimList_t::const_iterator listItr    = l.begin();
00156       SimList_t::const_iterator listItrEnd = l.end();
00157       while ( listItr != listItrEnd ) {
00158         DbiSimFlag::SimFlag_t v = *listItr;
00159         string n = DbiSimFlag::AsString(v);
00160         s << n << "(" << v << ")";
00161         ++listItr;
00162         if ( listItr != listItrEnd ) s << ", ";
00163       }
00164       s << endl;
00165       ++mapItr;
00166     }
00167 
00168   }
00169 }
00170 
00171 //.....................................................................
00172 
00173 void TDbiSimFlagAssociation::Set(TDbiRegistry& reg) {
00174 //
00175 //
00176 //  Purpose:  Extract SimFlag association lists from TDbiRegistry.
00177 //
00178 //  Arguments:
00179 //    reg          in    TDbiRegistry containing "SimFlagAssociation:..." keys.
00180 //                 out   Updated TDbiRegistry with these keys removed.
00181 //
00182 //  Contact:   N. West
00183 //
00184 //  Specification:-
00185 //  =============
00186 //
00187 //  o Extract SimFlag association lists from TDbiRegistry.
00188 
00189   TDbiRegistry::TDbiRegistryKey keyItr(&reg);
00190 
00191   Bool_t  hasChanged = kFALSE;
00192 
00193   const char* key = keyItr();
00194   while ( key ) {
00195 
00196     const char* nextKey =  keyItr();
00197     if ( ! strncmp("SimFlagAssociation:",key,19) ) {
00198 
00199       // Found a SimFlagAssociation key, extract its value.
00200       string Name = key+19;
00201       DbiSimFlag::SimFlag_t value = DbiSimFlag::StringToEnum(Name.c_str());
00202       const char* listChars = 0;
00203       bool ok = reg.Get(key,listChars) && (value != DbiSimFlag::kUnknown);
00204       // Collect the associated list
00205       SimList_t lv;
00206       if ( ok ) {
00207         vector<string> ls;
00208         UtilString::StringTok(ls,listChars,",");
00209         vector<string>::iterator itr    = ls.begin();
00210         vector<string>::iterator itrEnd = ls.end();
00211         for (; itr != itrEnd; ++itr ) {
00212           DbiSimFlag::SimFlag_t v = DbiSimFlag::StringToEnum(itr->c_str());
00213           if ( v == DbiSimFlag::kUnknown) ok = false;
00214           lv.push_back(v);
00215         }
00216       }
00217 
00218       if ( ok ) {
00219         this->Set(value,lv);
00220         hasChanged = true;
00221       }
00222       else  SK_DBI_Warn(  "Illegal SimFlagAssociation registry item: " << key
00223           << " = " << listChars << "  ");
00224 
00225       reg.RemoveKey(key);
00226     }
00227     key = nextKey;
00228   }
00229 
00230   if ( hasChanged ) this->Show();
00231 }
00232 
00233 //.....................................................................
00234 
00235 void TDbiSimFlagAssociation::Show() {
00236 //
00237 //
00238 //  Purpose:
00239 
00240   SK_DBI_Info( *this << "  ");
00241 
00242 
00243 }
00244 
00245 /*    Template for New Member Function
00246 
00247 //.....................................................................
00248 
00249 TDbiSimFlagAssociation:: {
00250 //
00251 //
00252 //  Purpose:
00253 //
00254 //  Arguments:
00255 //    xxxxxxxxx    in    yyyyyy
00256 //
00257 //  Return:
00258 //
00259 //  Contact:   N. West
00260 //
00261 //  Specification:-
00262 //  =============
00263 //
00264 //  o
00265 
00266 //  Program Notes:-
00267 //  =============
00268 
00269 //  None.
00270 
00271 
00272 }
00273 
00274 */
00275 
00276 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1