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
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
00051
00052
00053
00054
00055
00056
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
00072
00073
00074
00075 template<class T>
00076 bool get(const std::string& name, T& value) {
00077
00078 ContainerType::iterator it = m_map.find(name);
00079
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
00087 return false;
00088 }
00089
00090
00091
00092
00093
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
00106
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
00119
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