/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2006 StatPro Italia srl This file is part of QuantLib, a free-software/open-source library for financial quantitative analysts and developers - http://quantlib.org/ QuantLib is free software: you can redistribute it and/or modify it under the terms of the QuantLib license. You should have received a copy of the license along with this program; if not, please email . The license is also available online at . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. */ /*! \file clone.hpp \brief cloning proxy to an underlying object */ #ifndef quantlib_clone_hpp #define quantlib_clone_hpp #include #include #include #include namespace QuantLib { //! cloning proxy to an underlying object /*! When copied, this class will make a clone of its underlying object (which must provide a clone() method returning a std::auto_ptr to a newly-allocated instance.) */ template class Clone { public: Clone(); Clone(std::auto_ptr); Clone(const T&); Clone(const Clone&); Clone& operator=(const T&); Clone& operator=(const Clone&); T& operator*() const; T* operator->() const; bool empty() const; void swap(Clone& t); private: boost::scoped_ptr ptr_; }; /*! \relates Clone */ template void swap(Clone&, Clone&); // inline definitions template inline Clone::Clone() {} template inline Clone::Clone(std::auto_ptr p) : ptr_(p) {} template inline Clone::Clone(const T& t) : ptr_(t.clone().release()) {} template inline Clone::Clone(const Clone& t) : ptr_(t.empty() ? (T*)(0) : t->clone().release()) {} template inline Clone& Clone::operator=(const T& t) { ptr_.reset(t.clone().release()); return *this; } template inline Clone& Clone::operator=(const Clone& t) { ptr_.reset(t.empty() ? (T*)(0) : t->clone().release()); return *this; } template inline T& Clone::operator*() const { QL_REQUIRE(!this->empty(), "no underlying objects"); return *(this->ptr_); } template inline T* Clone::operator->() const { return this->ptr_.get(); } template inline bool Clone::empty() const { return !ptr_; } template inline void Clone::swap(Clone& t) { this->ptr_.swap(t.ptr_); } template inline void swap(Clone& t, Clone& u) { t.swap(u); } } #endif