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 #include "AIDA_visibility_hack.h"
6 
7 // Include files
8 #include <vector>
9 #include <string>
10 #include <map>
11 
12 #include "AIDA/IAnnotation.h"
13 
14 namespace AIDA {
15 
17 
18 class Annotation : virtual public IAnnotation {
19 
20 public:
22  Annotation() = default;
23 
25  ~Annotation() override = default;
26 
28  bool addItem( const std::string & key,
29  const std::string & value,
30  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,
40  const std::string& value) override;
41 
43  void setSticky( const std::string & key,
44  bool sticky) override;
45 
47  int size() const override;
48 
50  std::string key(int index) const override;
51  std::string value(int index) const override;
52 
54  void reset() override;
55 
56 private:
58  struct AnnotationItem final {
59  AnnotationItem( std::string k = "", std::string v = "", bool vis = true):
60  m_key( std::move(k) ), m_value( std::move(v) ), m_sticky( vis )
61  {/* nop */};
62 
63  ~AnnotationItem() = default;
64 
67  bool m_sticky;
68  };
69 
72 
75 
77 };
78 }
79 
81  const std::string & value,
82  bool sticky )
83 {
84  if ( m_identifiers.find( key ) != m_identifiers.end() ) return false;
85  m_annotationItems.emplace_back( key, value, sticky );
86  m_identifiers.emplace( key, m_annotationItems.size() - 1 );
87  return true;
88 }
89 
91  auto iKey = m_identifiers.find( key );
92  if ( iKey == m_identifiers.end() ) return false;
93 
94  unsigned int indexToBeRemoved = iKey->second;
95  // check stickness
96 
97  if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
98 
99  // why rebuilding ?
100 
102  std::vector< AnnotationItem > annotationItemsNew;
103  if ( m_annotationItems.size() > 1 ) annotationItemsNew.reserve( m_annotationItems.size() - 1 );
104  for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) {
105  if ( iItem == indexToBeRemoved ) continue;
106  const auto& item = m_annotationItems[ iItem ];
107  annotationItemsNew.emplace_back( item.m_key, item.m_value, item.m_sticky );
108  m_identifiers.emplace( item.m_key, annotationItemsNew.size() - 1 );
109  }
110  m_annotationItems = std::move(annotationItemsNew);
111  return true;
112 }
113 
115 {
116  auto iKey = m_identifiers.find( key );
117  return iKey != m_identifiers.end() ? m_annotationItems[ iKey->second ].m_value
118  : emptyString;
119 }
120 
122 {
123  auto iKey = m_identifiers.find( key );
124  if ( iKey == m_identifiers.end() )
125  // if not found, then add it
126  addItem(key,value);
127  else
128  m_annotationItems[ iKey->second ].m_value = value;
129 }
130 
131 inline void AIDA::Annotation::setSticky( const std::string & key, bool sticky)
132 {
133  auto iKey = m_identifiers.find( key );
134  if ( iKey != m_identifiers.end() ) m_annotationItems[ iKey->second ].m_sticky = sticky;
135 }
136 
137 inline int AIDA::Annotation::size() const {
138  return m_annotationItems.size();
139 }
140 
141 inline std::string AIDA::Annotation::key(int index) const
142 {
143  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
144  return m_annotationItems[ index ].m_key;
145 }
146 
147 inline std::string AIDA::Annotation::value(int index) const
148 {
149  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
150  return m_annotationItems[ index ].m_value;
151 }
152 
154 {
155  // Collect the non-sticky items
156  std::vector< std::string > itemsToRemove;
157  itemsToRemove.reserve( size() );
158  for ( const auto& item : m_annotationItems ) {
159  if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
160  }
161  for ( const auto& i : itemsToRemove ) removeItem(i);
162 }
163 
164 #endif
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:131
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:74
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:141
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition: Annotation.h:121
Internal private annotation item class.
Definition: Annotation.h:58
STL namespace.
~Annotation() override=default
Destructor.
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:80
GaudiKernel.
Definition: Fill.h:8
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:137
STL class.
T push_back(T...args)
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:59
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
std::string emptyString
Definition: Annotation.h:76
T clear(T...args)
T move(T...args)
Annotation()=default
Constructor.
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:114
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:153
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:90
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:71
T reserve(T...args)
T emplace_back(T...args)