Another minor change, but this should almost get us to the point that we
[lyx.git] / development / attic / cow_ptr.h
blob685d0e4ba9740364d956b4fa3a31a0b68ebc14bb
1 // -*- C++ -*-
2 /**
3 * \file cow_ptr.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * A pointer with copy-on-write semantics
11 * The original version of this class was written by Yonat Sharon
12 * and is freely available at http://ootips.org/yonat/
14 * I modified it to use boost::shared_ptr internally, rather than use his
15 * home-grown equivalent.
18 #ifndef COW_PTR_H
19 #define COW_PTR_H
21 #include <boost/shared_ptr.hpp>
24 namespace lyx {
25 namespace support {
27 template <typename T>
28 class cow_ptr {
29 public:
30 explicit cow_ptr(T * = 0);
31 cow_ptr(cow_ptr const &);
32 cow_ptr & operator=(cow_ptr const &);
34 T const & operator*() const;
35 T const * operator->() const;
36 T const * get() const;
38 T & operator*();
39 T * operator->();
40 T * get();
42 private:
43 boost::shared_ptr<T> ptr_;
44 void copy();
48 template <typename T>
49 cow_ptr<T>::cow_ptr(T * p)
50 : ptr_(p)
54 template <typename T>
55 cow_ptr<T>::cow_ptr(cow_ptr const & other)
56 : ptr_(other.ptr_)
60 template <typename T>
61 cow_ptr<T> & cow_ptr<T>::operator=(cow_ptr const & other)
63 if (&other != this)
64 ptr_ = other.ptr_;
65 return *this;
69 template <typename T>
70 T const & cow_ptr<T>::operator*() const
72 return *ptr_;
76 template <typename T>
77 T const * cow_ptr<T>::operator->() const
79 return ptr_.get();
83 template <typename T>
84 T const * cow_ptr<T>::get() const
86 return ptr_.get();
90 template <typename T>
91 T & cow_ptr<T>::operator*()
93 copy();
94 return *ptr_;
98 template <typename T>
99 T * cow_ptr<T>::operator->()
101 copy();
102 return ptr_.get();
106 template <typename T>
107 T * cow_ptr<T>::get()
109 copy();
110 return ptr_.get();
114 template <typename T>
115 void cow_ptr<T>::copy()
117 if (!ptr_.unique())
118 ptr_ = boost::shared_ptr<T>(new T(*ptr_.get()));
121 } // namespace support
122 } // namespace lyx
124 #endif // NOT COW_PTR_H