Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Annotation.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
13 
14 // Include files
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 #include <AIDA/IAnnotation.h>
20 
21 namespace AIDA {
22 
24 
25  class Annotation : virtual public IAnnotation {
26 
27  public:
29  bool addItem( const std::string& key, const std::string& value, bool sticky = false ) override;
30 
32  bool removeItem( const std::string& key ) override;
33 
35  std::string value( const std::string& key ) const override;
36 
38  void setValue( const std::string& key, const std::string& value ) override;
39 
41  void setSticky( const std::string& key, bool sticky ) override;
42 
44  int size() const override;
45 
47  std::string key( int index ) const override;
48  std::string value( int index ) const override;
49 
51  void reset() override;
52 
53  private:
55  struct AnnotationItem final {
56  AnnotationItem( std::string k = "", std::string v = "", bool vis = true )
57  : m_key( std::move( k ) ), m_value( std::move( v ) ), m_sticky( vis ) {}
58 
59  ~AnnotationItem() = default;
60 
61  std::string m_key;
62  std::string m_value;
63  bool m_sticky;
64  };
65 
67  std::vector<AnnotationItem> m_annotationItems;
68 
70  std::map<std::string, unsigned int> m_identifiers;
71 
72  std::string emptyString;
73  };
74 } // namespace AIDA
75 
76 inline bool AIDA::Annotation::addItem( const std::string& key, const std::string& value, bool sticky ) {
77  if ( m_identifiers.find( key ) != m_identifiers.end() ) return false;
78  m_annotationItems.emplace_back( key, value, sticky );
79  m_identifiers.emplace( key, m_annotationItems.size() - 1 );
80  return true;
81 }
82 
83 inline bool AIDA::Annotation::removeItem( const std::string& key ) {
84  auto iKey = m_identifiers.find( key );
85  if ( iKey == m_identifiers.end() ) return false;
86 
87  unsigned int indexToBeRemoved = iKey->second;
88  // check stickness
89 
90  if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
91 
92  // why rebuilding ?
93 
94  m_identifiers.clear();
95  std::vector<AnnotationItem> annotationItemsNew;
96  if ( m_annotationItems.size() > 1 ) annotationItemsNew.reserve( m_annotationItems.size() - 1 );
97  for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) {
98  if ( iItem == indexToBeRemoved ) continue;
99  const auto& item = m_annotationItems[iItem];
100  annotationItemsNew.emplace_back( item.m_key, item.m_value, item.m_sticky );
101  m_identifiers.emplace( item.m_key, annotationItemsNew.size() - 1 );
102  }
103  m_annotationItems = std::move( annotationItemsNew );
104  return true;
105 }
106 
107 inline std::string AIDA::Annotation::value( const std::string& key ) const {
108  auto iKey = m_identifiers.find( key );
109  return iKey != m_identifiers.end() ? m_annotationItems[iKey->second].m_value : emptyString;
110 }
111 
112 inline void AIDA::Annotation::setValue( const std::string& key, const std::string& value ) {
113  auto iKey = m_identifiers.find( key );
114  if ( iKey == m_identifiers.end() )
115  // if not found, then add it
116  addItem( key, value );
117  else
118  m_annotationItems[iKey->second].m_value = value;
119 }
120 
121 inline void AIDA::Annotation::setSticky( const std::string& key, bool sticky ) {
122  auto iKey = m_identifiers.find( key );
123  if ( iKey != m_identifiers.end() ) m_annotationItems[iKey->second].m_sticky = sticky;
124 }
125 
126 inline int AIDA::Annotation::size() const { return m_annotationItems.size(); }
127 
128 inline std::string AIDA::Annotation::key( int index ) const {
129  if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
130  return m_annotationItems[index].m_key;
131 }
132 
133 inline std::string AIDA::Annotation::value( int index ) const {
134  if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
135  return m_annotationItems[index].m_value;
136 }
137 
138 inline void AIDA::Annotation::reset() {
139  // Collect the non-sticky items
140  std::vector<std::string> itemsToRemove;
141  itemsToRemove.reserve( size() );
142  for ( const auto& item : m_annotationItems ) {
143  if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
144  }
145  for ( const auto& i : itemsToRemove ) removeItem( i );
146 }
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:76
AIDA
Definition: Annotation.h:21
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:56
AIDA::Annotation::AnnotationItem::~AnnotationItem
~AnnotationItem()=default
AIDA::Annotation::key
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:128
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
AIDA::Annotation::removeItem
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:83
AIDA::Annotation::setSticky
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:121
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:25
AIDA::Annotation::emptyString
std::string emptyString
Definition: Annotation.h:72
AIDA::Annotation::AnnotationItem
Internal private annotation item class.
Definition: Annotation.h:55
AIDA::Annotation::AnnotationItem::m_key
std::string m_key
Definition: Annotation.h:61
AIDA::Annotation::value
std::string value(const std::string &key) const override
Retrieve the value for a given key.
Definition: Annotation.h:107
AIDA::Annotation::m_annotationItems
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:67
AIDA::Annotation::reset
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:138
AIDA::Annotation::size
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:126
AIDA::Annotation::AnnotationItem::m_sticky
bool m_sticky
Definition: Annotation.h:63
Properties.v
v
Definition: Properties.py:122
AIDA::Annotation::AnnotationItem::m_value
std::string m_value
Definition: Annotation.h:62
AIDA::Annotation::m_identifiers
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:70
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:112
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