ATLAS Offline Software
AthenaAttributeListSpecification.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 #ifndef ATHENAPOOLUTILITIES_ATHENAATTRIBUTELISTSPECIFICATION_H
6 #define ATHENAPOOLUTILITIES_ATHENAATTRIBUTELISTSPECIFICATION_H
7 
8 #include <string>
9 #include <iostream>
10 #include <sstream>
11 #include <typeinfo>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <atomic>
16 #include "CoralBase/AttributeSpecification.h"
17 #include "CoralBase/AttributeListException.h"
18 #include "CoralBase/AttributeListSpecification.h"
19 //#include "CoralBase/Blob.h"
20 //#include "CoralBase/Date.h"
21 //#include "CoralBase/TimeStamp.h"
22 
23 #include "GaudiKernel/System.h"
24 
26 
28 //
29 // Class which acts like coral::AttributeSpecification
30 // BUT allows annotation like pool::CollectionDescription
31 // BUT which is encased in name,value pair interface
32 // which is not accessible through the constructor
33 //
35 
36 
43 public:
46 
49 
51  AthenaAttributeListSpecification( const coral::AttributeListSpecification& rhs );
52 
54  void release() const;
55 
57  template<typename T> void extend( const std::string& name,const std::string& unitName ="UNKNOWN", const std::string& groupName ="UNKNOWN" );
58 
59  void extend( const std::string& name,
60  const std::string& typeName,
61  const std::string& unitName="UNKNOWN",
62  const std::string& groupName="UNKNOWN");
63  void extend( const std::string& name,
64  const std::type_info& type,
65  const std::string& unitName="UNKNOWN",
66  const std::string& groupName="UNKNOWN");
67  void extend( const std::string& name,
68  const AthenaAttributeType& type );
70 
72  size_t size() const;
73 
75  bool operator==( const AthenaAttributeListSpecification& rhs ) const;
76 
78  bool operator!=( const AthenaAttributeListSpecification& rhs ) const;
79 
81  int index( const std::string& name ) const;
82 
84  const AthenaAttributeSpecification& operator[]( size_t index ) const;
85 
88 
90  void coralSpec(coral::AttributeListSpecification&) const;
91 
94  {
95  public:
97 
98  const_iterator( const const_iterator& rhs );
100 
101  private:
103  const_iterator( std::vector< AthenaAttributeSpecification* >::const_iterator theIterator );
104 
105  public:
108  void operator++();
109  bool operator==( const const_iterator& rhs ) const;
110  bool operator!=( const const_iterator& rhs ) const;
111 
112  private:
113  std::vector< AthenaAttributeSpecification* >::const_iterator m_iterator;
114  };
115 
116 
118  const_iterator begin() const;
119  const_iterator end() const;
120 
121 
122 protected:
124  friend class AthenaAttributeList;
125  void addRef() const;
126 
129 
132 
133 private:
135  mutable std::atomic<int> m_counter;
136 
138  std::vector< AthenaAttributeSpecification* > m_attributeSpecifications;
139 
141  std::map< std::string, int > m_mapOfNameToIndex;
142 };
143 
144 
145 
146 
147 // Inline methods
148 template<typename T> void
149 AthenaAttributeListSpecification::extend( const std::string& name, const std::string& unitName, const std::string& groupName )
150 {
151  this->extend( name, typeid(T), unitName, groupName );
152 }
153 
154 
155 inline size_t
157 {
158  return m_attributeSpecifications.size();
159 }
160 
161 
164 {
166 }
167 
168 
171 {
173 }
174 
175 inline
177  m_iterator( rhs.m_iterator )
178 {}
179 
182 {
183  m_iterator = rhs.m_iterator;
184  return *this;
185 }
186 
187 inline
188 AthenaAttributeListSpecification::const_iterator::const_iterator( std::vector< AthenaAttributeSpecification* >::const_iterator theIterator ):
189  m_iterator( theIterator )
190 {}
191 
192 
193 inline const AthenaAttributeSpecification*
195 {
196  return *m_iterator;
197 }
198 
199 inline const AthenaAttributeSpecification&
201 {
202  return **m_iterator;
203 }
204 
205 inline void
207 {
208  ++m_iterator;
209 }
210 
211 inline bool
213 {
214  return m_iterator == rhs.m_iterator;
215 }
216 
217 inline bool
219 {
220  return m_iterator != rhs.m_iterator;
221 }
222 
223 inline const AthenaAttributeSpecification&
225 {
226  return this->specificationForAttribute( static_cast<int>( index ) );
227 }
228 
229 inline bool
231 {
232  return ( ! ( *this == rhs ) );
233 }
234 
235 inline
237  m_counter( 1 ),
240 {}
241 
242 inline
244  m_counter( 1 ),
245  m_attributeSpecifications(),
246  m_mapOfNameToIndex( rhs.m_mapOfNameToIndex )
247 {
248  for ( std::vector< AthenaAttributeSpecification* >::const_iterator iSpecification = rhs.m_attributeSpecifications.begin();
249  iSpecification != rhs.m_attributeSpecifications.end(); ++iSpecification ) {
250  m_attributeSpecifications.push_back( new AthenaAttributeSpecification( **iSpecification ) );
251  }
252 }
253 
254 inline
255 AthenaAttributeListSpecification::AthenaAttributeListSpecification( const coral::AttributeListSpecification& rhs ):
256  m_counter( 1 ),
257  m_attributeSpecifications(),
258  m_mapOfNameToIndex()
259 {
260  //std::cerr << "AthenaAttributeListSpecification (coral version) with counter = " << m_counter << std::endl;
261  for ( coral::AttributeListSpecification::const_iterator iSpecification = rhs.begin();
262  iSpecification != rhs.end(); ++iSpecification )
263  {
264  this->extend(iSpecification->name(),iSpecification->typeName());
265  }
266 }
267 
268 inline
270 {
271  //std::cerr << "In AthenaAttributeListSpecification destructor" << std::endl;
273  iSpecification != m_attributeSpecifications.end(); ++iSpecification ) delete *iSpecification;
274 }
275 
276 
277 inline void
279 {
280  ++m_counter;
281 }
282 
283 
284 inline void
286 {
287  //std::cerr << "Before decrement " << std::endl;
288  //std::cerr << "AthenaAttributeListSpecification release called "
289  // << "with counter = " << m_counter << std::endl;
290  if (--m_counter == 0) delete this;
291 }
292 
293 inline void
295  const std::string& typeName,
296  const std::string& unitName,
297  const std::string& groupName )
298 {
299  if ( m_mapOfNameToIndex.find( name ) != m_mapOfNameToIndex.end() )
300  throw coral::AttributeListException( "An attribute with the name \"" + name + "\" exists already" );
302  m_mapOfNameToIndex.insert( std::make_pair( name, static_cast<int>( m_attributeSpecifications.size() - 1 ) ) );
303 }
304 
305 
306 inline void
308  const std::type_info& typeInfo,
309  const std::string& unitName,
310  const std::string& groupName)
311 {
312  std::string typeName = System::typeinfoName(typeInfo);
313  if ( m_mapOfNameToIndex.find( name ) != m_mapOfNameToIndex.end() )
314  throw coral::AttributeListException( "An attribute with the name \"" + name + "\" exists already" );
316  m_mapOfNameToIndex.insert( std::make_pair( name, static_cast<int>( m_attributeSpecifications.size() - 1 ) ) );
317 }
318 
319 
320 inline void
323 {
324  if ( m_mapOfNameToIndex.find( name ) != m_mapOfNameToIndex.end() )
325  throw coral::AttributeListException( "An attribute with the name \"" + name + "\" exists already" );
327  m_mapOfNameToIndex.insert( std::make_pair( name, static_cast<int>( m_attributeSpecifications.size() - 1 ) ) );
328 }
329 
330 
331 inline void
333 {
334  if ( m_mapOfNameToIndex.find( aas.name() ) != m_mapOfNameToIndex.end() )
335  throw coral::AttributeListException( "An attribute with the name \"" + aas.name() + "\" exists already" );
337  m_mapOfNameToIndex.insert( std::make_pair( aas.name(), static_cast<int>( m_attributeSpecifications.size() - 1 ) ) );
338 }
339 
340 
341 inline int
343 {
344  std::map<std::string, int>::const_iterator iSpecificationForAttribute = m_mapOfNameToIndex.find( name );
345  if ( iSpecificationForAttribute == m_mapOfNameToIndex.end() ) return -1;
346  else return iSpecificationForAttribute->second;
347 }
348 
349 
350 inline const AthenaAttributeSpecification&
352 {
353  if ( index < 0 || index >= static_cast<int>( m_attributeSpecifications.size() ) )
354  throw coral::AttributeListException( "Invalid index for the attribute already" );
355  return *( m_attributeSpecifications[ index ] );
356 }
357 
358 // Utility function which
359 // transforms an AthenaAttributeListSpecification
360 // into a coral::AttributeListSpecification
361 inline void
362 AthenaAttributeListSpecification::coralSpec(coral::AttributeListSpecification& s) const
363 {
365  i != this->end();
366  ++i)
367  {
368  s.extend( i->name(), i->attributeType().typeName() );
369  }
370 }
371 
372 
373 inline bool
375 {
376  if ( this->size() != rhs.size() ) return false;
377  size_t theSize = this->size();
378  const AthenaAttributeListSpecification& lhs = *this;
379  for ( size_t i = 0; i < theSize; ++i )
380  //if ( (*this)[i] != rhs[i] )
381  if ( lhs[i] != rhs[i] )
382  return false;
383  return true;
384 }
385 
386 #endif
AthenaAttributeListSpecification::addRef
void addRef() const
Definition: AthenaAttributeListSpecification.h:278
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AthenaAttributeListSpecification::index
int index(const std::string &name) const
Returns the index of a specification given its name. If the name is not found it returns -1.
Definition: AthenaAttributeListSpecification.h:342
query_example.row
row
Definition: query_example.py:24
AthenaAttributeSpecification.h
AthenaAttributeListSpecification::const_iterator::operator=
const_iterator & operator=(const const_iterator &rhs)
Definition: AthenaAttributeListSpecification.h:181
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
AthenaAttributeListSpecification
Definition: AthenaAttributeListSpecification.h:42
AthenaAttributeListSpecification::m_mapOfNameToIndex
std::map< std::string, int > m_mapOfNameToIndex
The map of names to indices.
Definition: AthenaAttributeListSpecification.h:141
AthenaAttributeListSpecification::const_iterator::operator==
bool operator==(const const_iterator &rhs) const
Definition: AthenaAttributeListSpecification.h:212
AthenaAttributeListSpecification::const_iterator::operator++
void operator++()
Definition: AthenaAttributeListSpecification.h:206
AthenaAttributeListSpecification::const_iterator
The iterator class.
Definition: AthenaAttributeListSpecification.h:94
index
Definition: index.py:1
RunTileMonitoring.groupName
groupName
Definition: RunTileMonitoring.py:158
AthenaAttributeListSpecification::const_iterator::operator->
const AthenaAttributeSpecification * operator->() const
Definition: AthenaAttributeListSpecification.h:194
AthenaAttributeListSpecification::begin
const_iterator begin() const
Returns a forward iterator.
Definition: AthenaAttributeListSpecification.h:163
AthenaAttributeListSpecification::specificationForAttribute
const AthenaAttributeSpecification & specificationForAttribute(int index) const
Returns the specification given an index.
Definition: AthenaAttributeListSpecification.h:351
AthenaAttributeListSpecification::m_counter
std::atomic< int > m_counter
The reference counter.
Definition: AthenaAttributeListSpecification.h:135
AthenaAttributeListSpecification::extend
void extend(const std::string &name, const std::string &unitName="UNKNOWN", const std::string &groupName="UNKNOWN")
Appends a new specification.
Definition: AthenaAttributeListSpecification.h:149
AthenaAttributeListSpecification::const_iterator::const_iterator
const_iterator(const const_iterator &rhs)
Definition: AthenaAttributeListSpecification.h:176
AthenaAttributeListSpecification::m_attributeSpecifications
std::vector< AthenaAttributeSpecification * > m_attributeSpecifications
The specifications.
Definition: AthenaAttributeListSpecification.h:138
lumiFormat.i
int i
Definition: lumiFormat.py:92
AthenaAttributeListSpecification::const_iterator::m_iterator
std::vector< AthenaAttributeSpecification * >::const_iterator m_iterator
Definition: AthenaAttributeListSpecification.h:113
AthenaAttributeListSpecification::const_iterator::operator!=
bool operator!=(const const_iterator &rhs) const
Definition: AthenaAttributeListSpecification.h:218
AthenaAttributeListSpecification::~AthenaAttributeListSpecification
~AthenaAttributeListSpecification()
The destructor is private.
Definition: AthenaAttributeListSpecification.h:269
AthenaAttributeListSpecification::end
const_iterator end() const
Definition: AthenaAttributeListSpecification.h:170
AthenaAttributeList
An AttributeList represents a logical row of attributes in a metadata table. The name and type of eac...
Definition: PersistentDataModel/PersistentDataModel/AthenaAttributeList.h:45
AthenaAttributeListSpecification::release
void release() const
Decrements the reference counter.
Definition: AthenaAttributeListSpecification.h:285
AthenaAttributeSpecification::name
std::string name() const
Definition: AthenaAttributeSpecification.h:39
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
AthenaAttributeListSpecification::const_iterator::~const_iterator
~const_iterator()
Definition: AthenaAttributeListSpecification.h:96
AthenaAttributeType
Definition: AthenaAttributeType.h:31
AthenaAttributeListSpecification::size
size_t size() const
Returns the size of the specification.
Definition: AthenaAttributeListSpecification.h:156
AthenaAttributeListSpecification::AthenaAttributeListSpecification
AthenaAttributeListSpecification()
Constructor.
Definition: AthenaAttributeListSpecification.h:236
AthenaAttributeListSpecification::operator=
AthenaAttributeListSpecification & operator=(const AthenaAttributeListSpecification &)
No assignment operator.
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
AthenaAttributeListSpecification::operator!=
bool operator!=(const AthenaAttributeListSpecification &rhs) const
Comparison operator. Compares only types and values. Not the attribute names.
Definition: AthenaAttributeListSpecification.h:230
AthenaAttributeListSpecification::operator[]
const AthenaAttributeSpecification & operator[](size_t index) const
Bracket operator.
Definition: AthenaAttributeListSpecification.h:224
AthenaAttributeListSpecification::const_iterator::operator*
const AthenaAttributeSpecification & operator*() const
Definition: AthenaAttributeListSpecification.h:200
ReadCalibFromCool.typeName
typeName
Definition: ReadCalibFromCool.py:477
AthenaAttributeSpecification
Definition: AthenaAttributeSpecification.h:25
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
AthenaAttributeListSpecification::coralSpec
void coralSpec(coral::AttributeListSpecification &) const
Fills a coral Specification.
Definition: AthenaAttributeListSpecification.h:362
AthenaAttributeListSpecification::operator==
bool operator==(const AthenaAttributeListSpecification &rhs) const
Equal operator. Compares only types and values. Not the attribute names.
Definition: AthenaAttributeListSpecification.h:374