ATLAS Offline Software
Loading...
Searching...
No Matches
DbAccessObj.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5//====================================================================
6// DbAccessObj Base class definitions
7//--------------------------------------------------------------------
8//
9// Package : StorageSvc (The POOL project)
10// @author M.Frank
11//====================================================================
12#ifndef POOL_DBACCESSOBJ_H
13#define POOL_DBACCESSOBJ_H 1
14
15// Framework include files
16#include "StorageSvc/pool.h"
17#include "StorageSvc/DbType.h"
19#include "GaudiKernel/StatusCode.h"
20
21// STL include files
22#include <map>
23#include <utility>
24#include <atomic>
25
26// #define DEBUG_REFCOUNTS
27#ifdef DEBUG_REFCOUNTS
28# include<iostream>
29#endif
30
31/*
32 * POOL namespace declaration
33 */
34namespace pool {
35
47 template <class KEY, class TYPE> class DbAccessObj {
48 public:
51 typedef std::map< KEY, TYPE* > Keys;
52 typedef typename Keys::iterator iterator;
53 typedef typename Keys::const_iterator const_iterator;
54 private:
56 mutable std::atomic<int> m_refCount;
60 std::string m_name;
67
68 public:
70 DbAccessObj(const std::string& n, DbAccessMode m, const DbType& t, IOODatabase* s=0)
71 : m_refCount(0), m_mode(m), m_name(n), m_type(t), m_pool(s)
72 { if( m_pool ) m_pool->addRef(); }
73
74 virtual ~DbAccessObj() { releasePtr( m_pool ); }
76 const std::string& name() const { return m_name; }
78 void setName(const std::string& n){ m_name = n; }
80 DbAccessMode mode() const { return m_mode; }
82 void setMode(DbAccessMode m) { m_mode = m; }
83 // Inline functions
84 const DbType& type() const { return m_type; }
86 const IOODatabase* db() const { return m_pool; }
87 IOODatabase* db() { return m_pool; }
89 int refCount() const { return m_refCount; }
91 int addRef() const {
92#ifdef DEBUG_REFCOUNTS
93 std::cout << typeid(*this).name()
94 << " "
95 << m_name
96 << " Refcount:"
97 << m_refCount+1
98 << std::endl;
99#endif
100 return ++m_refCount;
101 }
102
103 int release() const {
104 int count = --m_refCount;
105#ifdef DEBUG_REFCOUNTS
106 std::cout << typeid(*this).name()
107 << " "
108 << m_name
109 << " Refcount:"
110 << count
111 << std::endl;
112#endif
113 if ( count == 0 ) {
114 delete this;
115 }
116 return count;
117 }
118
119 size_t size() const {
120 return m_keys.size();
121 }
122
124 for( const auto & k : m_keys ) k.second->release();
125 m_keys.clear();
126 }
127
128 const TYPE* find(const KEY& key) const {
129 const_iterator i = m_keys.find(key);
130 return (m_keys.end() == i) ? 0 : (*i).second;
131 }
132
133 TYPE* find(const KEY& key) {
134 iterator i = m_keys.find(key);
135 return (m_keys.end() == i) ? 0 : (*i).second;
136 }
137
138 StatusCode add(const KEY& key, TYPE* val) {
139 iterator i = m_keys.find(key);
140 if ( m_keys.end() == i ) {
141 m_keys.insert(std::make_pair(key, val));
142 val->addRef();
143 return StatusCode::SUCCESS;
144 }
145 return StatusCode::FAILURE;
146 }
147
148 StatusCode remove(const TYPE* val) {
149 for (iterator j = m_keys.begin(); j != m_keys.end(); ++j ) {
150 if ( (*j).second == val ) {
151 TYPE* p = (*j).second;
152 m_keys.erase(j);
153 p->release();
154 return StatusCode::SUCCESS;
155 }
156 }
157 return StatusCode::FAILURE;
158 }
159 iterator begin() { return m_keys.begin(); }
160 const_iterator begin() const { return m_keys.begin(); }
161 iterator end() { return m_keys.end(); }
162 const_iterator end() const { return m_keys.end(); }
163 };
164} // End namespace pool
165#endif // POOL_DBACCESSOBJ_H
#define TYPE(CODE, TYP, IOTYP)
const_iterator end() const
std::map< std::string, DbContainerObj * > Keys
Definition DbAccessObj.h:51
const_iterator begin() const
DbAccessObj< std::string, DbContainerObj > Base
Definition DbAccessObj.h:50
int addRef() const
Add reference count.
Definition DbAccessObj.h:91
DbAccessMode mode() const
Access mode.
Definition DbAccessObj.h:80
void setMode(DbAccessMode m)
Set Access mode.
Definition DbAccessObj.h:82
const std::string & name() const
Access the instance name.
Definition DbAccessObj.h:76
virtual ~DbAccessObj()
Standard destructor.
Definition DbAccessObj.h:74
StatusCode remove(const TYPE *val)
Remove entry from container.
StatusCode add(const KEY &key, TYPE *val)
Add entry to container.
const TYPE * find(const KEY &key) const
Find object by key (CONST)
int refCount() const
Access reference counter.
Definition DbAccessObj.h:89
void clearEntries()
Object cleanup: remove all entries.
const DbType & type() const
Definition DbAccessObj.h:84
void setName(const std::string &n)
Access the instance name.
Definition DbAccessObj.h:78
const IOODatabase * db() const
Allow access to the Database implementation.
Definition DbAccessObj.h:86
int release() const
Remove reference count.
TYPE * find(const KEY &key)
Find object by key.
DbAccessObj(const std::string &n, DbAccessMode m, const DbType &t, IOODatabase *s=0)
Constructor with initializing arguments.
Definition DbAccessObj.h:70
IOODatabase * db()
Definition DbAccessObj.h:87
size_t size() const
Object size.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
pool namespace
Definition libname.h:15
void releasePtr(T *&p)
Release a pointer.