| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
65 std::string m_key;
66 std::string m_value;
67 bool m_sticky;
68 };
69
71 std::vector< AnnotationItem > m_annotationItems;
72
74 std::map< std::string, unsigned int > m_identifiers;
75
76 std::string emptyString;
77 };
78 }
79
80 inline bool AIDA::Annotation::addItem( const std::string & key,
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
90 inline bool AIDA::Annotation::removeItem( const std::string & key ) {
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
101 m_identifiers.clear();
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
114 inline std::string AIDA::Annotation::value( const std::string & key) const
115 {
116 auto iKey = m_identifiers.find( key );
117 return iKey != m_identifiers.end() ? m_annotationItems[ iKey->second ].m_value
118 : emptyString;
119 }
120
121 inline void AIDA::Annotation::setValue( const std::string & key, const std::string& value)
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
153 inline void AIDA::Annotation::reset()
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
AIDA::Annotation::setSticky
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:131
AIDA::Annotation::m_identifiers
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:74
AIDA_visibility_hack.h
AIDA::Annotation::setValue
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition: Annotation.h:121
AIDA::Annotation::AnnotationItem
Internal private annotation item class.
Definition: Annotation.h:58
std
STL namespace.
AIDA::Annotation::key
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:141
AIDA::Annotation::~Annotation
~Annotation() override=default
Destructor.
std::map::end
T end(T...args)
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:80
std::map< std::string, unsigned int >
AIDA
GaudiKernel.
Definition: Fill.h:8
AIDA::Annotation::AnnotationItem::m_sticky
bool m_sticky
Definition: Annotation.h:67
std::string
STL class.
AIDA::Annotation::size
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:137
std::vector::push_back
T push_back(T...args)
AIDA::Annotation::AnnotationItem::m_key
std::string m_key
Definition: Annotation.h:65
AIDA::Annotation::AnnotationItem::AnnotationItem
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:59
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:18
AIDA::Annotation::emptyString
std::string emptyString
Definition: Annotation.h:76
dump_component_properties.value
list value
Definition: dump_component_properties.py:12
std::move
T move(T...args)
AIDA::Annotation::Annotation
Annotation()=default
Constructor.
std::map::find
T find(T...args)
std::vector::size
T size(T...args)
std::vector
STL class.
std::map::emplace
T emplace(T...args)
AIDA::Annotation::value
std::string value(const std::string &key) const override
Retrieve the value for a given key.
Definition: Annotation.h:114
ana.item
tuple item
print s1,s2
Definition: ana.py:146
AIDA::Annotation::reset
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:153
AIDA::Annotation::AnnotationItem::~AnnotationItem
~AnnotationItem()=default
AIDA::Annotation::AnnotationItem::m_value
std::string m_value
Definition: Annotation.h:66
ana.i
list i
Definition: ana.py:128
AIDA::Annotation::removeItem
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:90
AIDA::Annotation::m_annotationItems
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:71
std::vector::reserve
T reserve(T...args)
std::vector::emplace_back
T emplace_back(T...args)