/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/EventFilter/HltUtilities/HltStore/HltStore-01-00-04/HltStore/HltStoreSvc.h

Go to the documentation of this file.
00001 #ifndef HLTSTORESVC_H
00002 #define HLTSTORESVC_H
00003 
00004 #include <string>
00005 #include <stdio.h>
00006 #include <iostream>
00007 #include <map>
00008 
00009 #include "GaudiKernel/Service.h"
00010 #include "GaudiKernel/IInterface.h"
00011 
00012 using namespace std;
00013 
00014 static const InterfaceID  IID_IHltStoreSvc("IID_IHltStoreSvc", 1, 0);
00015 
00016 class HltStoreSvc :public Service , virtual public IInterface {
00017 
00018 public:
00019   
00020   HltStoreSvc(const std::string& name, ISvcLocator* sl);
00021   ~HltStoreSvc();
00022  
00023   virtual StatusCode queryInterface(const InterfaceID& riid, void** ppvIF);
00024   virtual StatusCode initialize ( );
00025   virtual StatusCode finalize ( );
00026 
00027 private:
00028   //HltStoreSvc* m_HltStore;
00029   
00030 public:
00031 
00032   static const InterfaceID& interfaceID() { return IID_IHltStoreSvc; }
00033 
00034  protected:
00035 
00036   class BaseHolder;
00037 
00038   typedef map<string, BaseHolder*> ContainerType;
00039 
00040 
00041  public:
00042 
00043   void printKeys();
00044   int size();
00045   int max_size();
00046   string sListLength();
00047   bool exists(const std::string& name);
00048 
00049   // 
00050   // since these are member template functions, we have to
00051   // put the whole implementation here
00052   //
00053   
00054   //
00055   // insert a named value into the store: put("name", data)
00056   // if the key "name" already exists, it is replaced.
00057   //
00058 
00059   template<class T>
00060   bool put(const std::string& name, const T& value) {
00061     Holder<T> *ptr = new Holder<T>(value);
00062     if(BaseHolder *old = m_map[name]) {
00063       std::cout << "demanded key already exists, overwrite old one" << std::endl;
00064       delete old;
00065     }
00066     m_map[name] = ptr;
00067     return true;
00068   }
00069   
00070   //
00071   // retrieve a named value from the store: get("name", value)
00072   // returns false if entry not found; in this case the input
00073   // object is unchanged
00074   //
00075   template<class T>
00076   bool get(const std::string& name, T& value) {
00077     //std::cout << "HltStoreSvc::get()  "<<"start"<<std::endl;
00078     ContainerType::iterator it = m_map.find(name);
00079     //std::cout << "HltStoreSvc::get()  "<<"middle"<<std::endl;
00080     if(it != m_map.end()) {
00081       if(Holder<T>* ptr = static_cast<Holder<T>* >((*it).second)) {
00082         value = ptr->value();
00083         return true;
00084       }
00085     } 
00086     //std::cout << "HltStoreSvc::get()  "<<"end"<<std::endl;
00087     return false;
00088   }
00089   
00090 
00091  //
00092   // erase an entry from the store; the corresponding object is
00093   // destructed automatically or deleted if a pointer
00094   //
00095   bool erase(const std::string& name) {
00096     ContainerType::iterator it = m_map.find(name);
00097     if(it != m_map.end()) {
00098       delete (*it).second;
00099       m_map.erase(it);
00100       return true;
00101     }
00102     return false;
00103   }
00104   //
00105   // clear the store; the corresponding objects are
00106   // destructed automatically or deleted if a pointer
00107   //
00108   bool clear(void) {
00109     for (ContainerType::iterator it = m_map.begin(); it != m_map.end(); it++) {
00110       delete (*it).second;
00111     }
00112     m_map.erase(m_map.begin(), m_map.end());
00113     return true;
00114   }
00115   
00116 protected:
00117 
00118   // BaseHolder base class allows the use of a single hash_map for all
00119   // templated holder classes.
00120 
00121   class BaseHolder {
00122   public:
00123     virtual ~BaseHolder() {};
00124   };
00125   
00126   template<class T>
00127   class Holder : public BaseHolder {
00128   public:
00129     Holder(const T& value) : m_value(value) {};
00130     ~Holder() { 
00131       erase(m_value); } 
00132     const T& value() const { return m_value; }
00133   private:
00134     template<class T1>
00135     void erase(T1 value) {}
00136     
00137     template<class T1>
00138     void erase(T1 *value) { 
00139       delete value; }
00140     
00141   private:
00142     T m_value; 
00143   };
00144   
00145   ContainerType m_map;
00146   
00147 
00148 };
00149 
00150 
00151 #endif

Generated on Tue Nov 29 23:12:07 2016 for BOSS_7.0.2 by  doxygen 1.4.7