BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SafePointerVector.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Types/SafePointerVector.h
6 //! @brief Defines and implements template class SafePointerVector.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
21 #define BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
22 
23 #include <algorithm>
24 #include <vector>
25 
26 //! A vector of pointers, owned by *this, with methods to handle them safely.
27 //! @ingroup tools_internal
28 
29 //! The objects pointed to must support the ICloneable interface.
30 
31 template <class T> class SafePointerVector {
32 public:
33  typedef typename std::vector<T*>::iterator iterator;
34  typedef typename std::vector<T*>::const_iterator const_iterator;
39 
42  size_t size() const { return m_pointers.size(); }
43  bool empty() const { return m_pointers.empty(); }
44  void push_back(T* pointer) { m_pointers.push_back(pointer); }
45  T* operator[](size_t index) { return m_pointers[index]; }
46  const T* operator[](size_t index) const { return m_pointers[index]; }
47  iterator begin() { return m_pointers.begin(); }
48  const_iterator begin() const { return m_pointers.begin(); }
49  iterator end() { return m_pointers.end(); }
50  const_iterator end() const { return m_pointers.end(); }
51 
52  T* back() { return m_pointers.back(); }
53  const T* back() const { return m_pointers.back(); }
54  void clear();
55 
56 private:
57  std::vector<T*> m_pointers;
58 };
59 
61 {
62  for (const_iterator it = other.begin(); it != other.end(); ++it)
63  m_pointers.push_back((*it)->clone());
64 }
65 
66 template <class T>
68  : m_pointers(std::move(other.m_pointers))
69 {
70 }
71 
72 template <class T>
74 {
75  if (this == &right)
76  return *this;
77  clear();
78  for (const_iterator it = right.begin(); it != right.end(); ++it)
79  m_pointers.push_back((*it)->clone());
80  return *this;
81 }
82 
83 template <class T>
85 {
86  clear();
87  m_pointers = std::move(right.m_pointers);
88  right.m_pointers.clear();
89  return *this;
90 }
91 
92 template <class T> void SafePointerVector<T>::clear()
93 {
94  for (iterator it = begin(); it != end(); ++it)
95  delete (*it);
96  m_pointers.clear();
97 }
98 
99 #endif // BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
100 #endif // USER_API
A vector of pointers, owned by *this, with methods to handle them safely.
const T * operator[](size_t index) const
const_iterator begin() const
SafePointerVector(const SafePointerVector &other)
SafePointerVector & operator=(const SafePointerVector &right)
const_iterator end() const
std::vector< T * >::const_iterator const_iterator
size_t size() const
void push_back(T *pointer)
std::vector< T * > m_pointers
T * operator[](size_t index)
SafePointerVector(SafePointerVector &&other)
std::vector< T * >::iterator iterator
const T * back() const
SafePointerVector & operator=(SafePointerVector &&right)
Definition: filesystem.h:81