The Gaudi Framework  master (37c0b60a)
SmartRefVector.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 // ====================================================================
12 // SmartRefVector.h
13 // --------------------------------------------------------------------
14 //
15 // Package : Kernel
16 //
17 // Author : Markus Frank
18 //
19 // ====================================================================
20 #ifndef KERNEL_SMARTREFVECTOR_H
21 #define KERNEL_SMARTREFVECTOR_H 1
22 
23 // STL include files
24 #include <vector>
25 
26 // Include files
27 #include <GaudiKernel/SmartRef.h>
28 
29 // forward declare _object and PyObject to avoid including Python.h here
30 struct _object;
31 namespace SmartRefVectorImpl {
32  using PyObject = _object;
33  // Avoid leaking the below into global namespace
35  // newer PyROOT will map SmartRefVector<TYPE>& operator()( ContainedObject* pObj )
36  // to "__getitem__" deleting it via this callback will make `__getitem__` call
37  // the operator [] from the base class
38  // see e.g. https://github.com/root-project/root/issues/7179
39  static void __cppyy_pythonize__( PyObject* klass, const std::string& name );
40  };
41 } // namespace SmartRefVectorImpl
42 
78 template <class TYPE>
80 protected:
87 
89  mutable const DataObject* m_data;
91  mutable const ContainedObject* m_contd;
92 
94  void _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
95  m_data = pObj;
96  m_contd = pContd;
97  for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); ++i ) { ( *i )._setEnvironment( pObj, pContd ); }
98  }
99 
100 public:
101  using SmartRefVectorPythonizer::__cppyy_pythonize__;
102 
105  m_contd = 0;
106  m_data = 0;
107  }
109  template <class ITERATOR>
110  SmartRefVector( ITERATOR first, ITERATOR last )
111  : std::vector<SmartRef<TYPE>>( first, last ), m_data( 0 ), m_contd( 0 ) {}
113  SmartRefVector( const SmartRefVector& copy ) : std::vector<SmartRef<TYPE>>( copy ) { *this = copy; }
115  // virtual ~SmartRefVector() {
116  //}
117 
120  _setEnvironment( ( 0 == pObj ) ? 0 : pObj->parent(), pObj );
121  return *this;
122  }
124  const SmartRefVector<TYPE>& operator()( const ContainedObject* pObj ) const {
125  _setEnvironment( ( 0 == pObj ) ? 0 : pObj->parent(), pObj );
126  return *this;
127  }
130  _setEnvironment( pObj, 0 );
131  return *this;
132  }
134  const SmartRefVector<TYPE>& operator()( const DataObject* pObj ) const {
135  _setEnvironment( pObj, 0 );
136  return *this;
137  }
140  _Base::operator=( copy );
141  // Harms.... MF
142  // on copy we MUST make a 1 to 1 copy
143  // _setEnvironment( copy.m_data, copy.m_contd );
144  // use instead:
145  m_data = copy.m_data;
146  m_contd = copy.m_contd;
147  return *this;
148  }
150  const std::type_info* type() const { return &typeid( TYPE ); }
156  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
157  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRefVector<TYPE>& ptr ) { return ptr.writeRefs( _s ); }
159  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
160  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRefVector<TYPE>& ptr ) { return ptr.readRefs( _s ); }
161 };
162 
163 template <class TYPE>
165  long len = _Base::size();
166  s << len;
167  for ( _BaseConstIter i = _Base::begin(); i != _Base::end(); i++ ) {
168  ( *i )._setEnvironment( m_data, m_contd );
169  ( *i ).writeRef( s );
170  }
171  return s;
172 }
173 
174 template <class TYPE>
176  long len;
177  _Base::erase( _Base::begin(), _Base::end() );
178  s >> len;
179  for ( long i = 0; i < len; i++ ) {
180  _BaseValueType entry;
181  entry._setEnvironment( m_data, m_contd );
182  entry.readRef( s );
183  _Base::push_back( entry );
184  }
185  return s;
186 }
187 
188 #endif // KERNEL_SMARTREFVECTOR_H
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: details.h:252
std::string
STL class.
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
SmartRefVector
Kernel objects: SmartRefVector.
Definition: SmartRefVector.h:79
SmartRefVector::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRefVector< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRefVector.h:157
gaudirun.s
string s
Definition: gaudirun.py:346
SmartRefVector::SmartRefVector
SmartRefVector()
Standard Constructor.
Definition: SmartRefVector.h:104
std::vector
STL class.
std::type_info
SmartRefVector::_BaseValueType
std::vector< _Entry >::value_type _BaseValueType
Definition: SmartRefVector.h:86
SmartRefVectorImpl
Definition: SmartRefVector.h:31
SmartRefVector::operator()
SmartRefVector< TYPE > & operator()(ContainedObject *pObj)
Standard destructor.
Definition: SmartRefVector.h:119
SmartRefVector::operator()
SmartRefVector< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRefVector.h:129
SmartRefVector::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefVector.h:91
StreamBuffer
Definition: StreamBuffer.h:52
SmartRefVector::operator()
const SmartRefVector< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRefVector.h:134
SmartRefVector::SmartRefVector
SmartRefVector(const SmartRefVector &copy)
Copy Constructor.
Definition: SmartRefVector.h:113
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
SmartRefVector::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRefVector.h:150
SmartRefVector::writeRefs
StreamBuffer & writeRefs(StreamBuffer &s) const
Helper to write references.
Definition: SmartRefVector.h:164
SmartRefVector::_BaseConstIter
std::vector< _Entry >::const_iterator _BaseConstIter
Definition: SmartRefVector.h:85
SmartRefVectorImpl::PyObject
_object PyObject
Definition: SmartRefVector.h:32
SmartRefVector::_Base
std::vector< _Entry > _Base
Object types: typedef myself as Base.
Definition: SmartRefVector.h:84
SmartRef.h
SmartRefVector::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefVector.h:89
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:63
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
SmartRefVector::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRefVector< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRefVector.h:160
std::vector::begin
T begin(T... args)
SmartRefVector::SmartRefVector
SmartRefVector(ITERATOR first, ITERATOR last)
templated Constructor
Definition: SmartRefVector.h:110
std
STL namespace.
SmartRefVector::_Entry
SmartRef< TYPE > _Entry
That's the type of crap I am hosting.
Definition: SmartRefVector.h:82
std::vector::operator=
T operator=(T... args)
DataObject
Definition: DataObject.h:36
SmartRefVector::operator=
SmartRefVector< TYPE > & operator=(const SmartRefVector< TYPE > &copy)
Assignment.
Definition: SmartRefVector.h:139
SmartRefVector::operator()
const SmartRefVector< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRefVector.h:124
SmartRefVector::readRefs
StreamBuffer & readRefs(StreamBuffer &s)
Helper to read references.
Definition: SmartRefVector.h:175
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:76
std::vector::end
T end(T... args)
IOTest.end
end
Definition: IOTest.py:125
SmartRefVectorImpl::SmartRefVectorPythonizer
Definition: SmartRefVector.h:34
SmartRefVector::_setEnvironment
void _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment for the vector and all contained objects references.
Definition: SmartRefVector.h:94
ContainedObject
Definition: ContainedObject.h:41
SmartRefVectorImpl::SmartRefVectorPythonizer::__cppyy_pythonize__
static void __cppyy_pythonize__(PyObject *klass, const std::string &name)
Definition: SmartRefVector.cpp:15