From f475006726303e731389581a1493207e32498326 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de> Date: Mon, 16 May 2022 18:47:46 +0200 Subject: [PATCH 1/2] Powerfield: use OwningVector --- Device/Data/Powerfield.h | 6 +++--- Tests/Functional/Core/Consistence/CompareTwoReferences.cpp | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Device/Data/Powerfield.h b/Device/Data/Powerfield.h index 113b4eccb30..41b897a9412 100644 --- a/Device/Data/Powerfield.h +++ b/Device/Data/Powerfield.h @@ -18,7 +18,7 @@ #include "Base/Axis/Bin.h" #include "Base/Axis/FixedBinAxis.h" #include "Base/Py/PyObject.h" -#include "Base/Types/CloneableVector.h" +#include "Base/Types/OwningVector.h" #include "Base/Util/Assert.h" #include "Base/Util/ThreadInfo.h" #include "Device/Data/LLData.h" @@ -236,7 +236,7 @@ private: //! Checks if given axis name exists bool axisNameExists(const std::string& axis_name) const; - CloneableVector<IAxis> m_axes; + OwningVector<IAxis> m_axes; LLData<T>* m_ll_data; }; @@ -316,7 +316,7 @@ void Powerfield<T>::addAxis(const IAxis& new_axis) { ASSERT(!axisNameExists(new_axis.axisName())); if (new_axis.size() > 0) { - m_axes.push_back(new_axis.clone()); + m_axes.emplace_back(new_axis.clone()); allocate(); } } diff --git a/Tests/Functional/Core/Consistence/CompareTwoReferences.cpp b/Tests/Functional/Core/Consistence/CompareTwoReferences.cpp index 110235d71a3..0dd56fae581 100644 --- a/Tests/Functional/Core/Consistence/CompareTwoReferences.cpp +++ b/Tests/Functional/Core/Consistence/CompareTwoReferences.cpp @@ -17,6 +17,7 @@ #include "Device/Histo/HistoUtils.h" #include "Device/Histo/IOFactory.h" #include <iostream> +#include <memory> namespace { -- GitLab From 5a07fc0439325fa6837c7219ecb86fd96c5dbf78 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de> Date: Mon, 16 May 2022 19:19:44 +0200 Subject: [PATCH 2/2] repair OwningVector; rm CloneableVector --- Base/Types/CloneableVector.h | 65 ------------------------------------ Base/Types/OwningVector.h | 40 +++++++++++----------- auto/Wrap/doxygenBase.i | 31 ----------------- 3 files changed, 20 insertions(+), 116 deletions(-) delete mode 100644 Base/Types/CloneableVector.h diff --git a/Base/Types/CloneableVector.h b/Base/Types/CloneableVector.h deleted file mode 100644 index 3d112db5c41..00000000000 --- a/Base/Types/CloneableVector.h +++ /dev/null @@ -1,65 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file Base/Types/CloneableVector.h -//! @brief Defines and implements templated class CloneableVector. -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#ifdef SWIG -#error no need to expose this header to Swig -#endif - -#ifndef USER_API -#ifndef BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H -#define BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H - -#include <memory> -#include <vector> - -//! A vector of unique pointers to objects that are cloneable. -//! -//! Equips vector<unique_ptr<T>> with copy constructor. -//! For use with polymorphic objects, or in pimpl idiom. - -//! The objects pointed to must posses a clone() function. - -template <class T> -class CloneableVector : public std::vector<std::unique_ptr<T>> { - using super = std::vector<std::unique_ptr<T>>; - -public: - CloneableVector() = default; - CloneableVector(const CloneableVector& other) - : super() - { - copyOther(other); - } - CloneableVector& operator=(const CloneableVector& other) - { - if (this != &other) { - super::clear(); - copyOther(other); - } - return *this; - } - void push_back(T* t) { super::emplace_back(std::unique_ptr<T>(t)); } - void emplace_back(std::unique_ptr<T>&& t) { super::emplace_back(t); } - -private: - void copyOther(const CloneableVector& other) - { - super::reserve(other.size()); - for (const std::unique_ptr<T>& t : other) - super::emplace_back(t->clone()); - } -}; - -#endif // BORNAGAIN_BASE_TYPES_CLONEABLEVECTOR_H -#endif // USER_API diff --git a/Base/Types/OwningVector.h b/Base/Types/OwningVector.h index 6edc5460296..a13fdd556c1 100644 --- a/Base/Types/OwningVector.h +++ b/Base/Types/OwningVector.h @@ -32,49 +32,49 @@ //! The objects pointed to must posses a clone() function. template <class T> -class OwningVector : private std::vector<T*> { - using super = std::vector<T*>; - +class OwningVector { public: OwningVector() = default; ~OwningVector() { clear(); } OwningVector(const OwningVector& other) - : super() { - super::reserve(other.size()); + m_v.reserve(other.size()); for (T* e : other) - super::emplace_back(e->clone()); + m_v.emplace_back(e->clone()); } OwningVector& operator=(const OwningVector& other) { if (this == &other) return *this; OwningVector ret(other); - std::swap(*this, ret); + std::swap(m_v, ret.m_v); return *this; } - void emplace_back(T* e) { super::emplace_back(e); } + void emplace_back(T* e) { m_v.emplace_back(e); } void clear() { for (T* e : *this) delete e; - super::clear(); + m_v.clear(); } - size_t size() const { return super::size(); } - bool empty() const { return super::empty(); } - T* const& operator[](int i) const { return super::operator[](i); } - T* const& at(int i) const { return super::at(i); } - const T* back() const { return super::back(); } + size_t size() const { return m_v.size(); } + bool empty() const { return m_v.empty(); } + T* const& operator[](int i) const { return m_v.operator[](i); } + T* const& at(int i) const { return m_v.at(i); } + const T* back() const { return m_v.back(); } + + using Iterator = typename std::vector<T*>::iterator; // "typename" can be dropped under C++20 + using ConstIterator = typename std::vector<T*>::const_iterator; - using Iterator = typename super::iterator; // "typename" can be dropped under C++20 - using ConstIterator = typename super::const_iterator; + ConstIterator begin() const { return m_v.begin(); } + ConstIterator end() const { return m_v.end(); } + Iterator begin() { return m_v.begin(); } + Iterator end() { return m_v.end(); } - ConstIterator begin() const { return super::begin(); } - ConstIterator end() const { return super::end(); } - Iterator begin() { return super::begin(); } - Iterator end() { return super::end(); } +private: + std::vector<T*> m_v; }; #endif // BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H diff --git a/auto/Wrap/doxygenBase.i b/auto/Wrap/doxygenBase.i index e4ebea2b17e..11697d8f5e7 100644 --- a/auto/Wrap/doxygenBase.i +++ b/auto/Wrap/doxygenBase.i @@ -26,31 +26,6 @@ C++ includes: IntegratorMCMiser.h "; -// File: classCloneableVector.xml -%feature("docstring") CloneableVector " - -The objects pointed to must posses a clone() function. - -A vector of unique pointers to objects that are cloneable. - -Equips vector<unique_ptr<T>> with copy constructor. For use with polymorphic objects, or in pimpl idiom. - -C++ includes: CloneableVector.h -"; - -%feature("docstring") CloneableVector::CloneableVector "CloneableVector< T >::CloneableVector()=default -"; - -%feature("docstring") CloneableVector::CloneableVector "CloneableVector< T >::CloneableVector(const CloneableVector &other) -"; - -%feature("docstring") CloneableVector::push_back "void CloneableVector< T >::push_back(T *t) -"; - -%feature("docstring") CloneableVector::emplace_back "void CloneableVector< T >::emplace_back(std::unique_ptr< T > &&t) -"; - - // File: classComplexIntegrator.xml %feature("docstring") ComplexIntegrator " @@ -1455,9 +1430,6 @@ Returns a string of blanks with given width. By default the width equals standar "; -// File: namespacestd.xml - - // File: namespaceUnits.xml %feature("docstring") Units::rad2deg "double Units::rad2deg(double angle) "; @@ -1627,9 +1599,6 @@ Template function to create an integrator object. "; -// File: CloneableVector_8h.xml - - // File: ICloneable_8h.xml -- GitLab