| The Gaudi Framework v28r2-patches (bc4ad17a)

- 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
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
58 std::string m_key;
59 std::string m_value;
60 bool m_sticky;
61 };
62
64 std::vector< AnnotationItem > m_annotationItems;
65
67 std::map< std::string, unsigned int > m_identifiers;
68
69 std::string emptyString;
70 };
71 }
72
73 inline bool AIDA::Annotation::addItem( const std::string & key,
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
83 inline bool AIDA::Annotation::removeItem( const std::string & key ) {
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
94 m_identifiers.clear();
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
107 inline std::string AIDA::Annotation::value( const std::string & key) const
108 {
109 auto iKey = m_identifiers.find( key );
110 return iKey != m_identifiers.end() ? m_annotationItems[ iKey->second ].m_value
111 : emptyString;
112 }
113
114 inline void AIDA::Annotation::setValue( const std::string & key, const std::string& value)
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
146 inline void AIDA::Annotation::reset()
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
AIDA::Annotation::setSticky
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition: Annotation.h:124
AIDA::Annotation::m_identifiers
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition: Annotation.h:67
AIDA::Annotation::key
std::string key(int index) const override
Individual access to the Annotation-items.
Definition: Annotation.h:134
AIDA::Annotation::setValue
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition: Annotation.h:114
AIDA::Annotation::AnnotationItem
Internal private annotation item class.
Definition: Annotation.h:51
std
STL namespace.
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:73
std::map< std::string, unsigned int >
AIDA
GaudiKernel.
Definition: Fill.h:8
AIDA::Annotation::AnnotationItem::m_sticky
bool m_sticky
Definition: Annotation.h:60
AIDA::Annotation::size
int size() const override
Get the number of items in the Annotation.
Definition: Annotation.h:130
std::string
STL class.
std::vector::push_back
T push_back(T...args)
AIDA::Annotation::AnnotationItem::m_key
std::string m_key
Definition: Annotation.h:58
AIDA::Annotation::AnnotationItem::AnnotationItem
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition: Annotation.h:52
AIDA::Annotation
Implementation of the AIDA IAnnotation interface class.
Definition: Annotation.h:16
AIDA::Annotation::emptyString
std::string emptyString
Definition: Annotation.h:69
std::map::clear
T clear(T...args)
std::move
T move(T...args)
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:107
AIDA::Annotation::reset
void reset() override
Remove all the non-sticky items.
Definition: Annotation.h:146
AIDA::Annotation::AnnotationItem::~AnnotationItem
~AnnotationItem()=default
AIDA::Annotation::AnnotationItem::m_value
std::string m_value
Definition: Annotation.h:59
AIDA::Annotation::removeItem
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition: Annotation.h:83
AIDA::Annotation::m_annotationItems
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition: Annotation.h:64
std::vector::reserve
T reserve(T...args)
std::vector::emplace_back
T emplace_back(T...args)