]> git.lyx.org Git - lyx.git/blob - development/attic/cow_ptr.h
I'll find a solution for the 'dirList problem', Abdel.
[lyx.git] / development / attic / cow_ptr.h
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.
6  *
7  * \author Angus Leeming
8  *
9  * A pointer with copy-on-write semantics
10  *
11  * The original version of this class was written by Yonat Sharon
12  * and is freely available at http://ootips.org/yonat/
13  *
14  * I modified it to use boost::shared_ptr internally, rather than use his
15  * home-grown equivalent.
16  */
17
18 #ifndef COW_PTR_H
19 #define COW_PTR_H
20
21 #include <boost/shared_ptr.hpp>
22
23
24 namespace lyx {
25 namespace support {
26
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 &);
33
34         T const & operator*() const;
35         T const * operator->() const;
36         T const * get() const;
37
38         T & operator*();
39         T * operator->();
40         T * get();
41
42 private:
43         boost::shared_ptr<T> ptr_;
44         void copy();
45 };
46
47
48 template <typename T>
49 cow_ptr<T>::cow_ptr(T * p)
50         : ptr_(p)
51 {}
52
53
54 template <typename T>
55 cow_ptr<T>::cow_ptr(cow_ptr const & other)
56         : ptr_(other.ptr_)
57 {}
58
59
60 template <typename T>
61 cow_ptr<T> & cow_ptr<T>::operator=(cow_ptr const & other)
62 {
63         if (&other != this)
64                 ptr_ = other.ptr_;
65         return *this;
66 }
67
68
69 template <typename T>
70 T const & cow_ptr<T>::operator*() const
71 {
72         return *ptr_;
73 }
74
75
76 template <typename T>
77 T const * cow_ptr<T>::operator->() const
78 {
79         return ptr_.get();
80 }
81
82
83 template <typename T>
84 T const * cow_ptr<T>::get() const
85 {
86         return ptr_.get();
87 }
88
89
90 template <typename T>
91 T & cow_ptr<T>::operator*()
92 {
93         copy();
94         return *ptr_;
95 }
96
97
98 template <typename T>
99 T * cow_ptr<T>::operator->()
100 {
101         copy();
102         return ptr_.get();
103 }
104
105
106 template <typename T>
107 T * cow_ptr<T>::get()
108 {
109         copy();
110         return ptr_.get();
111 }
112
113
114 template <typename T>
115 void cow_ptr<T>::copy()
116 {
117         if (!ptr_.unique())
118                 ptr_ = boost::shared_ptr<T>(new T(*ptr_.get()));
119 }
120
121 } // namespace support
122 } // namespace lyx
123
124 #endif // NOT COW_PTR_H