Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:26

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 /* Emacs: -*- C++ -*- */
0012 #ifndef GAUDISVC_ANNOTATION_H
0013 #define GAUDISVC_ANNOTATION_H 1
0014 
0015 // Include files
0016 #include <map>
0017 #include <string>
0018 #include <vector>
0019 
0020 #include "AIDA/IAnnotation.h"
0021 
0022 namespace AIDA {
0023 
0024   ///  Implementation of the AIDA IAnnotation interface class
0025 
0026   class Annotation : virtual public IAnnotation {
0027 
0028   public:
0029     /// Add a key/value pair with a given sticky.
0030     bool addItem( const std::string& key, const std::string& value, bool sticky = false ) override;
0031 
0032     /// Remove the item indicated by a given key
0033     bool removeItem( const std::string& key ) override;
0034 
0035     /// Retrieve the value for a given key
0036     std::string value( const std::string& key ) const override;
0037 
0038     /// Set value for a given key
0039     void setValue( const std::string& key, const std::string& value ) override;
0040 
0041     /// Set sticky for a given key
0042     void setSticky( const std::string& key, bool sticky ) override;
0043 
0044     /// Get the number of items in the Annotation
0045     int size() const override;
0046 
0047     /// Individual access to the Annotation-items
0048     std::string key( int index ) const override;
0049     std::string value( int index ) const override;
0050 
0051     /// Remove all the non-sticky items
0052     void reset() override;
0053 
0054   private:
0055     /// Internal private annotation item class
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 ){ /* nop */ };
0059 
0060       ~AnnotationItem() = default;
0061 
0062       std::string m_key;
0063       std::string m_value;
0064       bool        m_sticky;
0065     };
0066 
0067     /// The vector of the annotation items
0068     std::vector<AnnotationItem> m_annotationItems;
0069 
0070     /// The map of strings to identifiers
0071     std::map<std::string, unsigned int> m_identifiers;
0072 
0073     std::string emptyString;
0074   };
0075 } // namespace AIDA
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   // check stickness
0090 
0091   if ( m_annotationItems[indexToBeRemoved].m_sticky ) return false;
0092 
0093   // why rebuilding ?
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     // if not found, then add it
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   // Collect the non-sticky items
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