ATLAS Offline Software
DbAccessObj.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: DbAccessObj.h 726071 2016-02-25 09:23:05Z krasznaa $
6 //====================================================================
7 // DbAccessObj Base class definitions
8 //--------------------------------------------------------------------
9 //
10 // Package : StorageSvc (The POOL project)
11 // @author M.Frank
12 //====================================================================
13 #ifndef POOL_DBACCESSOBJ_H
14 #define POOL_DBACCESSOBJ_H 1
15 
16 // Framework include files
17 #include "StorageSvc/pool.h"
18 #include "StorageSvc/DbType.h"
19 #include "StorageSvc/IOODatabase.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  */
34 namespace 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(); }
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  }
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  }
119  size_t size() const {
120  return m_keys.size();
121  }
124  Keys k = m_keys;
125  for(iterator j = k.begin(); j != k.end(); ++j ) {
126  (*j).second->release();
127  }
128  m_keys.clear();
129  return Success;
130  }
132  const TYPE* find(const KEY& key) const {
133  const_iterator i = m_keys.find(key);
134  return (m_keys.end() == i) ? 0 : (*i).second;
135  }
137  TYPE* find(const KEY& key) {
138  iterator i = m_keys.find(key);
139  return (m_keys.end() == i) ? 0 : (*i).second;
140  }
142  DbStatus add(const KEY& key, TYPE* val) {
143  iterator i = m_keys.find(key);
144  if ( m_keys.end() == i ) {
145  m_keys.insert(std::make_pair(key, val));
146  val->addRef();
147  return Success;
148  }
149  return Error;
150  }
153  for (iterator j = m_keys.begin(); j != m_keys.end(); ++j ) {
154  if ( (*j).second == val ) {
155  TYPE* p = (*j).second;
156  m_keys.erase(j);
157  p->release();
158  return Success;
159  }
160  }
161  return Error;
162  }
163  iterator begin() { return m_keys.begin(); }
164  const_iterator begin() const { return m_keys.begin(); }
165  iterator end() { return m_keys.end(); }
166  const_iterator end() const { return m_keys.end(); }
167  };
168 } // End namespace pool
169 #endif // POOL_DBACCESSOBJ_H
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
pool::DbAccessMode
int DbAccessMode
Definition: Database/APR/StorageSvc/StorageSvc/pool.h:47
pool::DbAccessObj::remove
DbStatus remove(const TYPE *val)
Remove entry from container.
Definition: DbAccessObj.h:152
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
pool::DbStatus
Definition: DbStatus.h:67
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
pool::DbAccessObj::m_mode
DbAccessMode m_mode
Access mode.
Definition: DbAccessObj.h:58
pool::DbAccessObj::setMode
void setMode(DbAccessMode m)
Set Access mode.
Definition: DbAccessObj.h:82
pool::RefCounter::addRef
int addRef()
Increase the reference count.
Definition: Database/APR/StorageSvc/StorageSvc/pool.h:139
pool::DbAccessObj::m_type
DbType m_type
Database type.
Definition: DbAccessObj.h:62
pool::DbAccessObj::end
const_iterator end() const
Definition: DbAccessObj.h:166
pool
pool namespace
Definition: libname.h:15
pool::DbAccessObj::find
TYPE * find(const KEY &key)
Find object by key.
Definition: DbAccessObj.h:137
pool::DbAccessObj::begin
iterator begin()
Definition: DbAccessObj.h:163
pool::DbAccessObj::end
iterator end()
Definition: DbAccessObj.h:165
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DbType.h
pool::DbAccessObj::db
const IOODatabase * db() const
Allow access to the Database implementation.
Definition: DbAccessObj.h:86
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
pool::DbAccessObj::name
const std::string & name() const
Access the instance name.
Definition: DbAccessObj.h:76
pool::DbAccessObj::setName
void setName(const std::string &n)
Access the instance name.
Definition: DbAccessObj.h:78
pool::DbAccessObj::release
int release() const
Remove reference count.
Definition: DbAccessObj.h:103
pool::DbAccessObj::clearEntries
DbStatus clearEntries()
Object cleanup: remove all entries.
Definition: DbAccessObj.h:123
pool::DbAccessObj::DbAccessObj
DbAccessObj(const std::string &n, DbAccessMode m, const DbType &t, IOODatabase *s=0)
Constructor with initializing arguments.
Definition: DbAccessObj.h:70
pool::DbAccessObj
Definition: DbAccessObj.h:47
pool::DbAccessObj::m_refCount
std::atomic< int > m_refCount
Reference counter.
Definition: DbAccessObj.h:56
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
IOODatabase.h
pool::DbType
Definition: DbType.h:31
pool::DbAccessObj::mode
DbAccessMode mode() const
Access mode.
Definition: DbAccessObj.h:80
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
pool::DbAccessObj::const_iterator
Keys::const_iterator const_iterator
Definition: DbAccessObj.h:53
pool::DbAccessObj::m_keys
Keys m_keys
Key entry buffer.
Definition: DbAccessObj.h:64
pool::DbAccessObj::m_name
std::string m_name
Name of the instance.
Definition: DbAccessObj.h:60
pool::DbAccessObj::Base
DbAccessObj< KEY, TYPE > Base
Type definitions.
Definition: DbAccessObj.h:50
pool::DbAccessObj::iterator
Keys::iterator iterator
Definition: DbAccessObj.h:52
TYPE
#define TYPE(CODE, TYP, IOTYP)
pool.h
pool::DbAccessObj::refCount
int refCount() const
Access reference counter.
Definition: DbAccessObj.h:89
pool::DbAccessObj::addRef
int addRef() const
Add reference count.
Definition: DbAccessObj.h:91
pool::DbAccessObj::Keys
std::map< KEY, TYPE * > Keys
Definition: DbAccessObj.h:51
pool::DbAccessObj::db
IOODatabase * db()
Definition: DbAccessObj.h:87
pool::DbAccessObj::begin
const_iterator begin() const
Definition: DbAccessObj.h:164
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
pool::DbAccessObj::find
const TYPE * find(const KEY &key) const
Find object by key (CONST)
Definition: DbAccessObj.h:132
pool::releasePtr
DbStatus releasePtr(T *&p)
Release a pointer.
Definition: Database/APR/StorageSvc/StorageSvc/pool.h:110
pool::DbAccessObj::m_pool
IOODatabase * m_pool
Pointer to specific pool implementation.
Definition: DbAccessObj.h:66
pool::DbAccessObj::~DbAccessObj
virtual ~DbAccessObj()
Standard destructor.
Definition: DbAccessObj.h:74
pool::DbAccessObj::size
size_t size() const
Object size.
Definition: DbAccessObj.h:119
pool::DbAccessObj::add
DbStatus add(const KEY &key, TYPE *val)
Add entry to container.
Definition: DbAccessObj.h:142
pool::DbAccessObj::type
const DbType & type() const
Definition: DbAccessObj.h:84
pool::IOODatabase
Definition: IOODatabase.h:50
fitman.k
k
Definition: fitman.py:528
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37