blob: ed0d1c7b168eb5792aa194a2eac65adea9c570e2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2006 Peter Kümmel
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author makes no representations about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
#ifndef LOKI_PIMPL_INC_
#define LOKI_PIMPL_INC_
// $Id: Pimpl.h 751 2006-10-17 19:50:37Z syntheticpp $
/// \defgroup PimplGroup Pimpl
#ifndef LOKI_INHERITED_PIMPL_NAME
#define LOKI_INHERITED_PIMPL_NAME d
#endif
#ifndef LOKI_INHERITED_RIMPL_NAME
#define LOKI_INHERITED_RIMPL_NAME d
#endif
namespace Loki
{
//////////////////////////////////////////
/// \class ConstPropPtr
///
/// \ingroup PimplGroup
/// Simple const propagating smart pointer
/// Is the default smart pointer of Pimpl.
//////////////////////////////////////////
template<class T>
struct ConstPropPtr
{
explicit ConstPropPtr(T* p) : ptr_(p) {}
~ConstPropPtr() { delete ptr_; ptr_ = 0; }
T* operator->() { return ptr_; }
T& operator*() { return *ptr_; }
const T* operator->() const { return ptr_; }
const T& operator*() const { return *ptr_; }
private:
ConstPropPtr();
ConstPropPtr(const ConstPropPtr&);
ConstPropPtr& operator=(const ConstPropPtr&);
T* ptr_;
};
////////////////////////////////////////////////////////////////////////////////
/// \class Pimpl
///
/// \ingroup PimplGroup
///
/// Implements the Pimpl idiom. It's a wrapper for a smart pointer which
/// automatically creates and deletes the implementation object and adds
/// const propagation to the smart pointer.
///
/// \par Usage
/// see test/Pimpl
////////////////////////////////////////////////////////////////////////////////
template
<
class T,
typename Pointer = ConstPropPtr<T>
>
class Pimpl
{
public:
typedef T Impl;
Pimpl() : ptr_(new T)
{}
~Pimpl()
{
// Don't compile with incomplete type
//
// If compilation breaks here make sure
// the compiler does not auto-generate the
// destructor of the class hosting the pimpl:
// - implement the destructor of the class
// - don't inline the destructor
typedef char T_must_be_defined[sizeof(T) ? 1 : -1 ];
}
T* operator->()
{
return ptr_.operator->();
}
T& operator*()
{
return ptr_.operator*();
}
const T* operator->() const
{
return ptr_.operator->();
}
const T& operator*() const
{
return ptr_.operator*();
}
Pointer& wrapped()
{
return ptr_;
}
const Pointer& wrapped() const
{
return ptr_;
}
private:
Pimpl(const Pimpl&);
Pimpl& operator=(const Pimpl&);
Pointer ptr_;
};
template<class T, typename Pointer = ConstPropPtr<T> >
struct PimplOwner
{
Pimpl<T,Pointer> LOKI_INHERITED_PIMPL_NAME;
};
//////////////////////////////////////////
/// \class ImplOf
///
/// \ingroup PimplGroup
/// Convenience template for the
/// implementations which Pimpl points to.
//////////////////////////////////////////
template<class T>
struct ImplOf;
//////////////////////////////////////////
/// \class PImplOf
///
/// \ingroup PimplGroup
/// Convenience template which uses ImplOf
/// as implementation structure
//////////////////////////////////////////
template<class T, template<class> class Ptr = ConstPropPtr>
struct PimplOf
{
typedef T Impl;
// declare pimpl
typedef Pimpl<ImplOf<T>, Ptr<ImplOf<T> > > Type;
// inherit pimpl
typedef PimplOwner<ImplOf<T>, Ptr<ImplOf<T> > > Owner;
};
template<class T, class UsedPimpl = typename PimplOf<T>::Type >
struct RimplOf
{
typedef typename UsedPimpl::Impl & Type;
class Owner
{
UsedPimpl pimpl;
public:
Owner() : LOKI_INHERITED_RIMPL_NAME(*pimpl)
{}
Type LOKI_INHERITED_RIMPL_NAME;
};
};
}
#endif // end file guardian
|