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 <vector>
7 #include <string>
8 #include <map>
9 
10 #include "AIDA/IAnnotation.h"
11 
12 namespace AIDA {
13 
15 
16 class Annotation : virtual public IAnnotation {
17 
18 public:
19 
21  bool addItem( const std::string & key,
22  const std::string & value,
23  bool sticky = false) override;
24 
26  bool removeItem( const std::string & key ) override;
27 
29  std::string value( const std::string & key) const override;
30 
32  void setValue( const std::string & key,
33  const std::string& value) override;
34 
36  void setSticky( const std::string & key,
37  bool sticky) override;
38 
40  int size() const override;
41 
43  std::string key(int index) const override;
44  std::string value(int index) const override;
45 
47  void reset() override;
48 
49 private:
51  struct AnnotationItem final {
52  AnnotationItem( std::string k = "", std::string v = "", bool vis = true):
53  m_key( std::move(k) ), m_value( std::move(v) ), m_sticky( vis )
54  {/* nop */};
55 
56  ~AnnotationItem() = default;
57 
60  bool m_sticky;
61  };
62 
65 
68 
70 };
71 }
72 
74  const std::string & value,
75  bool sticky )
76 {
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 
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 
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 
108 {
109  auto iKey = m_identifiers.find( key );
110  return iKey != m_identifiers.end() ? m_annotationItems[ iKey->second ].m_value
111  : emptyString;
112 }
113 
115 {
116  auto iKey = m_identifiers.find( key );
117  if ( iKey == m_identifiers.end() )
118  // if not found, then add it
119  addItem(key,value);
120  else
121  m_annotationItems[ iKey->second ].m_value = value;
122 }
123 
124 inline void AIDA::Annotation::setSticky( const std::string & key, bool sticky)
125 {
126  auto iKey = m_identifiers.find( key );
127  if ( iKey != m_identifiers.end() ) m_annotationItems[ iKey->second ].m_sticky = sticky;
128 }
129 
130 inline int AIDA::Annotation::size() const {
131  return m_annotationItems.size();
132 }
133 
134 inline std::string AIDA::Annotation::key(int index) const
135 {
136  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
137  return m_annotationItems[ index ].m_key;
138 }
139 
140 inline std::string AIDA::Annotation::value(int index) const
141 {
142  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
143  return m_annotationItems[ index ].m_value;
144 }
145 
147 {
148  // Collect the non-sticky items
149  std::vector< std::string > itemsToRemove;
150  itemsToRemove.reserve( size() );
151  for ( const auto& item : m_annotationItems ) {
152  if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
153  }
154  for ( const auto& i : itemsToRemove ) removeItem(i);
155 }
156 
157 #endif
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:124
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:67
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:134
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition: Annotation.h:114
Internal private annotation item class.
Definition: Annotation.h:51
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:73
GaudiKernel.
Definition: Fill.h:8
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:130
STL class.
T push_back(T...args)
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:52
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:16
std::string emptyString
Definition: Annotation.h:69
T clear(T...args)
T move(T...args)
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:107
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:146
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:83
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:64
T reserve(T...args)
T emplace_back(T...args)