// -*- C++ -*- /** * \file cow_ptr.h * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Angus Leeming * * A pointer with copy-on-write semantics * * The original version of this class was written by Yonat Sharon * and is freely available at http://ootips.org/yonat/ * * I modified it to use boost::shared_ptr internally, rather than use his * home-grown equivalent. */ #ifndef COW_PTR_H #define COW_PTR_H #include namespace lyx { namespace support { template class cow_ptr { public: explicit cow_ptr(T * = 0); cow_ptr(cow_ptr const &); cow_ptr & operator=(cow_ptr const &); T const & operator*() const; T const * operator->() const; T const * get() const; T & operator*(); T * operator->(); T * get(); private: boost::shared_ptr ptr_; void copy(); }; template cow_ptr::cow_ptr(T * p) : ptr_(p) {} template cow_ptr::cow_ptr(cow_ptr const & other) : ptr_(other.ptr_) {} template cow_ptr & cow_ptr::operator=(cow_ptr const & other) { if (&other != this) ptr_ = other.ptr_; return *this; } template T const & cow_ptr::operator*() const { return *ptr_; } template T const * cow_ptr::operator->() const { return ptr_.get(); } template T const * cow_ptr::get() const { return ptr_.get(); } template T & cow_ptr::operator*() { copy(); return *ptr_; } template T * cow_ptr::operator->() { copy(); return ptr_.get(); } template T * cow_ptr::get() { copy(); return ptr_.get(); } template void cow_ptr::copy() { if (!ptr_.unique()) ptr_ = boost::shared_ptr(new T(*ptr_.get())); } } // namespace support } // namespace lyx #endif // NOT COW_PTR_H