| The Gaudi Framework v32r1 (f65d50dc)

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