ATLAS Offline Software
Loading...
Searching...
No Matches
DbAccessObj.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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
50 private:
52 mutable std::atomic<int> m_refCount;
54 Io::IoFlag m_mode;
56 std::string m_name;
61
62 public:
64 DbAccessObjBase(const std::string& n, Io::IoFlag m, const DbType& t, IOODatabase* s=0)
65 : m_refCount(0), m_mode(m), m_name(n), m_type(t), m_pool(s)
66 { if( m_pool ) m_pool->addRef(); }
67
70 const std::string& name() const { return m_name; }
72 void setName(const std::string& n){ m_name = n; }
74 Io::IoFlag mode() const { return m_mode; }
76 void setMode(Io::IoFlag m) { m_mode = m; }
77 // Inline functions
78 const DbType& type() const { return m_type; }
80 const IOODatabase* db() const { return m_pool; }
81 IOODatabase* db() { return m_pool; }
83 int refCount() const { return m_refCount; }
85 int addRef() const {
86#ifdef DEBUG_REFCOUNTS
87 std::cout << typeid(*this).name()
88 << " "
89 << m_name
90 << " Refcount:"
91 << m_refCount+1
92 << std::endl;
93#endif
94 return ++m_refCount;
95 }
96
97 int release() const {
98 int count = --m_refCount;
99#ifdef DEBUG_REFCOUNTS
100 std::cout << typeid(*this).name()
101 << " "
102 << m_name
103 << " Refcount:"
104 << count
105 << std::endl;
106#endif
107 if ( count == 0 ) {
108 delete this;
109 }
110 return count;
111 }
112 };
113
114
115 template <class KEY, class TYPE> class DbAccessObj : public DbAccessObjBase {
116 public:
119 typedef std::map< KEY, TYPE* > Keys;
120 typedef typename Keys::iterator iterator;
121 typedef typename Keys::const_iterator const_iterator;
122
123 private:
126
127 public:
130
132 size_t size() const {
133 return m_keys.size();
134 }
135
137 for( const auto & k : m_keys ) k.second->release();
138 m_keys.clear();
139 }
140
141 const TYPE* find(const KEY& key) const {
142 const_iterator i = m_keys.find(key);
143 return (m_keys.end() == i) ? 0 : (*i).second;
144 }
145
146 TYPE* find(const KEY& key) {
147 iterator i = m_keys.find(key);
148 return (m_keys.end() == i) ? 0 : (*i).second;
149 }
150
151 StatusCode add(const KEY& key, TYPE* val) {
152 iterator i = m_keys.find(key);
153 if ( m_keys.end() == i ) {
154 m_keys.insert(std::make_pair(key, val));
155 val->addRef();
156 return StatusCode::SUCCESS;
157 }
158 return StatusCode::FAILURE;
159 }
160
161 StatusCode remove(const TYPE* val) {
162 for (iterator j = m_keys.begin(); j != m_keys.end(); ++j ) {
163 if ( (*j).second == val ) {
164 TYPE* p = (*j).second;
165 m_keys.erase(j);
166 p->release();
167 return StatusCode::SUCCESS;
168 }
169 }
170 return StatusCode::FAILURE;
171 }
172 iterator begin() { return m_keys.begin(); }
173 const_iterator begin() const { return m_keys.begin(); }
174 iterator end() { return m_keys.end(); }
175 const_iterator end() const { return m_keys.end(); }
176 };
177} // End namespace pool
178#endif // POOL_DBACCESSOBJ_H
#define TYPE(CODE, TYP, IOTYP)
DbType m_type
Database type.
Definition DbAccessObj.h:58
Io::IoFlag m_mode
Access mode.
Definition DbAccessObj.h:54
IOODatabase * m_pool
Pointer to specific pool implementation.
Definition DbAccessObj.h:60
void setMode(Io::IoFlag m)
Set Access mode.
Definition DbAccessObj.h:76
int refCount() const
Access reference counter.
Definition DbAccessObj.h:83
const DbType & type() const
Definition DbAccessObj.h:78
virtual ~DbAccessObjBase()
Standard destructor.
Definition DbAccessObj.h:68
const IOODatabase * db() const
Allow access to the Database implementation.
Definition DbAccessObj.h:80
void setName(const std::string &n)
Access the instance name.
Definition DbAccessObj.h:72
int release() const
Remove reference count.
Definition DbAccessObj.h:97
const std::string & name() const
Access the instance name.
Definition DbAccessObj.h:70
Io::IoFlag mode() const
Access mode.
Definition DbAccessObj.h:74
IOODatabase * db()
Definition DbAccessObj.h:81
std::string m_name
Name of the instance.
Definition DbAccessObj.h:56
DbAccessObjBase(const std::string &n, Io::IoFlag m, const DbType &t, IOODatabase *s=0)
Constructor with initializing arguments.
Definition DbAccessObj.h:64
std::atomic< int > m_refCount
Reference counter.
Definition DbAccessObj.h:52
int addRef() const
Add reference count.
Definition DbAccessObj.h:85
Description:
const_iterator end() const
std::map< KEY, TYPE * > Keys
const_iterator begin() const
Keys::const_iterator const_iterator
DbAccessObj< KEY, TYPE > Base
Type definitions.
Keys::iterator iterator
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).
void clearEntries()
Object cleanup: remove all entries.
DbAccessObjBase(const std::string &n, Io::IoFlag m, const DbType &t, IOODatabase *s=0)
Constructor from the base class.
Definition DbAccessObj.h:64
TYPE * find(const KEY &key)
Find object by key.
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:148
Framework include files.
Definition libname.h:15
void releasePtr(T *&p)
Release a pointer.