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