ATLAS Offline Software
ElementProxy.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 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 ATH_MEMBER_REQUIRES_DEF(!DVL::must_own, ElementProxy<DVL>&)
73 ElementProxy<DVL>::operator= (typename DVL::value_type rhs)
74 {
75  if (rhs != *m_proxied)
76  m_container->assignElement(m_proxied, rhs);
77  return *this;
78 }
79 
80 
81 /**
82  * @brief Assignment operator, from a pointer.
83  * @param rhs The pointer from which we're assigning.
84  *
85  * If @a rhs is the same as the element we're proxying, then
86  * we don't need to do anything. Otherwise, @c can_insert must
87  * be true. The container must own its elements in order
88  * to use this interface.
89  */
90 template <class DVL>
91 ElementProxy<DVL>&
92 ElementProxy<DVL>::operator= (typename DVL::unique_type rhs)
93 {
94  if (rhs.get() != *m_proxied)
95  m_container->assignElement(m_proxied, std::move(rhs));
96  return *this;
97 }
98 
99 
100 /**
101  * @brief Conversion to a (const) pointer.
102  *
103  * We just need to do a cast here.
104  */
105 template <class DVL>
106 inline
107 ElementProxy<DVL>::operator typename DVL::value_type const() const
108 {
109  return DataModel_detail::DVLCast<DVL>::cast (*m_proxied);
110 }
111 
112 
113 /**
114  * @brief Conversion to a (const) pointer.
115  *
116  * We just need to do a cast here.
117  */
118 template <class DVL>
119 inline
120 typename DVL::value_type const ElementProxy<DVL>::operator-> () const
121 {
122  return DataModel_detail::DVLCast<DVL>::cast (*m_proxied);
123 }
124 
125 
126 /**
127  * @brief Return the container holding the element that this object proxies.
128  */
129 template <class DVL>
130 inline
131 DVL* ElementProxy<DVL>::container()
132 {
133  return m_container;
134 }
135 
136 
137 /**
138  * @brief Return the container holding the element that this object proxies.
139  */
140 template <class DVL>
141 inline
142 const DVL* ElementProxy<DVL>::container() const
143 {
144  return m_container;
145 }
146 
147 
148 } // namespace DataModel_detail