1 #ifndef PandaTree_Framework_Ref_h 2 #define PandaTree_Framework_Ref_h 4 #include "ContainerBase.h" 24 typedef Short_t index_type;
35 Ref(self_type
const& orig) : container_(orig.container_), idx_(orig.idx_) {}
50 if (!container_ || !(*container_) || !idx_ || (*idx_) < 0)
53 return &
static_cast<E const&
>((*container_)->elemAt(*idx_));
61 if (!container_ || !(*container_) || !idx_ || (*idx_) < 0)
62 throw std::runtime_error(
"Dereferencing an invalid Ref");
64 return static_cast<E const&
>((*container_)->elemAt(*idx_));
73 container_ = rhs.container_;
76 (*idx_) = (*rhs.idx_);
95 if (!container_ || !(*container_) || !idx_)
99 for ((*idx_) = 0; (*idx_) != Short_t((*container_)->size()); ++(*idx_)) {
100 if (&(*container_)->elemAt(*idx_) == rhs)
110 Bool_t
isValid()
const {
return container_ && *container_ && idx_ && unsigned(*idx_) < (*container_)->size(); }
115 void init() {
if (idx_) (*idx_) = -1; }
123 throw std::runtime_error(
"Invalid index ref");
134 throw std::runtime_error(
"Invalid index ref");
145 throw std::runtime_error(
"Invalid container ref");
167 typedef E value_type;
168 typedef Short_t index_type;
179 ConstRef(self_type
const& orig) : container_(orig.container_), idx_(orig.idx_) {}
181 self_type&
operator=(self_type
const&) =
delete;
197 if (!container_ || !(*container_) || !idx_ || (*idx_) < 0)
200 return &
static_cast<E const&
>((*container_)->elemAt(*idx_));
208 if (!container_ || !(*container_) || !idx_ || (*idx_) < 0)
209 throw std::runtime_error(
"Dereferencing an invalid ConstRef");
211 return static_cast<E const&
>((*container_)->elemAt(*idx_));
214 Bool_t
isValid()
const {
return container_ && *container_ && idx_ && unsigned(*idx_) < (*container_)->size(); }
219 index_type
const&
idx()
const 222 throw std::runtime_error(
"Invalid index ref");
233 throw std::runtime_error(
"Invalid container ref");
242 index_type
const* idx_{0};
248 std::ostream& operator<<(std::ostream& _out, panda::Ref<E>
const& _ref)
250 _out <<
"Ref<" << E::typeName() <<
">";
252 _out <<
" " << _ref.container()->
getName() <<
"(" << _ref.idx() <<
")";
260 std::ostream& operator<<(std::ostream& _out, panda::ConstRef<E>
const& _ref)
262 _out <<
"Ref<" << E::typeName() <<
">";
264 _out <<
" " << _ref.container()->getName() <<
"(" << _ref.idx() <<
")";
ConstRef()
Default constructor.
Definition: Ref.h:171
A reference to an element in a container.
Definition: Ref.h:20
self_type & operator=(self_type const &rhs)
Copy assignment.
Definition: Ref.h:71
index_type const & idx() const
Accessor to idx.
Definition: Ref.h:219
value_type const & operator*() const
The dereference operator.
Definition: Ref.h:206
void setIndex(index_type &idx)
Set the index.
Definition: Ref.h:37
ContainerBase const * container() const
Accessor to container.
Definition: Ref.h:142
Ref(self_type const &orig)
Copy constructor.
Definition: Ref.h:35
Ref()
Default constructor.
Definition: Ref.h:27
ConstRef(ContainerBase const *&c, index_type const &idx)
Standard constructor.
Definition: Ref.h:177
value_type const * operator->() const
The arrow operator.
Definition: Ref.h:48
Bool_t isValid() const
Validity check. Both container and idx must be valid, and idx must not be 0xffffffff.
Definition: Ref.h:110
index_type & idx()
Accessor to idx.
Definition: Ref.h:120
index_type idx() const
Accessor to idx.
Definition: Ref.h:131
void setIndex(index_type const &idx)
Set the index.
Definition: Ref.h:184
Bool_t setRef(value_type const *rhs)
Setter function.
Definition: Ref.h:88
ConstRef(self_type const &orig)
Copy constructor.
Definition: Ref.h:179
void setContainer(ContainerBase const *&c)
Set the container.
Definition: Ref.h:190
char const * getName() const final
Name of this object.
Definition: ContainerBase.h:25
Base class for all containers.
Definition: ContainerBase.h:18
void init()
Initializer.
Definition: Ref.h:115
void setContainer(ContainerBase const *&c)
Set the container.
Definition: Ref.h:43
value_type const * operator->() const
The arrow operator.
Definition: Ref.h:195
Bool_t isValid() const
Validity check. Both container and idx must be valid, and idx must not be 0xffffffff.
Definition: Ref.h:214
A const version of Ref.
Definition: Ref.h:164
ContainerBase const * container() const
Accessor to container.
Definition: Ref.h:230
value_type const & operator*() const
The dereference operator.
Definition: Ref.h:59
Ref(ContainerBase const *&c, index_type &idx)
Standard constructor.
Definition: Ref.h:33