BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
HltStoreSvc.h
Go to the documentation of this file.
1#ifndef HLTSTORESVC_H
2#define HLTSTORESVC_H
3
4#include <string>
5#include <stdio.h>
6#include <iostream>
7#include <map>
8
9#include "GaudiKernel/Service.h"
10#include "GaudiKernel/IInterface.h"
11
12using namespace std;
13
14static const InterfaceID IID_IHltStoreSvc("IID_IHltStoreSvc", 1, 0);
15
16class HltStoreSvc :public Service , virtual public IInterface {
17
18public:
19
20 HltStoreSvc(const std::string& name, ISvcLocator* sl);
22
23 virtual StatusCode queryInterface(const InterfaceID& riid, void** ppvIF);
24 virtual StatusCode initialize ( );
25 virtual StatusCode finalize ( );
26
27private:
28 //HltStoreSvc* m_HltStore;
29
30public:
31
32 static const InterfaceID& interfaceID() { return IID_IHltStoreSvc; }
33
34 protected:
35
36 class BaseHolder;
37
38 typedef map<string, BaseHolder*> ContainerType;
39
40
41 public:
42
43 void printKeys();
44 int size();
45 int max_size();
46 string sListLength();
47 bool exists(const std::string& name);
48
49 //
50 // since these are member template functions, we have to
51 // put the whole implementation here
52 //
53
54 //
55 // insert a named value into the store: put("name", data)
56 // if the key "name" already exists, it is replaced.
57 //
58
59 template<class T>
60 bool put(const std::string& name, const T& value) {
61 Holder<T> *ptr = new Holder<T>(value);
62 if(BaseHolder *old = m_map[name]) {
63 std::cout << "demanded key already exists, overwrite old one" << std::endl;
64 delete old;
65 }
66 m_map[name] = ptr;
67 return true;
68 }
69
70 //
71 // retrieve a named value from the store: get("name", value)
72 // returns false if entry not found; in this case the input
73 // object is unchanged
74 //
75 template<class T>
76 bool get(const std::string& name, T& value) {
77 //std::cout << "HltStoreSvc::get() "<<"start"<<std::endl;
78 ContainerType::iterator it = m_map.find(name);
79 //std::cout << "HltStoreSvc::get() "<<"middle"<<std::endl;
80 if(it != m_map.end()) {
81 if(Holder<T>* ptr = static_cast<Holder<T>* >((*it).second)) {
82 value = ptr->value();
83 return true;
84 }
85 }
86 //std::cout << "HltStoreSvc::get() "<<"end"<<std::endl;
87 return false;
88 }
89
90
91 //
92 // erase an entry from the store; the corresponding object is
93 // destructed automatically or deleted if a pointer
94 //
95 bool erase(const std::string& name) {
96 ContainerType::iterator it = m_map.find(name);
97 if(it != m_map.end()) {
98 delete (*it).second;
99 m_map.erase(it);
100 return true;
101 }
102 return false;
103 }
104 //
105 // clear the store; the corresponding objects are
106 // destructed automatically or deleted if a pointer
107 //
108 bool clear(void) {
109 for (ContainerType::iterator it = m_map.begin(); it != m_map.end(); it++) {
110 delete (*it).second;
111 }
112 m_map.erase(m_map.begin(), m_map.end());
113 return true;
114 }
115
116protected:
117
118 // BaseHolder base class allows the use of a single hash_map for all
119 // templated holder classes.
120
122 public:
123 virtual ~BaseHolder() {};
124 };
125
126 template<class T>
127 class Holder : public BaseHolder {
128 public:
129 Holder(const T& value) : m_value(value) {};
131 erase(m_value); }
132 const T& value() const { return m_value; }
133 private:
134 template<class T1>
135 void erase(T1 value) {}
136
137 template<class T1>
138 void erase(T1 *value) {
139 delete value; }
140
141 private:
142 T m_value;
143 };
144
146
147
148};
149
150
151#endif
const T & value() const
Definition: HltStoreSvc.h:132
Holder(const T &value)
Definition: HltStoreSvc.h:129
map< string, BaseHolder * > ContainerType
Definition: HltStoreSvc.h:38
void printKeys()
Definition: HltStoreSvc.cxx:66
bool put(const std::string &name, const T &value)
Definition: HltStoreSvc.h:60
string sListLength()
Definition: HltStoreSvc.cxx:86
static const InterfaceID & interfaceID()
Definition: HltStoreSvc.h:32
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvIF)
Definition: HltStoreSvc.cxx:21
virtual StatusCode finalize()
Definition: HltStoreSvc.cxx:53
bool get(const std::string &name, T &value)
Definition: HltStoreSvc.h:76
ContainerType m_map
Definition: HltStoreSvc.h:145
bool erase(const std::string &name)
Definition: HltStoreSvc.h:95
bool clear(void)
Definition: HltStoreSvc.h:108
bool exists(const std::string &name)
virtual StatusCode initialize()
Definition: HltStoreSvc.cxx:32
int max_size()
Definition: HltStoreSvc.cxx:77