The Gaudi Framework  master (37c0b60a)
Annotation.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 /* Emacs: -*- C++ -*- */
12 #ifndef GAUDISVC_ANNOTATION_H
13 #define GAUDISVC_ANNOTATION_H 1
14 
15 // Include files
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 #include <AIDA/IAnnotation.h>
21 
22 namespace AIDA {
23 
25 
26  class Annotation : virtual public IAnnotation {
27 
28  public:
30  bool addItem( const std::string& key, const std::string& value, bool sticky = false ) override;
31 
33  bool removeItem( const std::string& key ) override;
34 
36  std::string value( const std::string& key ) const override;
37 
39  void setValue( const std::string& key, const std::string& value ) override;
40 
42  void setSticky( const std::string& key, bool sticky ) override;
43 
45  int size() const override;
46 
48  std::string key( int index ) const override;
49  std::string value( int index ) const override;
50 
52  void reset() override;
53 
54  private:
56  struct AnnotationItem final {
57  AnnotationItem( std::string k = "", std::string v = "", bool vis = true )
58  : m_key( std::move( k ) ), m_value( std::move( v ) ), m_sticky( vis ) {}
59 
60  ~AnnotationItem() = default;
61 
64  bool m_sticky;
65  };
66 
69 
72 
74  };
75 } // namespace AIDA
76 
77 inline bool AIDA::Annotation::addItem( const std::string& key, const std::string& value, bool sticky ) {
78  if ( m_identifiers.find( key ) != m_identifiers.end() ) return false;
79  m_annotationItems.emplace_back( key, value, sticky );
81  return true;
82 }
83 
85  auto iKey = m_identifiers.find( key );
86  if ( iKey == m_identifiers.end() ) return false;
87 
88  unsigned int indexToBeRemoved = iKey->second;
89  // check stickness
90 
91  if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
92 
93  // why rebuilding ?
94 
95  m_identifiers.clear();
96  std::vector<AnnotationItem> annotationItemsNew;
97  if ( m_annotationItems.size() > 1 ) annotationItemsNew.reserve( m_annotationItems.size() - 1 );
98  for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) {
99  if ( iItem == indexToBeRemoved ) continue;
100  const auto& item = m_annotationItems[iItem];
101  annotationItemsNew.emplace_back( item.m_key, item.m_value, item.m_sticky );
102  m_identifiers.emplace( item.m_key, annotationItemsNew.size() - 1 );
103  }
104  m_annotationItems = std::move( annotationItemsNew );
105  return true;
106 }
107 
109  auto iKey = m_identifiers.find( key );
110  return iKey != m_identifiers.end() ? m_annotationItems[iKey->second].m_value : emptyString;
111 }
112 
113 inline void AIDA::Annotation::setValue( const std::string& key, const std::string& value ) {
114  auto iKey = m_identifiers.find( key );
115  if ( iKey == m_identifiers.end() )
116  // if not found, then add it
117  addItem( key, value );
118  else
119  m_annotationItems[iKey->second].m_value = value;
120 }
121 
122 inline void AIDA::Annotation::setSticky( const std::string& key, bool sticky ) {
123  auto iKey = m_identifiers.find( key );
124  if ( iKey != m_identifiers.end() ) m_annotationItems[iKey->second].m_sticky = sticky;
125 }
126 
127 inline int AIDA::Annotation::size() const { return m_annotationItems.size(); }
128 
130  if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
131  return m_annotationItems[index].m_key;
132 }
133 
135  if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
136  return m_annotationItems[index].m_value;
137 }
138 
139 inline void AIDA::Annotation::reset() {
140  // Collect the non-sticky items
141  std::vector<std::string> itemsToRemove;
142  itemsToRemove.reserve( size() );
143  for ( const auto& item : m_annotationItems ) {
144  if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
145  }
146  for ( const auto& i : itemsToRemove ) removeItem( i );
147 }
148 
149 #endif
AIDA::Annotation::addItem
bool addItem(const std::string &key, const std::string &value, bool sticky=false) override
Add a key/value pair with a given sticky.
Definition: Annotation.h:77
AIDA
GaudiKernel.
Definition: Annotation.h:22
std::string
STL class.
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
AIDA::Annotation::AnnotationItem::AnnotationItem
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:57
AIDA::Annotation::AnnotationItem::~AnnotationItem
~AnnotationItem()=default
std::move
T move(T... args)
AIDA::Annotation::key
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:129
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
std::map::find
T find(T... args)
std::vector::size
T size(T... args)
AIDA::Annotation::removeItem
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:84
AIDA::Annotation::setSticky
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:122
std::map::emplace
T emplace(T... args)
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:26
std::vector::push_back
T push_back(T... args)
AIDA::Annotation::emptyString
std::string emptyString
Definition: Annotation.h:73
AIDA::Annotation::AnnotationItem
Internal private annotation item class.
Definition: Annotation.h:56
AIDA::Annotation::AnnotationItem::m_key
std::string m_key
Definition: Annotation.h:62
AIDA::Annotation::value
std::string value(const std::string &key) const override
Retrieve the value for a given key.
Definition: Annotation.h:108
AIDA::Annotation::m_annotationItems
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:68
AIDA::Annotation::reset
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:139
AIDA::Annotation::size
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:127
std::map< std::string, unsigned int >
std::vector::emplace_back
T emplace_back(T... args)
std
STL namespace.
AIDA::Annotation::AnnotationItem::m_sticky
bool m_sticky
Definition: Annotation.h:64
Properties.v
v
Definition: Properties.py:122
AIDA::Annotation::AnnotationItem::m_value
std::string m_value
Definition: Annotation.h:63
AIDA::Annotation::m_identifiers
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:71
std::map::end
T end(T... args)
ProduceConsume.key
key
Definition: ProduceConsume.py:84
AIDA::Annotation::setValue
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition: Annotation.h:113
Gaudi::ParticleProperties::index
size_t index(const Gaudi::ParticleProperty *property, const Gaudi::Interfaces::IParticlePropertySvc *service)
helper utility for mapping of Gaudi::ParticleProperty object into non-negative integral sequential id...
Definition: IParticlePropertySvc.cpp:39