]> git.lyx.org Git - lyx.git/blob - src/support/copied_ptr.h
Use explicit macro to declare that we want to use C++11
[lyx.git] / src / support / copied_ptr.h
1 // -*- C++ -*-
2 /**
3  * \file copied_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 templated class that can serve as a pointer to an object, with the
10  * property that when the copied_ptr is copied, it creates its own copy
11  * of the object pointed to as well.
12  *
13  * The implementation was based originally on Yonat Sharon's copied_ptr templated
14  * class, as described at http://ootips.org/yonat/, but has evolved toward's
15  * Herb Sutter's HolderPtr, as described at http://www.gotw.ca/gotw/062.htm.
16  * (Note, HolderPtr became ValuePtr in his book, More Exceptional C++.)
17  *
18  * Warning: if the class stores 'Base * ptr_', but the actual object pointed to
19  * belongs to a derived class, then you must specialise memory_traits<Base> so that
20  * its clone and destroy member functions do the right thing. Otherwise, you'll
21  * end up slicing the data.
22  */
23
24 #ifndef COPIED_PTR_H
25 #define COPIED_PTR_H
26
27 namespace lyx {
28 namespace support {
29
30 template <typename T>
31 struct memory_traits {
32         static T * clone(T const * ptr) { return new T(*ptr); }
33         static void destroy(T * ptr) { delete ptr; }
34 };
35
36
37 template <typename T, typename Traits=memory_traits<T> >
38 class copied_ptr {
39 public:
40         explicit copied_ptr(T * = 0);
41         copied_ptr(copied_ptr const &);
42         ~copied_ptr();
43         copied_ptr & operator=(copied_ptr const &);
44
45         T & operator*() const;
46         T * operator->() const;
47         T * get() const;
48
49 private:
50         T * ptr_;
51 };
52
53
54 template <typename T, typename Traits>
55 copied_ptr<T, Traits>::copied_ptr(T * p)
56         : ptr_(p)
57 {}
58
59
60 template <typename T, typename Traits>
61 copied_ptr<T, Traits>::copied_ptr(copied_ptr const & other)
62         : ptr_(other.ptr_ ? Traits::clone(other.ptr_) : 0)
63 {}
64
65
66 template <typename T, typename Traits>
67 copied_ptr<T, Traits>::~copied_ptr()
68 {
69         Traits::destroy(ptr_);
70 }
71
72
73 template <typename T, typename Traits>
74 copied_ptr<T, Traits> & copied_ptr<T, Traits>::operator=(copied_ptr const & other)
75 {
76         if (&other != this) {
77                 copied_ptr temp(other);
78                 std::swap(ptr_, temp.ptr_);
79         }
80         return *this;
81 }
82
83
84 template <typename T, typename Traits>
85 T & copied_ptr<T, Traits>::operator*() const
86 {
87         return *ptr_;
88 }
89
90
91 template <typename T, typename Traits>
92 T * copied_ptr<T, Traits>::operator->() const
93 {
94         return ptr_;
95 }
96
97
98 template <typename T, typename Traits>
99 T * copied_ptr<T, Traits>::get() const
100 {
101         return ptr_;
102 }
103
104 } // namespace support
105 } // namespace lyx
106
107 #endif // NOT COPIED_PTR_H