/src/quantlib/ql/utilities/clone.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2006 StatPro Italia srl |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | /*! \file clone.hpp |
21 | | \brief cloning proxy to an underlying object |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_clone_hpp |
25 | | #define quantlib_clone_hpp |
26 | | |
27 | | #include <ql/errors.hpp> |
28 | | #include <algorithm> |
29 | | #include <memory> |
30 | | |
31 | | namespace QuantLib { |
32 | | |
33 | | //! cloning proxy to an underlying object |
34 | | /*! When copied, this class will make a clone of its underlying |
35 | | object, which must provide a <tt>clone()</tt> method returning |
36 | | a std::auto_ptr (or a std::unique_ptr, depending on your |
37 | | configuration) to a newly-allocated instance. |
38 | | */ |
39 | | template <class T> |
40 | | class Clone { |
41 | | public: |
42 | 0 | Clone() = default; |
43 | | Clone(std::unique_ptr<T>&&); |
44 | | Clone(const T&); |
45 | | Clone(const Clone<T>&); |
46 | | Clone(Clone<T>&&) noexcept; |
47 | | Clone<T>& operator=(const T&); |
48 | | Clone<T>& operator=(const Clone<T>&); |
49 | | Clone<T>& operator=(Clone<T>&&) noexcept; |
50 | | T& operator*() const; |
51 | | T* operator->() const; |
52 | | bool empty() const; |
53 | | void swap(Clone<T>& t) noexcept; |
54 | 0 | ~Clone() = default; Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelParametricExercise>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::~Clone() Unexecuted instantiation: QuantLib::Clone<QuantLib::FittedBondDiscountCurve::FittingMethod>::~Clone() |
55 | | private: |
56 | | std::unique_ptr<T> ptr_; |
57 | | }; |
58 | | |
59 | | /*! \relates Clone */ |
60 | | template <class T> |
61 | | void swap(Clone<T>&, Clone<T>&) noexcept; |
62 | | |
63 | | |
64 | | // inline definitions |
65 | | |
66 | | template <class T> |
67 | | inline Clone<T>::Clone(std::unique_ptr<T>&& p) |
68 | | : ptr_(std::move(p)) {} |
69 | | |
70 | | template <class T> |
71 | | inline Clone<T>::Clone(const T& t) |
72 | 0 | : ptr_(t.clone().release()) {}Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelParametricExercise>::Clone(QuantLib::MarketModelParametricExercise const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::Clone(QuantLib::CurveState const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::Clone(QuantLib::MarketModelMultiProduct const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::Clone(QuantLib::MarketModelExerciseValue const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::Clone(QuantLib::ExerciseStrategy<QuantLib::CurveState> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::Clone(QuantLib::MarketModelPathwiseMultiProduct const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::FittedBondDiscountCurve::FittingMethod>::Clone(QuantLib::FittedBondDiscountCurve::FittingMethod const&) |
73 | | |
74 | | template <class T> |
75 | | inline Clone<T>::Clone(const Clone<T>& t) |
76 | 0 | : ptr_(t.empty() ? (T*)nullptr : t->clone().release()) {}Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::Clone(QuantLib::Clone<QuantLib::MarketModelMultiProduct> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::Clone(QuantLib::Clone<QuantLib::MarketModelBasisSystem> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::Clone(QuantLib::Clone<QuantLib::MarketModelExerciseValue> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelParametricExercise>::Clone(QuantLib::Clone<QuantLib::MarketModelParametricExercise> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::Clone(QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> > const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::Clone(QuantLib::Clone<QuantLib::CurveState> const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::Clone(QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct> const&) |
77 | | |
78 | | template <class T> |
79 | 0 | inline Clone<T>::Clone(Clone<T>&& t) noexcept { |
80 | 0 | swap(t); |
81 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::Clone(QuantLib::Clone<QuantLib::MarketModelBasisSystem>&&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::Clone(QuantLib::Clone<QuantLib::MarketModelExerciseValue>&&) Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::Clone(QuantLib::Clone<QuantLib::CurveState>&&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::Clone(QuantLib::Clone<QuantLib::MarketModelMultiProduct>&&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::Clone(QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>&&) |
82 | | |
83 | | template <class T> |
84 | 0 | inline Clone<T>& Clone<T>::operator=(const T& t) { |
85 | 0 | ptr_ = t.clone(); |
86 | 0 | return *this; |
87 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::operator=(QuantLib::MarketModelMultiProduct const&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::operator=(QuantLib::MarketModelPathwiseMultiProduct const&) |
88 | | |
89 | | template <class T> |
90 | 0 | inline Clone<T>& Clone<T>::operator=(const Clone<T>& t) { |
91 | 0 | ptr_.reset(t.empty() ? (T*)nullptr : t->clone().release()); |
92 | 0 | return *this; |
93 | 0 | } |
94 | | |
95 | | template <class T> |
96 | | inline Clone<T>& Clone<T>::operator=(Clone<T>&& t) noexcept { |
97 | | swap(t); |
98 | | return *this; |
99 | | } |
100 | | |
101 | | template <class T> |
102 | 0 | inline T& Clone<T>::operator*() const { |
103 | 0 | QL_REQUIRE(!this->empty(), "no underlying objects"); |
104 | 0 | return *(this->ptr_); |
105 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::operator*() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::operator*() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::operator*() const Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::operator*() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::operator*() const Unexecuted instantiation: QuantLib::Clone<QuantLib::FittedBondDiscountCurve::FittingMethod>::operator*() const |
106 | | |
107 | | template <class T> |
108 | 0 | inline T* Clone<T>::operator->() const { |
109 | 0 | return this->ptr_.get(); |
110 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelParametricExercise>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::operator->() const Unexecuted instantiation: QuantLib::Clone<QuantLib::FittedBondDiscountCurve::FittingMethod>::operator->() const |
111 | | |
112 | | template <class T> |
113 | 0 | inline bool Clone<T>::empty() const { |
114 | 0 | return !ptr_; |
115 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelParametricExercise>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::ExerciseStrategy<QuantLib::CurveState> >::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::empty() const Unexecuted instantiation: QuantLib::Clone<QuantLib::FittedBondDiscountCurve::FittingMethod>::empty() const |
116 | | |
117 | | template <class T> |
118 | 0 | inline void Clone<T>::swap(Clone<T>& t) noexcept { |
119 | 0 | this->ptr_.swap(t.ptr_); |
120 | 0 | } Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelBasisSystem>::swap(QuantLib::Clone<QuantLib::MarketModelBasisSystem>&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelExerciseValue>::swap(QuantLib::Clone<QuantLib::MarketModelExerciseValue>&) Unexecuted instantiation: QuantLib::Clone<QuantLib::CurveState>::swap(QuantLib::Clone<QuantLib::CurveState>&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelMultiProduct>::swap(QuantLib::Clone<QuantLib::MarketModelMultiProduct>&) Unexecuted instantiation: QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>::swap(QuantLib::Clone<QuantLib::MarketModelPathwiseMultiProduct>&) |
121 | | |
122 | | template <class T> |
123 | | inline void swap(Clone<T>& t, Clone<T>& u) noexcept { |
124 | | t.swap(u); |
125 | | } |
126 | | |
127 | | } |
128 | | |
129 | | |
130 | | #endif |