emcDataStorageMap.C

Go to the documentation of this file.
00001 #include "emcDataStorageMap.h"
00002 
00003 #include <string>
00004 #include <vector>
00005 
00006 namespace
00007 {
00008   std::vector<std::string> split(const std::string& x, char sep)
00009   {
00010     std::string str = x;
00011 
00012     std::vector<std::string> rv;
00013     std::vector<int> pos;
00014 
00015     if ( str[0] != sep ) 
00016     { 
00017       str.insert(str.begin(),sep);
00018     }
00019 
00020     if ( str[str.size()-1] != sep ) 
00021     {
00022       str.push_back(sep);
00023     }
00024 
00025 
00026     for ( std::string::size_type i = 0; i < str.size(); ++i )
00027       {
00028         if ( str[i] == sep )
00029           {
00030             pos.push_back(i);
00031           }
00032       }
00033 
00034     if ( pos.size() > 0 )
00035       {
00036         for ( std::vector<int>::size_type i = 0; i < pos.size()-1; ++i )
00037           {
00038             rv.push_back(str.substr(pos[i]+1,pos[i+1]-pos[i]-1));
00039           }
00040       }
00041     return rv;
00042   }
00043 }
00044 
00045 //_____________________________________________________________________________
00046 emcDataStorageMap::emcDataStorageMap(emcManageable::EStorage def)
00047   : fDefaultStorage(def)
00048 {
00049 }
00050 
00051 //_____________________________________________________________________________
00052 void
00053 emcDataStorageMap::clear()
00054 {
00055   fMap.clear();
00056 }
00057 
00058 //_____________________________________________________________________________
00059 bool
00060 emcDataStorageMap::empty() const
00061 {
00062   return fMap.empty();
00063 }
00064 
00065 //_____________________________________________________________________________
00066 std::vector<std::string>
00067 emcDataStorageMap::knownStorages() const
00068 {
00069   std::vector<std::string> rv;
00070 
00071   TMAP::const_iterator it;
00072   TMAP::const_iterator end = fMap.end();
00073   for ( it = fMap.begin(); it != end; ++it )
00074     {
00075       rv.push_back(it->first);
00076     }  
00077   return rv;
00078 }
00079 
00080 //_____________________________________________________________________________
00081 bool
00082 emcDataStorageMap::parse(const std::string& x)
00083 {
00084   bool error = false;
00085   emcDataStorageMap rv;
00086   std::vector<std::string> pairs = split(x,',');
00087   for ( size_t i = 0; i < pairs.size(); ++i )
00088     {
00089       // check the pairs
00090       std::vector<std::string> p = split(pairs[i],':');
00091       if ( p.size() != 2 )
00092         {
00093           error = true;
00094           break;
00095         }
00096       else
00097         {
00098           rv.storage(p[0],p[1]);
00099         }
00100     }  
00101   if (error)
00102     {
00103       return false;
00104     }
00105   else
00106     {
00107       *this = rv;
00108       return true;
00109     }
00110 }
00111 
00112 //_____________________________________________________________________________
00113 emcManageable::EStorage
00114 emcDataStorageMap::storage() const
00115 {
00116   return fDefaultStorage;
00117 }
00118 
00119 //_____________________________________________________________________________
00120 emcManageable::EStorage
00121 emcDataStorageMap::storage(const std::string& what) const
00122 {
00123   if ( empty() ) return fDefaultStorage;
00124   
00125   TMAP::const_iterator it = fMap.find(what);
00126   if ( it != fMap.end() )
00127     {
00128       return it->second;
00129     }
00130   else
00131     {
00132       return emcManageable::kNone;
00133     }
00134 }
00135 
00136 //_____________________________________________________________________________
00137 emcManageable::EStorage
00138 emcDataStorageMap::storage(const char* what) const
00139 {
00140   return storage(std::string(what));
00141 }
00142 
00143 //_____________________________________________________________________________
00144 bool
00145 emcDataStorageMap::storage(const std::string& what, 
00146                            const std::string& storagename)
00147 {
00148   emcManageable::EStorage value = emcManageable::GetStorage(storagename.c_str());
00149   TMAP::const_iterator it = fMap.find(what);
00150   if ( it == fMap.end() )
00151     {
00152       return fMap.insert(TMAP::value_type(what,value)).second;
00153     }
00154   else
00155     {
00156       emcManageable::EStorage old = it->second;
00157       fMap[what] = value;
00158       if ( old != storage(what) )
00159         {
00160           std::cout << "<WARNING> emcDataStorageMap::storage(" << what
00161                     << "," << storagename << ") : changing from "
00162                     << emcManageable::GetStorageName(old) << " to " 
00163                     << emcManageable::GetStorageName(storage(what))
00164                     << std::endl;
00165         }
00166     }
00167   return true;
00168 }
00169 
00170 //_____________________________________________________________________________
00171 bool
00172 emcDataStorageMap::storage(const char* what, const char* storagename)
00173 {
00174   return storage(std::string(what),std::string(storagename));
00175 }
00176 
00177 //_____________________________________________________________________________
00178 void
00179 emcDataStorageMap::print(std::ostream& out) const
00180 {
00181   TMAP::const_iterator it;
00182   TMAP::const_iterator end = fMap.end();
00183   for ( it = fMap.begin(); it != end; ++it )
00184     {     
00185       out << it->first << " : " 
00186           << emcManageable::GetStorageName(it->second)
00187           << " (" << it->second << ")" << std::endl;
00188     }
00189 }
00190 
00191 //_____________________________________________________________________________
00192 std::ostream& operator<<(std::ostream& out, const emcDataStorageMap& d)
00193 {
00194   d.print(out);
00195   return out;
00196 }