All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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(){ /* nop */ }
23 
25  virtual ~Annotation(){ /* nop */ }
26 
28  bool addItem( const std::string & key,
29  const std::string & value,
30  bool sticky = false);
31 
33  bool removeItem( const std::string & key );
34 
36  std::string value( const std::string & key) const;
37 
39  void setValue( const std::string & key,
40  const std::string& value);
41 
43  void setSticky( const std::string & key,
44  bool sticky);
45 
47  int size() const;
48 
50  std::string key(int index) const;
51  std::string value(int index) const;
52 
54  void reset();
55 
56 private:
59  public:
60  AnnotationItem( std::string k = "",
61  std::string v = "",
62  bool vis = true):
63  m_key( k ), m_value( v ), m_sticky( vis )
64  {/* nop */};
65 
66  ~AnnotationItem(){ /* nop */};
67 
68  std::string m_key;
69  std::string m_value;
70  bool m_sticky;
71  };
72 
74  std::vector< AnnotationItem > m_annotationItems;
75 
77  std::map< std::string, unsigned int > m_identifiers;
78 
79  std::string emptyString;
80 };
81 }
82 
83 inline bool AIDA::Annotation::addItem( const std::string & key,
84  const std::string & value,
85  bool sticky )
86 {
87  if ( m_identifiers.find( key ) != m_identifiers.end() ) return false;
88  m_annotationItems.push_back( AnnotationItem( key, value, sticky ) );
89  m_identifiers.insert( std::make_pair( key, m_annotationItems.size() - 1 ) );
90  return true;
91 }
92 
93 inline bool AIDA::Annotation::removeItem( const std::string & key ) {
94  std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key );
95  if ( iKey == m_identifiers.end() ) return false;
96 
97  unsigned int indexToBeRemoved = iKey->second;
98  // check stickness
99 
100  if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
101 
102  // why rebuilding ?
103 
104  m_identifiers.clear();
105  std::vector< AnnotationItem > m_annotationItemsNew;
106  if ( m_annotationItems.size() > 1 ) m_annotationItemsNew.reserve( m_annotationItems.size() - 1 );
107  for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) {
108  if ( iItem == indexToBeRemoved ) continue;
109  const AnnotationItem& item = m_annotationItems[ iItem ];
110  m_annotationItemsNew.push_back( AnnotationItem( item.m_key, item.m_value, item.m_sticky ) );
111  m_identifiers.insert( std::make_pair( item.m_key, m_annotationItemsNew.size() - 1 ) );
112  }
113  m_annotationItems = m_annotationItemsNew;
114  return true;
115 }
116 
117 inline std::string AIDA::Annotation::value( const std::string & key) const
118 {
119  std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key );
120  if ( iKey == m_identifiers.end() ) return emptyString;
121  return ( m_annotationItems[ iKey->second ] ).m_value;
122 }
123 
124 inline void AIDA::Annotation::setValue( const std::string & key, const std::string& value)
125 {
126  std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key );
127  if ( iKey == m_identifiers.end() )
128  // not found then add it
129  addItem(key,value);
130  else
131  ( m_annotationItems[ iKey->second ] ).m_value = value;
132 }
133 
134 inline void AIDA::Annotation::setSticky( const std::string & key, bool sticky)
135 {
136  std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key );
137  if ( iKey == m_identifiers.end() ) return;
138  ( m_annotationItems[ iKey->second ] ).m_sticky = sticky;
139 }
140 
141 inline int AIDA::Annotation::size() const {
142  return m_annotationItems.size();
143 }
144 
145 inline std::string AIDA::Annotation::key(int index) const
146 {
147  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
148  return ( m_annotationItems[ index ] ).m_key;
149 }
150 
151 inline std::string AIDA::Annotation::value(int index) const
152 {
153  if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString;
154  return ( m_annotationItems[ index ] ).m_value;
155 }
156 
158 {
159  // Collect the non-sticky items
160  std::vector< std::string > itemsToRemove;
161  itemsToRemove.reserve( size() );
162  for ( int item = 0; item < size(); ++item ) {
163  if ( ! ( m_annotationItems[ item ] ).m_sticky ) {
164  itemsToRemove.push_back( ( m_annotationItems[ item ] ).m_key );
165  }
166  }
167 
168  for ( unsigned int i = 0; i < itemsToRemove.size(); ++i ) removeItem( itemsToRemove[i] );
169 }
170 
171 #endif
virtual ~Annotation()
Destructor.
Definition: Annotation.h:25
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:77
Annotation()
Constructor.
Definition: Annotation.h:22
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:60
bool removeItem(const std::string &key)
Remove the item indicated by a given key.
Definition: Annotation.h:93
std::string value(const std::string &key) const
Retrieve the value for a given key.
Definition: Annotation.h:117
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
bool addItem(const std::string &key, const std::string &value, bool sticky=false)
Add a key/value pair with a given sticky.
Definition: Annotation.h:83
std::string emptyString
Definition: Annotation.h:79
void reset()
Remove all the non-sticky items.
Definition: Annotation.h:157
std::string key(int index) const
Individual access to the Annotation-items.
Definition: Annotation.h:145
int size() const
Get the number of items in the Annotation.
Definition: Annotation.h:141
void setSticky(const std::string &key, bool sticky)
Set sticky for a given key.
Definition: Annotation.h:134
tuple item
print s1,s2
Definition: ana.py:146
Internal private annotation item class.
Definition: Annotation.h:58
void setValue(const std::string &key, const std::string &value)
Set value for a given key.
Definition: Annotation.h:124
list i
Definition: ana.py:128
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:74