![]() |
|
|
Generated: 24 Nov 2008 |
00001 /* Emacs: -*- C++ -*- */ 00002 #ifndef GAUDISVC_ANNOTATION_H 00003 #define GAUDISVC_ANNOTATION_H 1 00004 00005 // Include files 00006 #include <vector> 00007 #include <string> 00008 #include <map> 00009 00010 #include "AIDA/IAnnotation.h" 00011 00012 namespace AIDA { 00013 00015 00016 class Annotation : virtual public IAnnotation { 00017 00018 public: 00020 Annotation(){ /* nop */ } 00021 00023 ~Annotation(){ /* nop */ } 00024 00026 bool addItem( const std::string & key, 00027 const std::string & value, 00028 bool sticky = false); 00029 00031 bool removeItem( const std::string & key ); 00032 00034 std::string value( const std::string & key) const; 00035 00037 void setValue( const std::string & key, 00038 const std::string& value); 00039 00041 void setSticky( const std::string & key, 00042 bool sticky); 00043 00045 int size() const; 00046 00048 std::string key(int index) const; 00049 std::string value(int index) const; 00050 00052 void reset(); 00053 00054 private: 00056 class AnnotationItem { 00057 public: 00058 AnnotationItem( std::string k = "", 00059 std::string v = "", 00060 bool vis = true): 00061 m_key( k ), m_value( v ), m_sticky( vis ) 00062 {/* nop */}; 00063 00064 ~AnnotationItem(){ /* nop */}; 00065 00066 std::string m_key; 00067 std::string m_value; 00068 bool m_sticky; 00069 }; 00070 00072 std::vector< AnnotationItem > m_annotationItems; 00073 00075 std::map< std::string, unsigned int > m_identifiers; 00076 00077 std::string emptyString; 00078 }; 00079 } 00080 00081 inline bool AIDA::Annotation::addItem( const std::string & key, 00082 const std::string & value, 00083 bool sticky ) 00084 { 00085 if ( m_identifiers.find( key ) != m_identifiers.end() ) return false; 00086 m_annotationItems.push_back( AnnotationItem( key, value, sticky ) ); 00087 m_identifiers.insert( std::make_pair( key, m_annotationItems.size() - 1 ) ); 00088 return true; 00089 } 00090 00091 inline bool AIDA::Annotation::removeItem( const std::string & key ) { 00092 std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key ); 00093 if ( iKey == m_identifiers.end() ) return false; 00094 00095 unsigned int indexToBeRemoved = iKey->second; 00096 // check stickness 00097 00098 if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false; 00099 00100 // why rebuilding ? 00101 00102 m_identifiers.clear(); 00103 std::vector< AnnotationItem > m_annotationItemsNew; 00104 if ( m_annotationItems.size() > 1 ) m_annotationItemsNew.reserve( m_annotationItems.size() - 1 ); 00105 for ( unsigned int iItem = 0; iItem < m_annotationItems.size(); ++iItem ) { 00106 if ( iItem == indexToBeRemoved ) continue; 00107 const AnnotationItem& item = m_annotationItems[ iItem ]; 00108 m_annotationItemsNew.push_back( AnnotationItem( item.m_key, item.m_value, item.m_sticky ) ); 00109 m_identifiers.insert( std::make_pair( item.m_key, m_annotationItemsNew.size() - 1 ) ); 00110 } 00111 m_annotationItems = m_annotationItemsNew; 00112 return true; 00113 } 00114 00115 inline std::string AIDA::Annotation::value( const std::string & key) const 00116 { 00117 std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key ); 00118 if ( iKey == m_identifiers.end() ) return emptyString; 00119 return ( m_annotationItems[ iKey->second ] ).m_value; 00120 } 00121 00122 inline void AIDA::Annotation::setValue( const std::string & key, const std::string& value) 00123 { 00124 std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key ); 00125 if ( iKey == m_identifiers.end() ) 00126 // not found then add it 00127 addItem(key,value); 00128 else 00129 ( m_annotationItems[ iKey->second ] ).m_value = value; 00130 } 00131 00132 inline void AIDA::Annotation::setSticky( const std::string & key, bool sticky) 00133 { 00134 std::map< std::string, unsigned int >::const_iterator iKey = m_identifiers.find( key ); 00135 if ( iKey == m_identifiers.end() ) return; 00136 ( m_annotationItems[ iKey->second ] ).m_sticky = sticky; 00137 } 00138 00139 inline int AIDA::Annotation::size() const { 00140 return m_annotationItems.size(); 00141 } 00142 00143 inline std::string AIDA::Annotation::key(int index) const 00144 { 00145 if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString; 00146 return ( m_annotationItems[ index ] ).m_key; 00147 } 00148 00149 inline std::string AIDA::Annotation::value(int index) const 00150 { 00151 if ( index < 0 || index >= static_cast<int>(m_annotationItems.size()) ) return emptyString; 00152 return ( m_annotationItems[ index ] ).m_value; 00153 } 00154 00155 inline void AIDA::Annotation::reset() 00156 { 00157 // Collect the non-sticky items 00158 std::vector< std::string > itemsToRemove; 00159 itemsToRemove.reserve( size() ); 00160 for ( int item = 0; item < size(); ++item ) { 00161 if ( ! ( m_annotationItems[ item ] ).m_sticky ) { 00162 itemsToRemove.push_back( ( m_annotationItems[ item ] ).m_key ); 00163 } 00164 } 00165 00166 for ( unsigned int i = 0; i < itemsToRemove.size(); ++i ) removeItem( itemsToRemove[i] ); 00167 } 00168 00169 #endif