Back to home page

EIC code displayed by LXR

 
 

    


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

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 #ifndef GAUDIKERNEL_KEYEDTRAITS_H
0012 #define GAUDIKERNEL_KEYEDTRAITS_H
0013 
0014 #define CHECK_KEYED_CONTAINER
0015 
0016 // Include files
0017 #include "GaudiKernel/Kernel.h" // GAUDI_API
0018 #include <vector>
0019 
0020 // Forward declarations
0021 template <class K>
0022 class KeyedObject;
0023 template <class T, class M>
0024 class KeyedContainer;
0025 
0026 /*
0027   Namespace for Key classes used by the Keyed Container
0028 */
0029 namespace Containers {
0030 
0031   // Status enumeration
0032   enum {
0033     OBJ_NOT_FOUND,    /**< Object not present in the container.              */
0034     OBJ_DELETED,      /**< Object was removed from the container and deleted */
0035     OBJ_ERASED,       /**< Object was removed, but not deleted               */
0036     OBJ_INSERTED,     /**< Object was inserted into the container.           */
0037     OBJ_CANNOT_INSERT /**< Cannot insert object into container.              */
0038   };
0039 
0040   // Forward declarations
0041 
0042   /// Container traits class
0043   template <class CONTAINER, class DATATYPE>
0044   struct traits;
0045   /// Key traits class
0046   template <class KEY>
0047   struct key_traits;
0048   /// Object manager class
0049   template <class SETUP>
0050   class KeyedObjectManager;
0051 
0052   /** Function to be called to indicate that an object cannot be inserted
0053       to the container. Internally an exception is thrown.                    */
0054   GAUDI_API void cannotInsertToContainer();
0055 
0056   /** Function to be called to indicate that the container is found to be
0057       inconsistent. Internally an exception is thrown.                        */
0058   GAUDI_API void containerIsInconsistent();
0059 
0060   /** Function to be called to indicate that an operation should be
0061       performed on the container or it's contained data, which is not
0062       allowed. Internally an exception is thrown.                             */
0063   GAUDI_API void invalidContainerOperation();
0064 
0065   /** Function to be called when an object key cannot be assigned.            */
0066   GAUDI_API void cannotAssignObjectKey();
0067 
0068   /** Definition of the key traits class.
0069 
0070       This is the default class for keys. This implementation
0071       works "as is" for integer, long etc. keys.
0072 
0073       For all other types of keys, this class must be either
0074       partially or completely specialized.
0075 
0076       @author   M.Frank CERN/LHCb
0077       @version  1.0
0078   */
0079   template <class KEY>
0080   struct key_traits {
0081     /// Declaration of key-type
0082     typedef KEY key_type;
0083     /// Declaration of keyed object type
0084     typedef KeyedObject<key_type> obj_type;
0085     /** Create key from its full integer representation.
0086         Not implementing on specialization may inhibit the creation
0087         of keys, i.e. then a key must be supplied at insertion time.
0088     */
0089     static key_type makeKey( long k ) { return key_type( k ); }
0090     static key_type makeKey( int k ) { return key_type( k ); }
0091     /// Full unhashed key identifier
0092     static long identifier( const key_type& k ) { return k; }
0093     /// Hash function for this key
0094     static long hash( const key_type& key_value ) { return key_value; }
0095     /// Set object key when inserted into the container
0096     static void setKey( obj_type* v, const key_type& k ) {
0097       if ( v ) v->setKey( k );
0098     }
0099     /** Check the validity of the object's key.
0100       Select if key-checks should be performed by
0101       switching on/off the macro CHECK_KEYED_CONTAINER.
0102     */
0103     static bool checkKey( obj_type* v, const key_type& k ) {
0104 #ifdef CHECK_KEYED_CONTAINER
0105       return ( v ) ? ( hash( v->key() ) == hash( k ) ) : false;
0106 #else
0107       return true;
0108 #endif
0109     }
0110     /// Add reference counter to object when inserted into the container
0111     static long addRef( obj_type* v ) { return ( v ) ? v->addRef() : 0; }
0112     /// Release reference to object
0113     static long release( obj_type* v ) { return ( v ) ? v->release() : 0; }
0114   };
0115 
0116   /** Definition of the container traits class.
0117 
0118       Select if container-checks should be performed by
0119       switching on/off the macro CHECK_KEYED_CONTAINER.
0120 
0121       @author   M.Frank CERN/LHCb
0122       @version  1.0
0123   */
0124   template <class CONTAINER, class DATATYPE>
0125   struct traits : public key_traits<typename DATATYPE::key_type> {
0126     /// Allow to check the access to container elements for consistency
0127     static bool checkBounds( const std::vector<DATATYPE*>* cnt, const typename DATATYPE::key_type& k ) {
0128 #ifdef CHECK_KEYED_CONTAINER
0129       return size_t( cnt->size() ) > size_t( traits::hash( k ) );
0130 #else
0131       return true;
0132 #endif
0133     }
0134   };
0135 } // namespace Containers
0136 #endif // GAUDIKERNEL_KEYEDTRAITS_H