| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# Annotation.h

Go to the documentation of this file.

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