Debian Codegraph @

contrib/classtemplates/cloneable.h

raw
1 /*
2 Cloneable template class protects a derived object from slicing when
3 used with stl containers
4
5 Author: Jesse van den Kieboom
6
7 Jesse provides the following example based on his class template Cloneable:
8
9 vector<Cloneable<A> > vec;
10
11 vec.push_back(B());
12 vec.push_back(B());
13
14 Cloneable<A> a = B();
15 a->(functie op A/B)
16
17 Cloneable<B> b = B();
18 a = b;
19
20 */
21 #ifndef __CLONEABLE_H__
22 #define __CLONEABLE_H__
23
24 namespace jessevdk
25 {
26 template <typename Base>
27 class Cloneable
28 {
29 Base *d_base;
30
31 public:
32 /* Default constructor */
33 Cloneable();
34
35 template <typename Other>
36 Cloneable(Cloneable<Other> const &other);
37
38 /* Specialisation needed to override default copy constructor */
39 Cloneable(Cloneable<Base> const &other);
40
41 template <typename Other>
42 Cloneable(Other const &other);
43
44 /* Deconstructor */
45 virtual ~Cloneable();
46
47 /* Public functions */
48 template <typename Other>
49 Cloneable<Base> &operator=(Cloneable<Other> const &other);
50
51 /* Specialisation needed to override default assignment operator */
52 Cloneable<Base> &operator=(Cloneable<Base> const &other);
53
54 template <typename Other>
55 Cloneable<Base> &operator=(Other const &other);
56
57 /* Operators */
58 Base &operator*();
59 Base const &operator*() const;
60
61 Base *operator->();
62 Base const *operator->() const;
63 operator Base&();
64 private:
65 /* Private functions */
66 void destroy();
67
68 template <typename Other>
69 Cloneable<Base> &assign(Other const &other);
70 };
71
72 /* Constructors */
73 template <typename Base>
74 inline Cloneable<Base>::Cloneable()
75 {
76 d_base = new Base();
77 }
78
79 template <typename Base>
80 template <typename Other>
81 inline Cloneable<Base>::Cloneable(Other const &other)
82 {
83 /* Clone other */
84 d_base = other.clone();
85 }
86
87 template <typename Base>
88 inline Cloneable<Base>::Cloneable(Cloneable<Base> const &other)
89 {
90 /* Clone other from cloneable */
91 d_base = other->clone();
92 }
93
94 template <typename Base>
95 template <typename Other>
96 inline Cloneable<Base>::Cloneable(Cloneable<Other> const &other)
97 {
98 /* Clone other from cloneable */
99 d_base = other->clone();
100 }
101
102 /* Destructor */
103 template <typename Base>
104 inline Cloneable<Base>::~Cloneable()
105 {
106 destroy();
107 }
108
109 template <typename Base>
110 template <typename Other>
111 Cloneable<Base>& Cloneable<Base>::assign(Other const &other)
112 {
113 /* Assign other to this cloneable with self-destroy check */
114 if (d_base != &other)
115 {
116 destroy();
117 d_base = other.clone();
118 }
119
120 return *this;
121 }
122
123 template <typename Base>
124 Cloneable<Base>& Cloneable<Base>::operator=(Cloneable<Base> const &other)
125 {
126 return assign(*other);
127 }
128
129 template <typename Base>
130 template <typename Other>
131 Cloneable<Base>& Cloneable<Base>::operator=(Cloneable<Other> const &other)
132 {
133 return assign(*other);
134 }
135
136 template <typename Base>
137 template <typename Other>
138 Cloneable<Base>& Cloneable<Base>::operator=(Other const &other)
139 {
140 return assign(other);
141 }
142
143 template <typename Base>
144 inline void Cloneable<Base>::destroy()
145 {
146 delete d_base;
147 }
148
149 /* Operators */
150 template <typename Base>
151 inline Base &Cloneable<Base>::operator*()
152 {
153 return *d_base;
154 }
155
156 template <typename Base>
157 inline Base *Cloneable<Base>::operator->()
158 {
159 return d_base;
160 }
161
162 template <typename Base>
163 inline Base const &Cloneable<Base>::operator*() const
164 {
165 return *d_base;
166 }
167
168 template <typename Base>
169 inline Base const *Cloneable<Base>::operator->() const
170 {
171 return d_base;
172 }
173
174 template <typename Base>
175 inline Cloneable<Base>::operator Base&()
176 {
177 return *d_base;
178 }
179 }
180
181 #endif /* __CLONEABLE_H__ */