| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

KeyedContainer.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_KEYEDCONTAINER_H
2 #define GAUDIKERNEL_KEYEDCONTAINER_H
3
4 // Include files
5 #include <algorithm>
6 #include <iterator>
7
8 namespace GaudiDict
9 {
10 template <class T>
11 struct KeyedContainerDict;
12 }
13
14 // Framework include files
15 #include "GaudiKernel/KeyedObject.h"
16 #include "GaudiKernel/KeyedObjectManager.h"
17 #include "GaudiKernel/ObjectContainerBase.h"
18
19 // Forward declarations
20 // template <class T, class M> class KeyedContainer;
21
22 #ifdef WIN32
23 #define FORCE_INLINE __forceinline
24 #else
25 #define FORCE_INLINE inline
26 #endif
27
64 template <class DATATYPE, class MAPPING = Containers::HashMap>
65 class GAUDI_API KeyedContainer : public ObjectContainerBase
66 {
67 friend struct GaudiDict::KeyedContainerDict<DATATYPE>;
68
69 public:
71 typedef DATATYPE contained_type;
73 typedef MAPPING container_type;
74
81 typedef typename std::vector<contained_type*> seq_type;
84 typedef typename contained_type::key_type key_type;
86 typedef typename seq_type::value_type value_type;
88 typedef typename seq_type::reference reference;
90 typedef typename seq_type::const_reference const_reference;
92 typedef typename seq_type::iterator iterator;
94 typedef typename seq_type::const_iterator const_iterator;
96 typedef typename seq_type::reverse_iterator reverse_iterator;
100 typedef typename seq_type::const_reverse_iterator const_reverse_iterator;
102 private:
106 typedef typename Containers::traits<container_type, contained_type> traits;
107
111 container_type m_cont;
114 seq_type m_sequential;
116 seq_type* m_random;
117
119 #ifdef CHECK_KEYED_CONTAINER
120 value_type i_object( const key_type& k ) const
121 {
122 if ( 0 == m_cont.isDirect() ) {
123 if ( traits::checkBounds( m_random, k ) ) {
124 value_type p = *( m_random->begin() + traits::hash( k ) );
125 if ( traits::checkKey( p, k ) ) {
126 return p;
127 }
128 }
129 return 0;
130 }
131 value_type p = value_type( m_cont.object( traits::hash( k ) ) );
132 return traits::checkKey( p, k ) ? p : 0;
133 }
134 #else
135 FORCE_INLINE value_type i_object( const key_type& k ) const
136 {
137 return 0 == m_cont.isDirect() ? value_type( *( m_random->begin() + traits::hash( k ) ) )
138 : value_type( m_cont.object( traits::hash( k ) ) );
139 }
140 #endif
141 long i_erase( const_reference v, const key_type& k )
143 {
144 value_type p = value_type( m_cont.erase( traits::hash( k ), v ) );
145 if ( p ) {
146 if ( p->parent() == this ) {
147 p->setParent( 0 );
148 }
149 }
150 return traits::release( p ) <= 0 ? (long)Containers::OBJ_ERASED : (long)Containers::OBJ_DELETED;
151 }
152
154 struct _InsertRelease {
155 KeyedContainer<DATATYPE, MAPPING>* m_obj;
156 _InsertRelease( KeyedContainer<DATATYPE, MAPPING>* p ) : m_obj( p ) {}
157 void operator()( value_type p )
158 {
159 m_obj->insert( p );
160 traits::release( p );
161 }
162 };
163
165 struct _RemoveRelease {
166 ObjectContainerBase* m_obj;
167 _RemoveRelease( ObjectContainerBase* p ) : m_obj( p ) {}
168 void operator()( value_type p )
169 {
170 const ObjectContainerBase* par = p->parent();
171 if ( par == m_obj ) {
172 p->setParent( 0 );
173 }
174 traits::release( p );
175 }
176 };
178
179 public:
183 KeyedContainer( void )
185 {
186 // avoid problems with strict-aliasing rules
187 seq_type** rptr = &m_random;
188 seq_type* sptr = &m_sequential;
189 m_cont.setup( (void*)sptr, (void**)rptr );
190 }
191 KeyedContainer( KeyedContainer&& other )
192 : m_cont( std::move( other.m_cont ) ), m_sequential( std::move( other.m_sequential ) )
193 {
194 m_cont.setup( (void*)&m_sequential, (void**)&m_random );
195 std::for_each( begin(), end(), [this]( ContainedObject* obj ) { obj->setParent( this ); } );
196
197 other.m_cont.setup( (void*)&other.m_sequential, (void**)&other.m_random );
198 }
199 KeyedContainer( const KeyedContainer& ) = delete;
201 ~KeyedContainer() override;
203
209 const CLID& clID() const override { return this->classID(); }
212 static const CLID& classID()
213 {
214 static CLID clid = contained_type::classID() + container_type::classID();
215 return clid;
216 }
218
235 size_type numberOfObjects() const override { return m_sequential.size(); }
248 long add( ContainedObject* pObject ) override;
249
262 long remove( ContainedObject* pObject ) override;
263
267 ContainedObject* containedObject( long key_value ) const override { return i_object( traits::makeKey( key_value ) ); }
271 long index( const ContainedObject* p ) const override;
277 virtual size_type containedObjects( std::vector<ContainedObject*>& v ) const;
279
285 size_type size() const { return m_sequential.size(); }
288 bool empty() const { return m_sequential.empty(); }
290 void reserve( size_type value ) { m_cont.reserve( value ); }
292 void clear() { erase( begin(), end() ); }
298 virtual const std::vector<const ContainedObject*>* containedObjects() const;
303 StatusCode update() override;
305
319 iterator begin() { return m_sequential.begin(); }
322 const_iterator begin() const { return m_sequential.begin(); }
324 iterator end() { return m_sequential.end(); }
326 const_iterator end() const { return m_sequential.end(); }
328 reverse_iterator rbegin() { return m_sequential.rbegin(); }
330 const_reverse_iterator rbegin() const { return m_sequential.rbegin(); }
332 reverse_iterator rend() { return m_sequential.rend(); }
334 const_reverse_iterator rend() const { return m_sequential.rend(); }
336
354 value_type object( const key_type& kval ) const { return i_object( kval ); }
355
365 value_type operator()( const key_type& kval ) const { return i_object( kval ); }
367
390 long erase( const key_type& kval ) { return i_erase( 0, kval ); }
391
412 long erase( const value_type val ) { return ( val ) ? i_erase( val, val->key() ) : (long)Containers::OBJ_NOT_FOUND; }
413
434 long erase( iterator pos ) { return erase( *pos ); }
435
444 void erase( iterator pos_start, iterator pos_stop, bool use_temp = false );
445
464 const key_type& insert( const value_type val, const key_type& kval );
465
487 const key_type& insert( const value_type val );
489 };
490
498 // Destructor
499 template <class DATATYPE, class MAPPING>
500 inline KeyedContainer<DATATYPE, MAPPING>::~KeyedContainer()
501 {
502 clear();
503 m_cont.clear();
504 }
505
506 // Configure direct access
507 template <class DATATYPE, class MAPPING>
508 inline StatusCode KeyedContainer<DATATYPE, MAPPING>::update()
509 {
510 int count = 0;
511 m_cont.clearDirect();
512 typename seq_type::iterator i = m_sequential.begin();
513 typename seq_type::iterator s = m_sequential.end();
514 for ( ; i != s; i++ ) {
515 typename seq_type::value_type v = *i;
516 if ( v ) {
517 if ( !v->hasKey() ) {
518 traits::setKey( v, v->key() );
519 traits::addRef( v );
520 }
521 long k0 = traits::hash( v->key() );
522 if ( m_cont.insertDirect( this, v, v, k0 ) == Containers::OBJ_INSERTED ) {
523 }
524 } else {
525 ++count;
526 }
527 }
528 if ( count > 0 ) {
529 Containers::cannotInsertToContainer();
530 }
531 return StatusCode::SUCCESS;
532 }
533
534 // Retrieve the full content of the object container by reference.
535 template <class DATATYPE, class MAPPING>
536 inline const std::vector<const ContainedObject*>* KeyedContainer<DATATYPE, MAPPING>::containedObjects() const
537 {
538 return (const std::vector<const ContainedObject*>*)( ( 0 == m_cont.isDirect() ) ? m_random : &m_sequential );
539 }
540
541 template <class DATATYPE, class MAPPING>
542 inline const typename KeyedContainer<DATATYPE, MAPPING>::key_type&
543 KeyedContainer<DATATYPE, MAPPING>::insert( const value_type val, const key_type& kval )
544 {
545 if ( val ) {
546 long k0 = traits::hash( kval );
547 if ( !val->hasKey() || ( traits::hash( val->key() ) == k0 ) ) {
548 if ( m_cont.insert( this, val, val, k0 ) == Containers::OBJ_INSERTED ) {
549 if ( !val->hasKey() ) traits::setKey( val, kval );
550 traits::addRef( val );
551 return val->key();
552 }
553 }
554 }
555 // Cannot insert object...indicate bad object insertion...
556 Containers::cannotInsertToContainer();
557 return val->key();
558 }
559
560 // Insert object
561 template <class DATATYPE, class MAPPING> // inline
562 const typename KeyedContainer<DATATYPE, MAPPING>::key_type&
563 KeyedContainer<DATATYPE, MAPPING>::insert( const value_type val )
564 {
565 if ( 0 != val ) {
566 if ( val->hasKey() ) {
567 if ( m_cont.insert( this, val, val, traits::hash( val->key() ) ) == Containers::OBJ_INSERTED ) {
568 traits::addRef( val );
569 return val->key();
570 }
571 }
572 long k0;
573 if ( m_cont.insert( this, val, val, &k0 ) == Containers::OBJ_INSERTED ) {
574 traits::setKey( val, traits::makeKey( k0 ) );
575 traits::addRef( val );
576 return val->key();
577 }
578 }
579 // Cannot insert object...indicate bad object insertion...
580 Containers::cannotInsertToContainer();
581 return val->key();
582 }
583
584 template <class DATATYPE, class MAPPING>
585 inline long KeyedContainer<DATATYPE, MAPPING>::index( const ContainedObject* p ) const
586 {
587 const contained_type* ptr = dynamic_cast<const contained_type*>( p );
588 if ( ptr ) return traits::identifier( ptr->key() );
589 return -1;
590 }
591
592 // Retrieve the full content of the object container.
593 template <class DATATYPE, class MAPPING>
594 inline typename KeyedContainer<DATATYPE, MAPPING>::size_type
595 KeyedContainer<DATATYPE, MAPPING>::containedObjects( std::vector<ContainedObject*>& vec ) const
596 {
597 typename seq_type::const_iterator i = m_sequential.begin();
598 typename seq_type::const_iterator s = m_sequential.end();
599 vec.clear();
600 vec.reserve( size() );
601 for ( ; i != s; i++ ) {
602 ContainedObject* p = const_cast<typename seq_type::value_type>( *i );
603 vec.push_back( p );
604 }
605 return vec.size();
606 }
607
608 // ObjectContainerBase overload: Add an object to the container.
609 template <class DATATYPE, class MAPPING>
610 inline long KeyedContainer<DATATYPE, MAPPING>::add( ContainedObject* pObject )
611 {
612 return traits::identifier( insert( dynamic_cast<typename seq_type::value_type>( pObject ) ) );
613 }
614
615 // ObjectContainerBase overload: Remove an object from the container.
616 template <class DATATYPE, class MAPPING>
617 inline long KeyedContainer<DATATYPE, MAPPING>::remove( ContainedObject* p )
618 {
619 contained_type* p1 = dynamic_cast<contained_type*>( p );
620 if ( p1 ) { // Normal case; object still fully intact
621 return this->erase( p1 );
622 } else if ( p ) {
623 const ObjectContainerBase* par = p->parent();
624 // The following should never occur: object is in a funny state,
625 // Because the parent was explicitly set to NULL in the
626 // KeyeObject destructor.
627 // - It cannot be a KeyedObject: It would not have a parent
628 // - Still the parent is present: We are not in the destructor
629 // of KeyedObject
630 if ( par ) {
631 Containers::invalidContainerOperation();
632 }
633 return m_cont.erase( 0, p ) == 0 ? (long)Containers::OBJ_ERASED : (long)Containers::OBJ_NOT_FOUND;
634 }
635 return (long)Containers::OBJ_NOT_FOUND;
636 }
637
638 template <class DATATYPE, class MAPPING>
639 inline void KeyedContainer<DATATYPE, MAPPING>::erase( iterator start_pos, iterator stop_pos, bool use_tmp )
640 {
641 bool is_start = start_pos == m_sequential.begin();
642 bool is_stop = stop_pos == m_sequential.end();
643 if ( is_start && is_stop ) {
644 // Nothing special. Taken care of by Keyed object manager
645 } else if ( is_start || is_stop || use_tmp ) {
646 std::vector<DATATYPE*> tmp( m_sequential.begin(), start_pos );
647 tmp.insert( tmp.end(), stop_pos, m_sequential.end() );
648 std::for_each( tmp.begin(), tmp.end(), traits::addRef );
649 this->erase( m_sequential.begin(), m_sequential.end() );
650 std::for_each( tmp.begin(), tmp.end(), _InsertRelease( this ) );
651 return;
652 }
653 std::for_each( start_pos, stop_pos, _RemoveRelease( this ) );
654 seq_type* sptr = &m_sequential; // avoid problems with strict-aliasing rules
655 std::vector<void*>* v = (std::vector<void*>*)sptr;
656 std::vector<void*>::iterator i1 = v->begin() + std::distance( m_sequential.begin(), start_pos );
657 std::vector<void*>::iterator i2 = v->begin() + std::distance( m_sequential.begin(), stop_pos );
658 m_cont.erase( i1, i2 );
659 }
660
661 #undef FORCE_INLINE
662 #endif // GAUDIKERNEL_KEYEDCONTAINER_H
KeyedContainer::containedObjects
virtual const std::vector< const ContainedObject * > * containedObjects() const
Retrieve the full content of the object container by reference.
Definition: KeyedContainer.h:536
GaudiDict::KeyedContainerDict
Definition: KeyedContainer.h:11
KeyedContainer::_InsertRelease::operator()
void operator()(value_type p)
Definition: KeyedContainer.h:157
KeyedContainer::rbegin
reverse_iterator rbegin()
reverse_iterator returns the beginning of the reversed container
Definition: KeyedContainer.h:328
StatusCode::SUCCESS
Definition: StatusCode.h:29
KeyedContainer::reverse_iterator
seq_type::reverse_iterator reverse_iterator
Sequential access: reverse iterator type used in sequential container.
Definition: KeyedContainer.h:96
std::vector::empty
T empty(T...args)
Containers::traits
Container traits class.
Definition: KeyedTraits.h:35
KeyedContainer::const_reference
seq_type::const_reference const_reference
Sequential access: const reference type used in sequential container.
Definition: KeyedContainer.h:90
KeyedContainer::reserve
void reserve(size_type value)
Reserve place for "value" objects in the container.
Definition: KeyedContainer.h:290
KeyedContainer::object
value_type object(const key_type &kval) const
Object access by key.
Definition: KeyedContainer.h:354
std::distance
T distance(T...args)
KeyedContainer::erase
long erase(iterator pos)
Remove/erase object (identified by iterator) from the container.
Definition: KeyedContainer.h:434
KeyedContainer::add
long add(ContainedObject *pObject) override
ObjectContainerBase overload: Add an object to the container.
Definition: KeyedContainer.h:610
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:30
KeyedContainer::rbegin
const_reverse_iterator rbegin() const
const reverse_iterator returns the beginning of the reversed container
Definition: KeyedContainer.h:330
std::vector::rend
T rend(T...args)
KeyedContainer::m_sequential
seq_type m_sequential
Array to allow sequential access to the object (can be ordered).
Definition: KeyedContainer.h:114
KeyedContainer::reference
seq_type::reference reference
Sequential access: reference type used in sequential container.
Definition: KeyedContainer.h:88
Gaudi::Functional::details::insert
constexpr struct Gaudi::Functional::details::insert_t insert
KeyedContainer::_RemoveRelease::m_obj
ObjectContainerBase * m_obj
Definition: KeyedContainer.h:166
KeyedContainer
template class KeyedContainer, KeyedContainer.h
Definition: KeyedContainer.h:65
std
STL namespace.
ObjectContainerBase.h
Containers::OBJ_NOT_FOUND
Object not present in the container.
Definition: KeyedTraits.h:24
KeyedContainer::erase
long erase(const key_type &kval)
Remove/erase object (identified by key) from the container.
Definition: KeyedContainer.h:390
KeyedContainer::remove
long remove(ContainedObject *pObject) override
ObjectContainerBase overload: Remove an object from the container.
Definition: KeyedContainer.h:617
std::vector::end
T end(T...args)
KeyedContainer::traits
Containers::traits< container_type, contained_type > traits
Traits class definition.
Definition: KeyedContainer.h:106
KeyedContainer::classID
static const CLID & classID()
Retrieve class ID.
Definition: KeyedContainer.h:212
KeyedContainer::iterator
seq_type::iterator iterator
Sequential access: iterator type used in sequential container.
Definition: KeyedContainer.h:92
KeyedContainer::_InsertRelease
Internal functor for insertion of objects.
Definition: KeyedContainer.h:154
KeyedContainer::contained_type
DATATYPE contained_type
Definition of the contained object type.
Definition: KeyedContainer.h:71
KeyedContainer::operator()
value_type operator()(const key_type &kval) const
STL algorithms support for object access.
Definition: KeyedContainer.h:365
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:52
KeyedContainer::insert
const key_type & insert(const value_type val, const key_type &kval)
Insert entry to the container with a valid key.
Definition: KeyedContainer.h:543
KeyedContainer::_InsertRelease::m_obj
KeyedContainer< DATATYPE, MAPPING > * m_obj
Definition: KeyedContainer.h:155
Containers::cannotInsertToContainer
GAUDI_API void cannotInsertToContainer()
Function to be called to indicate that an object cannot be inserted to the container.
Definition: KeyedObjectManager.cpp:78
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
KeyedContainer::rend
const_reverse_iterator rend() const
const reverse_iterator pointing to the end of the reversed container
Definition: KeyedContainer.h:334
KeyedContainer::empty
bool empty() const
For consistency with STL: check if container is empty.
Definition: KeyedContainer.h:288
KeyedContainer::index
long index(const ContainedObject *p) const override
ObjectContainerBase overload: Retrieve the full long integer representation of the object&#39;s key from ...
Definition: KeyedContainer.h:585
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:39
std::vector::push_back
T push_back(T...args)
KeyedContainer::rend
reverse_iterator rend()
reverse_iterator pointing to the end of the reversed container
Definition: KeyedContainer.h:332
KeyedContainer::_RemoveRelease::_RemoveRelease
_RemoveRelease(ObjectContainerBase *p)
Definition: KeyedContainer.h:167
compareOutputFiles.par
string par
Definition: compareOutputFiles.py:437
Containers::invalidContainerOperation
GAUDI_API void invalidContainerOperation()
Function to be called to indicate that an operation should be performed on the container or it&#39;s cont...
Definition: KeyedObjectManager.cpp:88
KeyedContainer::i_object
FORCE_INLINE value_type i_object(const key_type &k) const
Internal function to access objects within the container.
Definition: KeyedContainer.h:135
KeyedContainer::KeyedContainer
KeyedContainer(KeyedContainer &&other)
Definition: KeyedContainer.h:191
KeyedContainer::update
StatusCode update() override
Reconfigure direct access to elements (Needed by POOL data loading) This function reuses the "update"...
Definition: KeyedContainer.h:508
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
KeyedContainer::end
const_iterator end() const
Retrieve terminating const iterator.
Definition: KeyedContainer.h:326
KeyedContainer::value_type
seq_type::value_type value_type
Sequential access: definition of type stored in sequential container.
Definition: KeyedContainer.h:86
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
KeyedContainer::containedObject
ContainedObject * containedObject(long key_value) const override
ObjectContainerBase overload: Retrieve the object by reference given the long integer representation ...
Definition: KeyedContainer.h:267
KeyedContainer::const_iterator
seq_type::const_iterator const_iterator
Sequential access: const iterator type used in sequential container.
Definition: KeyedContainer.h:94
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
std::vector::clear
T clear(T...args)
FORCE_INLINE
#define FORCE_INLINE
Definition: KeyedContainer.h:25
Containers::OBJ_ERASED
Object was removed, but not deleted.
Definition: KeyedTraits.h:26
std::vector::insert
T insert(T...args)
KeyedObject.h
KeyedContainer::~KeyedContainer
~KeyedContainer() override
Destructor.
Definition: KeyedContainer.h:500
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
std::vector::size
T size(T...args)
std::vector< contained_type * >
Containers::OBJ_INSERTED
Object was inserted into the container.
Definition: KeyedTraits.h:27
std::vector::begin
T begin(T...args)
KeyedContainer::erase
long erase(const value_type val)
Remove/erase object (identified by pointer value) from the container.
Definition: KeyedContainer.h:412
KeyedContainer::_InsertRelease::_InsertRelease
_InsertRelease(KeyedContainer< DATATYPE, MAPPING > *p)
Definition: KeyedContainer.h:156
KeyedContainer::end
iterator end()
Retrieve terminating iterator.
Definition: KeyedContainer.h:324
ContainedObject::setParent
void setParent(ObjectContainerBase *value)
Update parent member.
Definition: ContainedObject.h:54
KeyedContainer::key_type
contained_type::key_type key_type
Definition of the key type: re-use definition of contained type.
Definition: KeyedContainer.h:84
KeyedContainer::_RemoveRelease
Internal functor for insertion of objects.
Definition: KeyedContainer.h:165
gaudirun.s
string s
Definition: gaudirun.py:253
KeyedContainer::begin
const_iterator begin() const
Retrieve start const iterator.
Definition: KeyedContainer.h:322
Containers::OBJ_DELETED
Object was removed from the container and deleted.
Definition: KeyedTraits.h:25
ObjectContainerBase
ObjectContainerBase is the base class for Gaudi container classes.
Definition: ObjectContainerBase.h:19
KeyedContainer::_RemoveRelease::operator()
void operator()(value_type p)
Definition: KeyedContainer.h:168
KeyedContainer::const_reverse_iterator
seq_type::const_reverse_iterator const_reverse_iterator
Sequential access: const reverse iterator type used in sequential container.
Definition: KeyedContainer.h:100
KeyedObjectManager.h
std::for_each
T for_each(T...args)
GaudiDict
Definition: KeyedContainer.h:8
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
KeyedContainer::container_type
MAPPING container_type
Definition of the implementing container type.
Definition: KeyedContainer.h:73
GaudiPython.Pythonizations.update
update
Definition: Pythonizations.py:123
KeyedContainer::m_random
seq_type * m_random
Array to allow random access to objects (not exposed)
Definition: KeyedContainer.h:116
std::vector::reserve
T reserve(T...args)
KeyedContainer::clear
void clear()
Clear the entire content and erase the objects from the container.
Definition: KeyedContainer.h:292
std::vector::rbegin
T rbegin(T...args)