File indexing completed on 2025-02-21 10:00:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef GAUDISVC_ANNOTATION_H
0013 #define GAUDISVC_ANNOTATION_H 1
0014
0015
0016 #include <map>
0017 #include <string>
0018 #include <vector>
0019
0020 #include "AIDA/IAnnotation.h"
0021
0022 namespace AIDA {
0023
0024
0025
0026 class Annotation : virtual public IAnnotation {
0027
0028 public:
0029
0030 bool addItem( const std::string& key, const std::string& value, bool sticky = false ) override;
0031
0032
0033 bool removeItem( const std::string& key ) override;
0034
0035
0036 std::string value( const std::string& key ) const override;
0037
0038
0039 void setValue( const std::string& key, const std::string& value ) override;
0040
0041
0042 void setSticky( const std::string& key, bool sticky ) override;
0043
0044
0045 int size() const override;
0046
0047
0048 std::string key( int index ) const override;
0049 std::string value( int index ) const override;
0050
0051
0052 void reset() override;
0053
0054 private:
0055
0056 struct AnnotationItem final {
0057 AnnotationItem( std::string k = "", std::string v = "", bool vis = true )
0058 : m_key( std::move( k ) ), m_value( std::move( v ) ), m_sticky( vis ){ };
0059
0060 ~AnnotationItem() = default;
0061
0062 std::string m_key;
0063 std::string m_value;
0064 bool m_sticky;
0065 };
0066
0067
0068 std::vector<AnnotationItem> m_annotationItems;
0069
0070
0071 std::map<std::string, unsigned int> m_identifiers;
0072
0073 std::string emptyString;
0074 };
0075 }
0076
0077 inline bool AIDA::Annotation::addItem( const std::string& key, const std::string& value, bool sticky ) {
0078 if ( m_identifiers.find( key ) != m_identifiers.end() ) return false;
0079 m_annotationItems.emplace_back( key, value, sticky );
0080 m_identifiers.emplace( key, m_annotationItems.size() - 1 );
0081 return true;
0082 }
0083
0084 inline bool AIDA::Annotation::removeItem( const std::string& key ) {
0085 auto iKey = m_identifiers.find( key );
0086 if ( iKey == m_identifiers.end() ) return false;
0087
0088 unsigned int indexToBeRemoved = iKey->second;
0089
0090
0091 if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
0092
0093
0094
0095 m_identifiers.clear();
0096 std::vector<AnnotationItem> annotationItemsNew;
0097 if ( m_annotationItems.size() > 1 ) annotationItemsNew.reserve( m_annotationItems.size() - 1 );
0098 for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) {
0099 if ( iItem == indexToBeRemoved ) continue;
0100 const auto& item = m_annotationItems[iItem];
0101 annotationItemsNew.emplace_back( item.m_key, item.m_value, item.m_sticky );
0102 m_identifiers.emplace( item.m_key, annotationItemsNew.size() - 1 );
0103 }
0104 m_annotationItems = std::move( annotationItemsNew );
0105 return true;
0106 }
0107
0108 inline std::string AIDA::Annotation::value( const std::string& key ) const {
0109 auto iKey = m_identifiers.find( key );
0110 return iKey != m_identifiers.end() ? m_annotationItems[iKey->second].m_value : emptyString;
0111 }
0112
0113 inline void AIDA::Annotation::setValue( const std::string& key, const std::string& value ) {
0114 auto iKey = m_identifiers.find( key );
0115 if ( iKey == m_identifiers.end() )
0116
0117 addItem( key, value );
0118 else
0119 m_annotationItems[iKey->second].m_value = value;
0120 }
0121
0122 inline void AIDA::Annotation::setSticky( const std::string& key, bool sticky ) {
0123 auto iKey = m_identifiers.find( key );
0124 if ( iKey != m_identifiers.end() ) m_annotationItems[iKey->second].m_sticky = sticky;
0125 }
0126
0127 inline int AIDA::Annotation::size() const { return m_annotationItems.size(); }
0128
0129 inline std::string AIDA::Annotation::key( int index ) const {
0130 if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
0131 return m_annotationItems[index].m_key;
0132 }
0133
0134 inline std::string AIDA::Annotation::value( int index ) const {
0135 if ( index < 0 || index >= static_cast<int>( m_annotationItems.size() ) ) return emptyString;
0136 return m_annotationItems[index].m_value;
0137 }
0138
0139 inline void AIDA::Annotation::reset() {
0140
0141 std::vector<std::string> itemsToRemove;
0142 itemsToRemove.reserve( size() );
0143 for ( const auto& item : m_annotationItems ) {
0144 if ( !item.m_sticky ) itemsToRemove.push_back( item.m_key );
0145 }
0146 for ( const auto& i : itemsToRemove ) removeItem( i );
0147 }
0148
0149 #endif