BornAgain  1.19.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 reflection and scattering
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H
21 #define BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H
22 
23 #include <memory>
24 #include <vector>
25 
26 //! A vector of unique pointers to objects that are cloneable.
27 //!
28 //! Equips vector<unique_ptr<T>> with copy constructor.
29 //! For use with polymorphic objects, or in pimpl idiom.
30 //!
31 //! @ingroup tools_internal
32 
33 //! The objects pointed to must posses a clone() function.
34 
35 template <class T> class CloneableVector : public std::vector<std::unique_ptr<T>> {
36  using super = std::vector<std::unique_ptr<T>>;
37 
38 public:
41  {
42  super::reserve(other.size());
43  for (const std::unique_ptr<T>& t : other)
44  super::emplace_back(t->clone());
45  }
46  void push_back(T* t) { super::emplace_back(std::unique_ptr<T>(t)); }
47  void emplace_back(std::unique_ptr<T>&& t) { super::emplace_back(t); }
48 };
49 
50 #endif // BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H
51 #endif // USER_API
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