BornAgain  1.18.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 scattering at grazing incidence
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 #ifndef BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
16 #define BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
17 
18 #include <algorithm>
19 #include <vector>
20 
21 //! A vector of pointers, owned by *this, with methods to handle them safely.
22 //! @ingroup tools_internal
23 
24 //! The objects pointed to must support the ICloneable interface.
25 
26 template <class T> class SafePointerVector
27 {
28 public:
29  typedef typename std::vector<T*>::iterator iterator;
30  typedef typename std::vector<T*>::const_iterator const_iterator;
35 
38  size_t size() const { return m_pointers.size(); }
39  bool empty() const { return m_pointers.empty(); }
40  void push_back(T* pointer) { m_pointers.push_back(pointer); }
41  T* operator[](size_t index) { return m_pointers[index]; }
42  const T* operator[](size_t index) const { return m_pointers[index]; }
43  iterator begin() { return m_pointers.begin(); }
44  const_iterator begin() const { return m_pointers.begin(); }
45  iterator end() { return m_pointers.end(); }
46  const_iterator end() const { return m_pointers.end(); }
47 
48  bool deleteElement(T* pointer);
49 
50  T* back() { return m_pointers.back(); }
51  const T* back() const { return m_pointers.back(); }
52  void clear();
53 
54 private:
55  std::vector<T*> m_pointers;
56 };
57 
59 {
60  for (const_iterator it = other.begin(); it != other.end(); ++it)
61  m_pointers.push_back((*it)->clone());
62 }
63 
64 template <class T>
65 SafePointerVector<T>::SafePointerVector(SafePointerVector<T>&& other) : m_pointers{other.m_pointers}
66 {
67 }
68 
69 template <class T>
71 {
72  if (this == &right)
73  return *this;
74  clear();
75  for (const_iterator it = right.begin(); it != right.end(); ++it)
76  m_pointers.push_back((*it)->clone());
77  return *this;
78 }
79 
80 template <class T>
82 {
83  clear();
84  m_pointers = std::move(right.m_pointers);
85  right.m_pointers.clear();
86  return *this;
87 }
88 
89 template <class T> inline bool SafePointerVector<T>::deleteElement(T* pointer)
90 {
91  iterator it = std::find(m_pointers.begin(), m_pointers.end(), pointer);
92  if (it == m_pointers.end())
93  return false;
94  m_pointers.erase(it);
95  delete pointer;
96  return true;
97 }
98 
99 template <class T> void SafePointerVector<T>::clear()
100 {
101  for (iterator it = begin(); it != end(); ++it)
102  delete (*it);
103  m_pointers.clear();
104 }
105 
106 #endif // BORNAGAIN_BASE_TYPES_SAFEPOINTERVECTOR_H
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)
bool deleteElement(T *pointer)
std::vector< T * >::iterator iterator
const T * back() const
SafePointerVector & operator=(SafePointerVector &&right)