From: Angus Leeming Date: Wed, 10 Sep 2003 23:06:18 +0000 (+0000) Subject: Add copied_ptr.h to the repository too. X-Git-Tag: 1.6.10~16111 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=d55d4b9c1c99488e37cbe383d5ff6cfa51c77254;p=features.git Add copied_ptr.h to the repository too. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7732 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/support/ChangeLog b/src/support/ChangeLog index ce82f3f96e..634cd5555e 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2003-09-11 Angus Leeming + + * cow_ptr.h: + * copied_ptr.h: added to the repository. Maybe temporarily. + 2003-09-09 Lars Gullik Bjønnes * Makefile.am (libsupport_la_SOURCES): remove LAssert.C and LAssert.h diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 9d7c22a366..6813f2c129 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -32,6 +32,8 @@ libsupport_la_SOURCES = \ boost-inst.C \ chdir.C \ copy.C \ + copied_ptr.h \ + cow_ptr.h \ filename.C \ filename.h \ filetools.C \ diff --git a/src/support/copied_ptr.h b/src/support/copied_ptr.h new file mode 100644 index 0000000000..0683158d81 --- /dev/null +++ b/src/support/copied_ptr.h @@ -0,0 +1,103 @@ +// -*- C++ -*- +/** + * \file copied_ptr.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Yonat Sharon http://ootips.org/yonat/ + * + * simple copy-on-create/assign pointer. + * + * Note: If the actual object pointed to belongs to a derived class, + * then copied_ptr will not create a copy of the derived class object, + * but a new base class object. + * If you want to use a polymorphic copy-on-assign pointer, use + * cloned_ptr. + */ + +#ifndef COPIED_PTR_H +#define COPIED_PTR_H + +namespace lyx { +namespace support { + +template +class copied_ptr { +public: + explicit copied_ptr(T * = 0); + ~copied_ptr(); + copied_ptr(copied_ptr const &); + copied_ptr & operator=(copied_ptr const &); + + T & operator*() const; + T * operator->() const; + T * get() const; + +private: + T * ptr_; + void copy(copied_ptr const &); +}; + + +template +copied_ptr::copied_ptr(T * p) + : ptr_(p) +{} + + +template +copied_ptr::~copied_ptr() +{ + delete ptr_; +} + + +template +copied_ptr::copied_ptr(copied_ptr const & other) +{ + copy(other.get()); +} + + +template +copied_ptr & copied_ptr::operator=(copied_ptr const & other) +{ + if (&other != this) { + delete ptr_; + copy(other); + } + return *this; +} + + +template +T & copied_ptr::operator*() const +{ + return *ptr_; +} + + +template +T * copied_ptr::operator->() const +{ + return ptr_; +} + + +template +T * copied_ptr::get() const +{ + return ptr_; +} + + +template +void copied_ptr::copy(copied_ptr const & other) +{ + ptr_ = other.ptr_ ? new T(*other.ptr_) : 0; +} + +} // namespace support +} // namespace lyx + +#endif // NOT COPIED_PTR_H