QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
clone.hpp
Go to the documentation of this file.
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 <http://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
31namespace 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 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 ~Clone() = default;
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 : ptr_(t.clone().release()) {}
73
74 template <class T>
75 inline Clone<T>::Clone(const Clone<T>& t)
76 : ptr_(t.empty() ? (T*)nullptr : t->clone().release()) {}
77
78 template <class T>
79 inline Clone<T>::Clone(Clone<T>&& t) noexcept {
80 swap(t);
81 }
82
83 template <class T>
84 inline Clone<T>& Clone<T>::operator=(const T& t) {
85 ptr_ = t.clone();
86 return *this;
87 }
88
89 template <class T>
91 ptr_.reset(t.empty() ? (T*)nullptr : t->clone().release());
92 return *this;
93 }
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 inline T& Clone<T>::operator*() const {
103 QL_REQUIRE(!this->empty(), "no underlying objects");
104 return *(this->ptr_);
105 }
106
107 template <class T>
108 inline T* Clone<T>::operator->() const {
109 return this->ptr_.get();
110 }
111
112 template <class T>
113 inline bool Clone<T>::empty() const {
114 return !ptr_;
115 }
116
117 template <class T>
118 inline void Clone<T>::swap(Clone<T>& t) noexcept {
119 this->ptr_.swap(t.ptr_);
120 }
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
cloning proxy to an underlying object
Definition: clone.hpp:40
T & operator*() const
Definition: clone.hpp:102
Clone(const T &)
Definition: clone.hpp:71
Clone(std::unique_ptr< T > &&)
Definition: clone.hpp:67
bool empty() const
Definition: clone.hpp:113
T * operator->() const
Definition: clone.hpp:108
Clone(Clone< T > &&) noexcept
Definition: clone.hpp:79
std::unique_ptr< T > ptr_
Definition: clone.hpp:56
Clone< T > & operator=(const T &)
Definition: clone.hpp:84
void swap(Clone< T > &t) noexcept
Definition: clone.hpp:118
Clone()=default
Clone(const Clone< T > &)
Definition: clone.hpp:75
const DefaultType & t
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.