The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
Annotation.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11/* Emacs: -*- C++ -*- */
12#pragma once
13
14// Include files
15#include <map>
16#include <string>
17#include <vector>
18
19#include <AIDA/IAnnotation.h>
20
21namespace AIDA {
22
24
25 class Annotation : virtual public IAnnotation {
26
27 public:
29 bool addItem( const std::string& key, const std::string& value, bool sticky = false ) override;
30
32 bool removeItem( const std::string& key ) override;
33
35 std::string value( const std::string& key ) const override;
36
38 void setValue( const std::string& key, const std::string& value ) override;
39
41 void setSticky( const std::string& key, bool sticky ) override;
42
44 int size() const override;
45
47 std::string key( int index ) const override;
48 std::string value( int index ) const override;
49
51 void reset() override;
52
53 private:
55 struct AnnotationItem final {
56 AnnotationItem( std::string k = "", std::string v = "", bool vis = true )
57 : m_key( std::move( k ) ), m_value( std::move( v ) ), m_sticky( vis ) {}
58
59 ~AnnotationItem() = default;
60
61 std::string m_key;
62 std::string m_value;
64 };
65
67 std::vector<AnnotationItem> m_annotationItems;
68
70 std::map<std::string, unsigned int> m_identifiers;
71
72 std::string emptyString;
73 };
74} // namespace AIDA
75
76inline bool AIDA::Annotation::addItem( const std::string& key, const std::string& value, bool sticky ) {
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
83inline 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
107inline std::string AIDA::Annotation::value( const std::string& key ) const {
108 auto iKey = m_identifiers.find( key );
109 return iKey != m_identifiers.end() ? m_annotationItems[iKey->second].m_value : emptyString;
110}
111
112inline void AIDA::Annotation::setValue( const std::string& key, const std::string& value ) {
113 auto iKey = m_identifiers.find( key );
114 if ( iKey == m_identifiers.end() )
115 // if not found, then add it
116 addItem( key, value );
117 else
118 m_annotationItems[iKey->second].m_value = value;
119}
120
121inline void AIDA::Annotation::setSticky( const std::string& key, bool sticky ) {
122 auto iKey = m_identifiers.find( key );
123 if ( iKey != m_identifiers.end() ) m_annotationItems[iKey->second].m_sticky = sticky;
124}
125
126inline int AIDA::Annotation::size() const { return m_annotationItems.size(); }
127
128inline std::string AIDA::Annotation::key( int index ) const {
129 if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
130 return m_annotationItems[index].m_key;
131}
132
133inline std::string AIDA::Annotation::value( int index ) const {
134 if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
135 return m_annotationItems[index].m_value;
136}
137
139 // Collect the non-sticky items
140 std::vector<std::string> itemsToRemove;
141 itemsToRemove.reserve( size() );
142 for ( const auto& item : m_annotationItems ) {
143 if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
144 }
145 for ( const auto& i : itemsToRemove ) removeItem( i );
146}
Implementation of the AIDA IAnnotation interface class.
Definition Annotation.h:25
void setSticky(const std::string &key, bool sticky) override
Set sticky for a given key.
Definition Annotation.h:121
bool removeItem(const std::string &key) override
Remove the item indicated by a given key.
Definition Annotation.h:83
int size() const override
Get the number of items in the Annotation.
Definition Annotation.h:126
std::string value(const std::string &key) const override
Retrieve the value for a given key.
Definition Annotation.h:107
std::string emptyString
Definition Annotation.h:72
std::string key(int index) const override
Individual access to the Annotation-items.
Definition Annotation.h:128
std::vector< AnnotationItem > m_annotationItems
The vector of the annotation items.
Definition Annotation.h:67
std::map< std::string, unsigned int > m_identifiers
The map of strings to identifiers.
Definition Annotation.h:70
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:76
void reset() override
Remove all the non-sticky items.
Definition Annotation.h:138
void setValue(const std::string &key, const std::string &value) override
Set value for a given key.
Definition Annotation.h:112
STL namespace.
AnnotationItem(std::string k="", std::string v="", bool vis=true)
Definition Annotation.h:56