Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few move constructors and assignments #1002

Merged
merged 4 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
brew install boost
- name: Compile
env:
CXXFLAGS: -O2 -stdlib=libc++ -mmacosx-version-min=10.9
CXXFLAGS: -O2 -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.9
run: |
cmake .
make -j 2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos-nondefault.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
include:
- os: [macos-10.15]
shortname: default
cxxflags: "-Wno-c++11-extensions"
cxxflags: "-std=c++11"
- os: [macos-10.15]
shortname: stdclasses
cxxflags: "-std=c++17"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
include:
- os: [macos-10.15]
shortname: default
cxxflags: "-Wno-c++11-extensions"
cxxflags: "-std=c++11"
- os: [macos-10.15]
shortname: stdclasses
cxxflags: "-std=c++11"
Expand Down
3 changes: 3 additions & 0 deletions ql/config.msvc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

// conditionally work around compiler glitches
#define QL_PATCH_MSVC
#if (_MSC_VER < 1900)
# define QL_PATCH_MSVC_2013
#endif

// prevent auto-link of Boost libs such as serialization
#ifndef BOOST_ALL_NO_LIB
Expand Down
28 changes: 21 additions & 7 deletions ql/math/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ namespace QuantLib {
*/
Array(Size size, Real value, Real increment);
Array(const Array&);
Array(Array&&) QL_NOEXCEPT;
Array(const Disposable<Array>&);
//! creates the array from an iterable sequence
template <class ForwardIterator>
Array(ForwardIterator begin, ForwardIterator end);

Array& operator=(const Array&);
Array& operator=(Array&&) QL_NOEXCEPT;
Array& operator=(const Disposable<Array>&);

bool operator==(const Array&) const;
bool operator!=(const Array&) const;
//@}
Expand Down Expand Up @@ -213,7 +216,8 @@ namespace QuantLib {

// inline definitions

inline Array::Array(Size size) : data_(size != 0U ? new Real[size] : (Real*)(0)), n_(size) {}
inline Array::Array(Size size)
: data_(size != 0U ? new Real[size] : (Real*)(0)), n_(size) {}

inline Array::Array(Size size, Real value)
: data_(size != 0U ? new Real[size] : (Real*)(0)), n_(size) {
Expand All @@ -228,12 +232,17 @@ namespace QuantLib {

inline Array::Array(const Array& from)
: data_(from.n_ != 0U ? new Real[from.n_] : (Real*)(0)), n_(from.n_) {
#if defined(QL_PATCH_MSVC) && defined(QL_DEBUG)
#if defined(QL_PATCH_MSVC) && defined(QL_DEBUG)
if (n_)
#endif
std::copy(from.begin(),from.end(),begin());
}

inline Array::Array(Array&& from) QL_NOEXCEPT
: data_((Real*)nullptr), n_(0) {
swap(from);
}

inline Array::Array(const Disposable<Array>& from)
: data_((Real*)(0)), n_(0) {
swap(const_cast<Disposable<Array>&>(from));
Expand Down Expand Up @@ -291,6 +300,16 @@ namespace QuantLib {
return *this;
}

inline Array& Array::operator=(Array&& from) QL_NOEXCEPT {
swap(from);
return *this;
}

inline Array& Array::operator=(const Disposable<Array>& from) {
swap(const_cast<Disposable<Array>&>(from));
return *this;
}

inline bool Array::operator==(const Array& to) const {
return (n_ == to.n_) && std::equal(begin(), end(), to.begin());
}
Expand All @@ -299,11 +318,6 @@ namespace QuantLib {
return !(this->operator==(to));
}

inline Array& Array::operator=(const Disposable<Array>& from) {
swap(const_cast<Disposable<Array>&>(from));
return *this;
}

inline const Array& Array::operator+=(const Array& v) {
QL_REQUIRE(n_ == v.n_,
"arrays with different sizes (" << n_ << ", "
Expand Down
14 changes: 13 additions & 1 deletion ql/math/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ namespace QuantLib {
*/
template <class Iterator>
Matrix(Size rows, Size columns, Iterator begin, Iterator end);
Matrix(const Matrix &);
Matrix(const Matrix&);
Matrix(Matrix&&) QL_NOEXCEPT;
Matrix(const Disposable<Matrix>&);
Matrix& operator=(const Matrix&);
Matrix& operator=(Matrix&&) QL_NOEXCEPT;
Matrix& operator=(const Disposable<Matrix>&);
//@}

Expand Down Expand Up @@ -221,6 +223,11 @@ namespace QuantLib {
std::copy(from.begin(),from.end(),begin());
}

inline Matrix::Matrix(Matrix&& from) QL_NOEXCEPT
: data_((Real*)nullptr) {
swap(from);
}

inline Matrix::Matrix(const Disposable<Matrix>& from)
: data_((Real*)(0)), rows_(0), columns_(0) {
swap(const_cast<Disposable<Matrix>&>(from));
Expand All @@ -233,6 +240,11 @@ namespace QuantLib {
return *this;
}

inline Matrix& Matrix::operator=(Matrix&& from) QL_NOEXCEPT {
swap(from);
return *this;
}

inline Matrix& Matrix::operator=(const Disposable<Matrix>& from) {
swap(const_cast<Disposable<Matrix>&>(from));
return *this;
Expand Down
13 changes: 3 additions & 10 deletions ql/methods/finitedifferences/operators/ninepointlinearop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ namespace QuantLib {
std::copy(m.a22_.get(), m.a22_.get()+size, a22_.get());
}

NinePointLinearOp& NinePointLinearOp::operator=(
const NinePointLinearOp& m) {
NinePointLinearOp temp(m);
swap(temp);
return *this;
NinePointLinearOp::NinePointLinearOp(
const Disposable<NinePointLinearOp>& from) {
swap(const_cast<Disposable<NinePointLinearOp>&>(from));
}

NinePointLinearOp& NinePointLinearOp::operator=(
Expand All @@ -123,11 +121,6 @@ namespace QuantLib {
return *this;
}

NinePointLinearOp::NinePointLinearOp(
const Disposable<NinePointLinearOp>& from) {
swap(const_cast<Disposable<NinePointLinearOp>&>(from));
}

Disposable<Array> NinePointLinearOp::apply(const Array& u)
const {

Expand Down
20 changes: 20 additions & 0 deletions ql/methods/finitedifferences/operators/ninepointlinearop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
#include <boost/shared_array.hpp>

namespace QuantLib {

class FdmMesher;

class NinePointLinearOp : public FdmLinearOp {
public:
NinePointLinearOp(Size d0, Size d1,
const ext::shared_ptr<FdmMesher>& mesher);
NinePointLinearOp(const NinePointLinearOp& m);
NinePointLinearOp(NinePointLinearOp&& m) QL_NOEXCEPT;
NinePointLinearOp(const Disposable<NinePointLinearOp>& m);
NinePointLinearOp& operator=(const NinePointLinearOp& m);
NinePointLinearOp& operator=(NinePointLinearOp&& m) QL_NOEXCEPT;
NinePointLinearOp& operator=(const Disposable<NinePointLinearOp>& m);

Disposable<Array> apply(const Array& r) const;
Expand All @@ -65,6 +68,23 @@ namespace QuantLib {

ext::shared_ptr<FdmMesher> mesher_;
};


inline NinePointLinearOp::NinePointLinearOp(NinePointLinearOp&& m) QL_NOEXCEPT {
swap(m);
}

inline NinePointLinearOp& NinePointLinearOp::operator=(const NinePointLinearOp& m) {
NinePointLinearOp temp(m);
swap(temp);
return *this;
}

inline NinePointLinearOp& NinePointLinearOp::operator=(NinePointLinearOp&& m) QL_NOEXCEPT {
swap(m);
return *this;
}

}

#endif
7 changes: 0 additions & 7 deletions ql/methods/finitedifferences/operators/triplebandlinearop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ namespace QuantLib {
swap(const_cast<Disposable<TripleBandLinearOp>&>(from));
}

TripleBandLinearOp& TripleBandLinearOp::operator=(
const TripleBandLinearOp& m) {
TripleBandLinearOp tmp(m);
swap(tmp);
return *this;
}

TripleBandLinearOp& TripleBandLinearOp::operator=(
const Disposable<TripleBandLinearOp>& m) {
swap(const_cast<Disposable<TripleBandLinearOp>&>(m));
Expand Down
19 changes: 19 additions & 0 deletions ql/methods/finitedifferences/operators/triplebandlinearop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ namespace QuantLib {
const ext::shared_ptr<FdmMesher>& mesher);

TripleBandLinearOp(const TripleBandLinearOp& m);
TripleBandLinearOp(TripleBandLinearOp&& m) QL_NOEXCEPT;
TripleBandLinearOp(const Disposable<TripleBandLinearOp>& m);
TripleBandLinearOp& operator=(const TripleBandLinearOp& m);
TripleBandLinearOp& operator=(TripleBandLinearOp&& m) QL_NOEXCEPT;
TripleBandLinearOp& operator=(const Disposable<TripleBandLinearOp>& m);

Disposable<Array> apply(const Array& r) const;
Expand Down Expand Up @@ -75,6 +77,23 @@ namespace QuantLib {

ext::shared_ptr<FdmMesher> mesher_;
};


inline TripleBandLinearOp::TripleBandLinearOp(TripleBandLinearOp&& m) QL_NOEXCEPT {
swap(m);
}

inline TripleBandLinearOp& TripleBandLinearOp::operator=(const TripleBandLinearOp& m) {
TripleBandLinearOp tmp(m);
swap(tmp);
return *this;
}

inline TripleBandLinearOp& TripleBandLinearOp::operator=(TripleBandLinearOp&& m) QL_NOEXCEPT {
swap(m);
return *this;
}

}

#endif
5 changes: 0 additions & 5 deletions ql/methods/finitedifferences/tridiagonaloperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ namespace QuantLib {
" instead of " << n_-1);
}

TridiagonalOperator::TridiagonalOperator(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
}

Disposable<Array> TridiagonalOperator::applyTo(const Array& v) const {
QL_REQUIRE(n_!=0,
"uninitialized TridiagonalOperator");
Expand Down
26 changes: 26 additions & 0 deletions ql/methods/finitedifferences/tridiagonaloperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ namespace QuantLib {
TridiagonalOperator(const Array& low,
const Array& mid,
const Array& high);
TridiagonalOperator(const TridiagonalOperator&) = default;
TridiagonalOperator(TridiagonalOperator&&) QL_NOEXCEPT;
TridiagonalOperator(const Disposable<TridiagonalOperator>&);
TridiagonalOperator& operator=(const TridiagonalOperator&);
TridiagonalOperator& operator=(TridiagonalOperator&&) QL_NOEXCEPT;
TridiagonalOperator& operator=(const Disposable<TridiagonalOperator>&);
//! \name Operator interface
//@{
Expand Down Expand Up @@ -128,6 +132,28 @@ namespace QuantLib {

// inline definitions

inline TridiagonalOperator::TridiagonalOperator(TridiagonalOperator&& from) QL_NOEXCEPT {
swap(from);
}

inline TridiagonalOperator::TridiagonalOperator(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
}

inline TridiagonalOperator& TridiagonalOperator::operator=(
const TridiagonalOperator& from) {
TridiagonalOperator temp(from);
swap(temp);
return *this;
}

inline TridiagonalOperator&
TridiagonalOperator::operator=(TridiagonalOperator&& from) QL_NOEXCEPT {
swap(from);
return *this;
}

inline TridiagonalOperator& TridiagonalOperator::operator=(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
Expand Down
8 changes: 8 additions & 0 deletions ql/qldefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,12 @@
#endif


// until we stop supporting Visual C++ 2013
#if defined(QL_PATCH_MSVC_2013)
# define QL_NOEXCEPT
#else
# define QL_NOEXCEPT noexcept
#endif


#endif
13 changes: 13 additions & 0 deletions ql/utilities/clone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ namespace QuantLib {
#endif
Clone(const T&);
Clone(const Clone<T>&);
Clone(Clone<T>&&) QL_NOEXCEPT;
Clone<T>& operator=(const T&);
Clone<T>& operator=(const Clone<T>&);
Clone<T>& operator=(Clone<T>&&) QL_NOEXCEPT;
T& operator*() const;
T* operator->() const;
bool empty() const;
Expand Down Expand Up @@ -92,6 +94,11 @@ namespace QuantLib {
inline Clone<T>::Clone(const Clone<T>& t)
: ptr_(t.empty() ? (T*)(0) : t->clone().release()) {}

template <class T>
inline Clone<T>::Clone(Clone<T>&& t) QL_NOEXCEPT {
swap(t);
}

template <class T>
inline Clone<T>& Clone<T>::operator=(const T& t) {
#if defined(QL_USE_STD_UNIQUE_PTR)
Expand All @@ -108,6 +115,12 @@ namespace QuantLib {
return *this;
}

template <class T>
inline Clone<T>& Clone<T>::operator=(Clone<T>&& t) QL_NOEXCEPT {
swap(t);
return *this;
}

template <class T>
inline T& Clone<T>::operator*() const {
QL_REQUIRE(!this->empty(), "no underlying objects");
Expand Down