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