The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
SmartRefVector.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
14#include <vector>
15
16// forward declare _object and PyObject to avoid including Python.h here
17struct _object;
19 using PyObject = _object;
20 // Avoid leaking the below into global namespace
22 // newer PyROOT will map SmartRefVector<TYPE>& operator()( ContainedObject* pObj )
23 // to "__getitem__" deleting it via this callback will make `__getitem__` call
24 // the operator [] from the base class
25 // see e.g. https://github.com/root-project/root/issues/7179
26 static void __cppyy_pythonize__( PyObject* klass, const std::string& name );
27 };
28} // namespace SmartRefVectorImpl
29
65template <class TYPE>
66class SmartRefVector : public std::vector<SmartRef<TYPE>>, SmartRefVectorImpl::SmartRefVectorPythonizer {
67protected:
71 typedef std::vector<_Entry> _Base;
72 typedef typename std::vector<_Entry>::const_iterator _BaseConstIter;
73 typedef typename std::vector<_Entry>::value_type _BaseValueType;
74
76 mutable const DataObject* m_data;
78 mutable const ContainedObject* m_contd;
79
81 void _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
82 m_data = pObj;
83 m_contd = pContd;
84 for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); ++i ) { ( *i )._setEnvironment( pObj, pContd ); }
85 }
86
87public:
88 using SmartRefVectorPythonizer::__cppyy_pythonize__;
89
91 m_contd = 0;
92 m_data = 0;
93 }
94 template <class ITERATOR>
95 SmartRefVector( ITERATOR first, ITERATOR last )
96 : std::vector<SmartRef<TYPE>>( first, last ), m_data( 0 ), m_contd( 0 ) {}
97 SmartRefVector( const SmartRefVector& copy ) : std::vector<SmartRef<TYPE>>( copy ) { *this = copy; }
98
101 _setEnvironment( ( 0 == pObj ) ? 0 : pObj->parent(), pObj );
102 return *this;
103 }
104
105 const SmartRefVector<TYPE>& operator()( const ContainedObject* pObj ) const {
106 _setEnvironment( ( 0 == pObj ) ? 0 : pObj->parent(), pObj );
107 return *this;
108 }
109
111 _setEnvironment( pObj, 0 );
112 return *this;
113 }
114
115 const SmartRefVector<TYPE>& operator()( const DataObject* pObj ) const {
116 _setEnvironment( pObj, 0 );
117 return *this;
118 }
119
121 _Base::operator=( copy );
122 // Harms.... MF
123 // on copy we MUST make a 1 to 1 copy
124 // _setEnvironment( copy.m_data, copy.m_contd );
125 // use instead:
126 m_data = copy.m_data;
127 m_contd = copy.m_contd;
128 return *this;
129 }
130
131 const std::type_info* type() const { return &typeid( TYPE ); }
137 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
138 friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRefVector<TYPE>& ptr ) { return ptr.writeRefs( _s ); }
140 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
141 friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRefVector<TYPE>& ptr ) { return ptr.readRefs( _s ); }
142};
143
144template <class TYPE>
146 long len = _Base::size();
147 s << len;
148 for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); i++ ) {
149 ( *i )._setEnvironment( m_data, m_contd );
150 ( *i ).writeRef( s );
151 }
152 return s;
153}
154
155template <class TYPE>
157 long len;
158 _Base::erase( _Base::begin(), _Base::end() );
159 s >> len;
160 for ( long i = 0; i < len; i++ ) {
161 _BaseValueType entry;
162 entry._setEnvironment( m_data, m_contd );
163 entry.readRef( s );
164 _Base::push_back( entry );
165 }
166 return s;
167}
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
const ObjectContainerBase * parent() const
Access to parent object.
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
Kernel objects: SmartRef.
Definition SmartRef.h:64
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
StreamBuffer & readRefs(StreamBuffer &s)
Helper to read references.
std::vector< _Entry >::const_iterator _BaseConstIter
SmartRefVector< TYPE > & operator=(const SmartRefVector< TYPE > &copy)
Assignment.
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
void _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment for the vector and all contained objects references.
const std::type_info * type() const
Access to embedded type.
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRefVector< TYPE > &ptr)
Input Streamer operator.
SmartRefVector< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
std::vector< _Entry > _Base
Object types: typedef myself as Base.
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRefVector< TYPE > &ptr)
Output Streamer operator.
SmartRefVector(ITERATOR first, ITERATOR last)
const SmartRefVector< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
SmartRefVector< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
SmartRefVector(const SmartRefVector &copy)
std::vector< _Entry >::value_type _BaseValueType
const SmartRefVector< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
SmartRef< TYPE > _Entry
That's the type of crap I am hosting.
StreamBuffer & writeRefs(StreamBuffer &s) const
Helper to write references.
The stream buffer is a small object collecting object data.
STL class.
STL namespace.
static void __cppyy_pythonize__(PyObject *klass, const std::string &name)