BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
OwningVector.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Types/OwningVector.h
6 //! @brief Defines and implements templated class OwningVector.
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_OWNINGVECTOR_H
21 #define BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H
22 
23 #include <cstddef>
24 #include <utility>
25 #include <vector>
26 
27 //! A vector of unique pointers to objects that are cloneable.
28 //!
29 //! Equips vector<unique_ptr<T>> with copy constructor.
30 //! For use with polymorphic objects, or in pimpl idiom.
31 
32 //! The objects pointed to must posses a clone() function.
33 
34 template <class T>
35 class OwningVector {
36 public:
37  OwningVector() = default;
38  //! Constructor that takes over ownership of elements in given vector
39  OwningVector(const std::vector<T*>& v)
40  {
41  m_v.reserve(v.size());
42  for (T* e : v)
43  m_v.emplace_back(e);
44  }
45  //! Constructor that clones elements in given vector
46  OwningVector(const OwningVector& other)
47  {
48  m_v.reserve(other.size());
49  for (T* e : other)
50  m_v.emplace_back(e->clone());
51  }
54  {
55  if (this == &other)
56  return *this;
57  OwningVector ret(other);
58  std::swap(m_v, ret.m_v);
59  return *this;
60  }
61 
62  void emplace_back(T* e) { m_v.emplace_back(e); }
63  void clear()
64  {
65  for (T* e : *this)
66  delete e;
67  m_v.clear();
68  }
69 
70  size_t size() const { return m_v.size(); }
71  bool empty() const { return m_v.empty(); }
72  T* const& operator[](int i) const { return m_v.operator[](i); }
73  T* const& at(int i) const { return m_v.at(i); }
74  const T* back() const { return m_v.back(); }
75 
76  std::vector<const T*> const_vector() const
77  {
78  const std::vector<const T*> ret(m_v.begin(), m_v.end());
79  return ret;
80  }
81  std::vector<T*> cloned_vector() const
82  {
83  std::vector<T*> ret;
84  for (T* e : m_v)
85  ret.emplace_back(e->clone());
86  return ret;
87  }
88 
89  using Iterator = typename std::vector<T*>::iterator; // "typename" can be dropped under C++20
90  using ConstIterator = typename std::vector<T*>::const_iterator;
91 
92  ConstIterator begin() const { return m_v.begin(); }
93  ConstIterator end() const { return m_v.end(); }
94  Iterator begin() { return m_v.begin(); }
95  Iterator end() { return m_v.end(); }
96 
97 private:
98  std::vector<T*> m_v;
99 };
100 
101 #endif // BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H
102 #endif // USER_API
A vector of unique pointers to objects that are cloneable.
Definition: OwningVector.h:35
OwningVector()=default
Iterator end()
Definition: OwningVector.h:95
OwningVector(const std::vector< T * > &v)
Constructor that takes over ownership of elements in given vector.
Definition: OwningVector.h:39
Iterator begin()
Definition: OwningVector.h:94
T *const & operator[](int i) const
Definition: OwningVector.h:72
OwningVector(const OwningVector &other)
Constructor that clones elements in given vector.
Definition: OwningVector.h:46
void emplace_back(T *e)
Definition: OwningVector.h:62
typename std::vector< T * >::const_iterator ConstIterator
Definition: OwningVector.h:90
ConstIterator begin() const
Definition: OwningVector.h:92
void clear()
Definition: OwningVector.h:63
std::vector< T * > cloned_vector() const
Definition: OwningVector.h:81
OwningVector & operator=(const OwningVector &other)
Definition: OwningVector.h:53
ConstIterator end() const
Definition: OwningVector.h:93
size_t size() const
Definition: OwningVector.h:70
const T * back() const
Definition: OwningVector.h:74
bool empty() const
Definition: OwningVector.h:71
std::vector< const T * > const_vector() const
Definition: OwningVector.h:76
std::vector< T * > m_v
Definition: OwningVector.h:98
T *const & at(int i) const
Definition: OwningVector.h:73
typename std::vector< T * >::iterator Iterator
Definition: OwningVector.h:89