CORSIKA  @c8_version@
The framework to simulate particle cascades for astroparticle physics
StackIteratorInterface.hpp
1 /*
2  * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
3  *
4  * This software is distributed under the terms of the GNU General Public
5  * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
6  * the license.
7  */
8 
9 #pragma once
10 
11 #include <corsika/framework/stack/ParticleBase.hpp>
12 
13 #include <boost/type_index.hpp>
14 
15 namespace corsika::history {
16  template <typename T, template <typename> typename TParticleInterface>
17  class HistorySecondaryProducer; // forward decl.
18 }
19 
20 namespace corsika {
21 
22  template <typename TStackData, template <typename> typename TParticleInterface,
23  template <class T1, template <class> class T2> class MSecondaryProducer>
24  class Stack; // forward decl
25 
26  template <typename TStackData, template <typename> typename TParticleInterface,
27  template <class T1, template <class> class T2> class MSecondaryProducer>
28  class SecondaryView; // forward decl
29 
30  template <typename TStackData, template <typename> typename TParticleInterface,
31  template <class T1, template <class> class T2> class MSecondaryProducer,
32  typename StackType>
33  class ConstStackIteratorInterface; // forward decl
34 
75  template <typename TStackData, template <typename> typename TParticleInterface,
76  template <typename T1, template <class> class T2> class MSecondaryProducer,
77  typename TStackType =
80  : public TParticleInterface<StackIteratorInterface<
81  TStackData, TParticleInterface, MSecondaryProducer, TStackType>> {
82 
83  public:
84  typedef TParticleInterface<corsika::StackIteratorInterface<
85  TStackData, TParticleInterface, MSecondaryProducer, TStackType>>
86  particle_interface_type;
87 
88  typedef TStackType stack_type;
89  typedef TStackData stack_data_type;
90 
91  // it is not allowed to create a "dangling" stack iterator
92  StackIteratorInterface() = delete;
93 
95  : index_(std::move(rhs.index_))
96  , data_(std::move(rhs.data_)) {}
97 
99  : index_(vR.index_)
100  , data_(vR.data_) {}
101 
102  StackIteratorInterface& operator=(StackIteratorInterface const& vR) {
103  if (&vR != this) {
104  index_ = vR.index_;
105  data_ = vR.data_;
106  }
107  return *this;
108  }
109 
116  StackIteratorInterface(stack_type& data, unsigned int const index)
117  : index_(index)
118  , data_(&data) {}
119 
129  template <typename... TArgs>
130  StackIteratorInterface(stack_type& data, unsigned int const index,
131  const TArgs... args)
132  : index_(index)
133  , data_(&data) {
134 
135  (**this).setParticleData(args...);
136  }
137 
150  template <typename... TArgs>
151  StackIteratorInterface(stack_type& data, unsigned int const index,
152  StackIteratorInterface& parent, const TArgs... args)
153  : index_(index)
154  , data_(&data) {
155 
156  (**this).setParticleData(*parent, args...);
157  }
158 
159  bool isErased() const { return getStack().isErased(*this); }
160 
161  public:
166  StackIteratorInterface& operator++() {
167  do {
168  ++index_;
169  } while (
170  getStack().isErased(*this)); // this also check the allowed bounds of index_
171  return *this;
172  }
173  StackIteratorInterface operator++(int) {
174  StackIteratorInterface tmp(*this);
175  do {
176  ++index_;
177  } while (
178  getStack().isErased(*this)); // this also check the allowed bounds of index_
179  return tmp;
180  }
181  StackIteratorInterface operator+(int delta) const {
182  return StackIteratorInterface(*data_, index_ + delta);
183  }
184  bool operator==(StackIteratorInterface const& rhs) const {
185  return index_ == rhs.index_;
186  }
187  bool operator!=(StackIteratorInterface const& rhs) const {
188  return index_ != rhs.index_;
189  }
190  bool operator==(const ConstStackIteratorInterface<
191  TStackData, TParticleInterface, MSecondaryProducer,
192  stack_type>& rhs) const; // implemented below
194  TStackData, TParticleInterface, MSecondaryProducer,
195  stack_type>& rhs) const; // implemented below
196 
201  particle_interface_type& operator*() {
202  return static_cast<particle_interface_type&>(*this);
203  }
204 
209  particle_interface_type const& operator*() const {
210  return static_cast<particle_interface_type const&>(*this);
211  }
213 
214  protected:
219  unsigned int getIndex() const { return index_; }
222  stack_type& getStack() { return *data_; }
224  stack_type const& getStack() const { return *data_; }
226  TStackData& getStackData() { return data_->getStackData(); }
228  TStackData const& getStackData() const { return data_->getStackData(); }
230  unsigned int getIndexFromIterator() const {
231  return data_->getIndexFromIterator(index_);
232  }
234 
235  // friends are needed for access to protected methods
236  friend class Stack<TStackData, TParticleInterface,
237  MSecondaryProducer>; // for access to getIndex for Stack
238  friend class Stack<TStackData&, TParticleInterface,
239  MSecondaryProducer>; // for access to getIndex
240  // SecondaryView : public Stack
241  friend class ParticleBase<StackIteratorInterface>; // for access to getStackDataType
242 
243  template <typename T1, // best fix this to: TStackData,
244  template <typename> typename M1, // best fix this to: TParticleInterface,
245  template <typename T, template <typename> typename T3> typename M2>
246  friend class SecondaryView; // access grant for SecondaryView
247 
248  template <typename T, template <typename> typename TParticleInterface_>
250 
251  friend class ConstStackIteratorInterface<TStackData, TParticleInterface,
252  MSecondaryProducer, stack_type>;
253 
254  protected:
255  unsigned int index_ = 0;
256 
257  private:
258  stack_type* data_ = 0; // info: Particles and StackIterators become invalid when
259  // parent Stack is copied or deleted!
260 
261  }; // end class StackIterator
262 
277  template <typename TStackData, template <typename> typename TParticleInterface,
278  template <typename T1, template <class> class T2> class MSecondaryProducer,
279  typename TStackType =
282  : public TParticleInterface<ConstStackIteratorInterface<
283  TStackData, TParticleInterface, MSecondaryProducer, TStackType>> {
284 
285  public:
286  typedef TParticleInterface<ConstStackIteratorInterface<
287  TStackData, TParticleInterface, MSecondaryProducer, TStackType>>
288  particle_interface_type;
289 
290  typedef TStackType stack_type;
291  typedef TStackData stack_data_type;
292 
293  // we don't want to allow dangling iterators to exist
294  ConstStackIteratorInterface() = delete;
295 
296  public:
298  : index_(std::move(rhs.index_))
299  , data_(std::move(rhs.data_)) {}
300 
301  ConstStackIteratorInterface(stack_type const& data, unsigned int const index)
302  : index_(index)
303  , data_(&data) {}
304 
305  bool isErased() const { return getStack().isErased(*this); }
306 
311  ConstStackIteratorInterface& operator++() {
312  do {
313  ++index_;
314  } while (
315  getStack().isErased(*this)); // this also check the allowed bounds of index_
316  return *this;
317  }
318  ConstStackIteratorInterface operator++(int) {
319  ConstStackIteratorInterface tmp(*this);
320  do {
321  ++index_;
322  } while (
323  getStack().isErased(*this)); // this also check the allowed bounds of index_
324  return tmp;
325  }
326  ConstStackIteratorInterface operator+(int const delta) const {
327  return ConstStackIteratorInterface(*data_, index_ + delta);
328  }
329  bool operator==(ConstStackIteratorInterface const& rhs) const {
330  return index_ == rhs.index_;
331  }
332  bool operator!=(ConstStackIteratorInterface const& rhs) const {
333  return index_ != rhs.index_;
334  }
335  bool operator==(
336  StackIteratorInterface<stack_data_type, TParticleInterface, MSecondaryProducer,
337  stack_type> const& rhs) const {
338  return index_ == rhs.index_;
339  }
340  bool operator!=(
341  StackIteratorInterface<stack_data_type, TParticleInterface, MSecondaryProducer,
342  stack_type> const& rhs) const {
343  return index_ != rhs.index_;
344  }
345 
346  particle_interface_type const& operator*() const {
347  return static_cast<particle_interface_type const&>(*this);
348  }
350 
351  protected:
357  unsigned int getIndex() const { return index_; }
358  stack_type const& getStack() const { return *data_; }
359  stack_data_type const& getStackData() const { return data_->getStackData(); }
361  unsigned int getIndexFromIterator() const {
362  return data_->getIndexFromIterator(index_);
363  }
365 
366  // friends are needed for access to protected methods
367  friend class Stack<stack_data_type, TParticleInterface,
368  MSecondaryProducer>; // for access to GetIndex for Stack
369  friend class Stack<stack_data_type&, TParticleInterface,
370  MSecondaryProducer>; // for access to GetIndex
371 
372  friend class ParticleBase<ConstStackIteratorInterface>; // for access to GetStackData
373 
374  template <typename T1, // best fix to: stack_data_type,
375  template <typename> typename M1, // best fix to: TParticleInterface,
376  template <class T2, template <class> class T3> class M2>
377  friend class SecondaryView; // access for SecondaryView
378 
379  friend class StackIteratorInterface<stack_data_type, TParticleInterface,
380  MSecondaryProducer, stack_type>;
381 
382  template <typename T, template <typename> typename TParticleInterface_>
384 
385  protected:
386  unsigned int index_ = 0;
387 
388  private:
389  stack_type const* data_ = 0; // info: Particles and StackIterators become invalid when
390  // parent Stack is copied or deleted!
391 
392  }; // end class ConstStackIterator
393 
394 } // namespace corsika
TStackData & getStackData()
Get current user particle TStackData object.
particle_interface_type const & operator*() const
Convert iterator to const value type, where value type is the user-provided particle readout class...
The Stack class provides (and connects) the main particle data storage machinery. ...
Definition: Stack.hpp:77
StackIteratorInterface(stack_type &data, unsigned int const index, const TArgs... args)
Constructor that also sets new values on particle data object.
STL namespace.
particle_interface_type & operator*()
Convert iterator to value type, where value type is the user-provided particle readout class...
constexpr quantity< D, X > operator+(quantity< D, X > const &x)
Definition: quantity.hpp:528
stack_type const & getStack() const
Get current particle const Stack object.
TStackData const & getStackData() const
Get current const user particle TStackData object.
Vector< phys::units::detail::product_d< TDimension, UDimension > > operator*(quantity< UDimension > const n, Vector< TDimension > const &vec)
Free operator to allow commutative multiplications of quantities and Vector.
`, since they are used everywhere as integral part of the framework.
constexpr bool operator!=(quantity< D, X > const &x, quantity< D, Y > const &y)
inequality.
Definition: quantity.hpp:707
StackIteratorInterface(StackIteratorInterface &&rhs)
unsigned int getIndexFromIterator() const
Get data index as mapped in Stack class.
StackIteratorInterface(stack_type &data, unsigned int const index)
Iterator must always point to data, with an index.
SecondaryView can only be constructed by giving a valid Projectile particle, following calls to addSe...
ConstStackIteratorInterface(ConstStackIteratorInterface &&rhs)
unsigned int getIndexFromIterator() const
Get data index as mapped in Stack class.
stack_type & getStack()
Get current particle Stack object.
The base class to define the readout of particle properties from a particle stack.
This is the iterator class for const-access to stack data.
StackIteratorInterface(stack_type &data, unsigned int const index, StackIteratorInterface &parent, const TArgs... args)
Constructor that also sets new values on particle data object, including reference to parent particle...
The StackIteratorInterface is the main interface to iterator over particles on a stack.