BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
CloneableVector.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Types/CloneableVector.h
6 //! @brief Defines and implements templated class CloneableVector.
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_CLONEABLEVECTOR_H
16 #define BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H
17 
18 #include <memory>
19 #include <vector>
20 
21 //! A vector of unique pointers to objects that are cloneable.
22 //!
23 //! Equips vector<unique_ptr<T>> with copy constructor.
24 //! For use with polymorphic objects, or in pimpl idiom.
25 //!
26 //! @ingroup tools_internal
27 
28 //! The objects pointed to must posses a clone() function.
29 
30 template <class T> class CloneableVector : public std::vector<std::unique_ptr<T>>
31 {
32  using super = std::vector<std::unique_ptr<T>>;
33 
34 public:
37  {
38  super::reserve(other.size());
39  for (const std::unique_ptr<T>& t : other)
40  super::emplace_back(t->clone());
41  }
42  void push_back(T* t) { super::emplace_back(std::unique_ptr<T>(t)); }
43  void emplace_back(std::unique_ptr<T>&& t) { super::emplace_back(t); }
44 };
45 
46 #endif // BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H
A vector of unique pointers to objects that are cloneable.
void push_back(T *t)
CloneableVector(const CloneableVector &other)
void emplace_back(std::unique_ptr< T > &&t)
std::vector< std::unique_ptr< T > > super