Newer
Older
Tomáš Oberhuber
committed
/***************************************************************************
SharedPointerHost.h - description
-------------------
begin : Aug 22, 2018
copyright : (C) 2018 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
// Implemented by: Tomas Oberhuber, Jakub Klinkovsky
#pragma once
Tomáš Oberhuber
committed
#include <TNL/Devices/Host.h>
Tomáš Oberhuber
committed
#include <TNL/Pointers/SmartPointer.h>
Tomáš Oberhuber
committed
namespace TNL {
Tomáš Oberhuber
committed
template< typename Object >
class SharedPointer< Object, Devices::Host > : public SmartPointer
Tomáš Oberhuber
committed
{
private:
// Convenient template alias for controlling the selection of copy- and
// move-constructors and assignment operators using SFINAE.
// The type Object_ is "enabled" iff Object_ and Object are not the same,
// but after removing const and volatile qualifiers they are the same.
template< typename Object_ >
using Enabler = std::enable_if< ! std::is_same< Object_, Object >::value &&
std::is_same< typename std::remove_cv< Object >::type, Object_ >::value >;
// friend class will be needed for templated assignment operators
template< typename Object_, typename Device_ >
friend class SharedPointer;
public:
typedef Object ObjectType;
typedef Devices::Host DeviceType;
typedef SharedPointer< Object, Devices::Host > ThisType;
SharedPointer( std::nullptr_t )
: pd( nullptr )
Tomáš Oberhuber
committed
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
template< typename... Args >
explicit SharedPointer( Args... args )
: pd( nullptr )
{
#ifdef TNL_DEBUG_SHARED_POINTERS
std::cerr << "Creating shared pointer to " << demangle(typeid(ObjectType).name()) << std::endl;
#endif
this->allocate( args... );
}
// this is needed only to avoid the default compiler-generated constructor
SharedPointer( const ThisType& pointer )
: pd( (PointerData*) pointer.pd )
{
this->pd->counter += 1;
}
// conditional constructor for non-const -> const data
template< typename Object_,
typename = typename Enabler< Object_ >::type >
SharedPointer( const SharedPointer< Object_, DeviceType >& pointer )
: pd( (PointerData*) pointer.pd )
{
this->pd->counter += 1;
}
// this is needed only to avoid the default compiler-generated constructor
SharedPointer( ThisType&& pointer )
: pd( (PointerData*) pointer.pd )
{
pointer.pd = nullptr;
}
// conditional constructor for non-const -> const data
template< typename Object_,
typename = typename Enabler< Object_ >::type >
SharedPointer( SharedPointer< Object_, DeviceType >&& pointer )
: pd( (PointerData*) pointer.pd )
{
pointer.pd = nullptr;
}
template< typename... Args >
bool recreate( Args... args )
{
#ifdef TNL_DEBUG_SHARED_POINTERS
std::cerr << "Recreating shared pointer to " << demangle(typeid(ObjectType).name()) << std::endl;
#endif
if( ! this->counter )
return this->allocate( args... );
if( *this->pd->counter == 1 )
{
/****
* The object is not shared -> recreate it in-place, without reallocation
*/
this->pd->data.~ObjectType();
new ( this->pd->data ) ObjectType( args... );
return true;
}
// free will just decrement the counter
this->free();
return this->allocate( args... );
}
const Object* operator->() const
{
return &this->pd->data;
}
Object* operator->()
{
return &this->pd->data;
}
const Object& operator *() const
{
return this->pd->data;
}
Object& operator *()
{
return this->pd->data;
}
__cuda_callable__
operator bool() const
{
return this->pd;
}
__cuda_callable__
bool operator!() const
{
return ! this->pd;
}
template< typename Device = Devices::Host >
__cuda_callable__
const Object& getData() const
{
return this->pd->data;
Tomáš Oberhuber
committed
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
199
200
201
202
203
204
205
206
207
208
template< typename Device = Devices::Host >
__cuda_callable__
Object& modifyData()
{
return this->pd->data;
}
// this is needed only to avoid the default compiler-generated operator
const ThisType& operator=( const ThisType& ptr )
{
this->free();
this->pd = (PointerData*) ptr.pd;
this->pd->counter += 1;
return *this;
}
// conditional operator for non-const -> const data
template< typename Object_,
typename = typename Enabler< Object_ >::type >
const ThisType& operator=( const SharedPointer< Object_, DeviceType >& ptr )
{
this->free();
this->pd = (PointerData*) ptr.pd;
this->pd->counter += 1;
return *this;
}
// this is needed only to avoid the default compiler-generated operator
const ThisType& operator=( ThisType&& ptr )
{
this->free();
this->pd = (PointerData*) ptr.pd;
ptr.pd = nullptr;
return *this;
}
// conditional operator for non-const -> const data
template< typename Object_,
typename = typename Enabler< Object_ >::type >
const ThisType& operator=( SharedPointer< Object_, DeviceType >&& ptr )
{
this->free();
this->pd = (PointerData*) ptr.pd;
ptr.pd = nullptr;
return *this;
}
bool synchronize()
{
return true;
}
Tomáš Oberhuber
committed
void clear()
{
this->free();
}
void swap( ThisType& ptr2 )
{
std::swap( this->pd, ptr2.pd );
}
Tomáš Oberhuber
committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
~SharedPointer()
{
this->free();
}
protected:
struct PointerData
{
Object data;
int counter;
template< typename... Args >
explicit PointerData( Args... args )
: data( args... ),
counter( 1 )
{}
};
template< typename... Args >
bool allocate( Args... args )
{
this->pd = new PointerData( args... );
return this->pd;
}
void free()
{
if( this->pd )
{
if( ! --this->pd->counter )
{
delete this->pd;
this->pd = nullptr;
}
}
}
PointerData* pd;
};
Tomáš Oberhuber
committed
} // namespace TNL