Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ElementProxy.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 /**
5  * @file AthContainers/tools/ElementProxy.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Sep, 2010
8  * @brief Proxy for lvalue access to @c DataVector/@c DataList elements.
9  */
10 
11 
12 #include "AthContainers/tools/DVLCast.h"
13 #include "AthContainers/tools/ATHCONTAINERS_ASSERT.h"
14 
15 
16 namespace DataModel_detail {
17 
18 
19 /**
20  * @brief Constructor.
21  * @param i The underlying container iterator pointing at
22  * the element which we're proxying.
23  * @param container The container that the iterator references.
24  */
25 template <class DVL>
26 inline
27 ElementProxy<DVL>::ElementProxy (typename DVL::BaseContainer::iterator i,
28  DVL* container)
29  : m_proxied (i),
30  m_container (container)
31 {
32 }
33 
34 
35 /**
36  * @brief Assignment operator, from an @c Element proxy.
37  * @param rhs The proxy from which we're assigning.
38  *
39  * If @a rhs is the same as the element we're proxying, then
40  * we don't need to do anything. Otherwise, @c can_insert must
41  * be true. If the parent @c DataVector/List owns its elements,
42  * we then need to delete the proxied object before making
43  * the assignment. We also disallow copying between two
44  * @c DataVector/List's, both of which own their elements.
45  */
46 template <class DVL>
47 // cppcheck-suppress operatorEqVarError
48 ElementProxy<DVL>& ElementProxy<DVL>::operator= (const ElementProxy& rhs)
49 {
50  static_assert (!DVL::must_own);
51  if (*rhs.m_proxied != *m_proxied) {
52  // cppcheck-suppress assertWithSideEffect
53  ATHCONTAINERS_ASSERT (! (container()->ownPolicy() == SG::OWN_ELEMENTS &&
54  rhs.container()->ownPolicy() == SG::OWN_ELEMENTS));
55  container()->assignBaseElement (m_proxied, *rhs.m_proxied);
56  }
57  return *this;
58 }
59 
60 
61 /**
62  * @brief Assignment operator, from a pointer.
63  * @param rhs The pointer from which we're assigning.
64  *
65  * If @a rhs is the same as the element we're proxying, then
66  * we don't need to do anything. Otherwise, @c can_insert must
67  * be true. If the parent @c DataVector/List owns its elements,
68  * we then need to delete the proxied object before making
69  * the assignment.
70  */
71 template <class DVL>
72 template <bool>
73 requires(!DVL::must_own)
74 ElementProxy<DVL>&
75 ElementProxy<DVL>::operator= (typename DVL::value_type rhs)
76 {
77  if (rhs != *m_proxied)
78  m_container->assignElement(m_proxied, rhs);
79  return *this;
80 }
81 
82 
83 /**
84  * @brief Assignment operator, from a pointer.
85  * @param rhs The pointer from which we're assigning.
86  *
87  * If @a rhs is the same as the element we're proxying, then
88  * we don't need to do anything. Otherwise, @c can_insert must
89  * be true. The container must own its elements in order
90  * to use this interface.
91  */
92 template <class DVL>
93 ElementProxy<DVL>&
94 ElementProxy<DVL>::operator= (typename DVL::unique_type rhs)
95 {
96  if (rhs.get() != *m_proxied)
97  m_container->assignElement(m_proxied, std::move(rhs));
98  return *this;
99 }
100 
101 
102 /**
103  * @brief Conversion to a (const) pointer.
104  *
105  * We just need to do a cast here.
106  */
107 template <class DVL>
108 inline
109 ElementProxy<DVL>::operator typename DVL::value_type const() const
110 {
111  return DataModel_detail::DVLCast<DVL>::cast (*m_proxied);
112 }
113 
114 
115 /**
116  * @brief Conversion to a (const) pointer.
117  *
118  * We just need to do a cast here.
119  */
120 template <class DVL>
121 inline
122 typename DVL::value_type const ElementProxy<DVL>::operator-> () const
123 {
124  return DataModel_detail::DVLCast<DVL>::cast (*m_proxied);
125 }
126 
127 
128 /**
129  * @brief Return the container holding the element that this object proxies.
130  */
131 template <class DVL>
132 inline
133 DVL* ElementProxy<DVL>::container()
134 {
135  return m_container;
136 }
137 
138 
139 /**
140  * @brief Return the container holding the element that this object proxies.
141  */
142 template <class DVL>
143 inline
144 const DVL* ElementProxy<DVL>::container() const
145 {
146  return m_container;
147 }
148 
149 
150 } // namespace DataModel_detail