| The Gaudi Framework v29r0 (ff2e7097)

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