-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathautoptr.h
More file actions
505 lines (427 loc) · 12.4 KB
/
autoptr.h
File metadata and controls
505 lines (427 loc) · 12.4 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* =====================================================================================
*
* Filename: autoptr.h
*
* Description: declaration of autoptr classes
*
* Version: 1.0
* Created: 07/04/2016 08:40:49 PM
* Revision: none
* Compiler: gcc
*
* Author: Ming Zhi( woodhead99@gmail.com )
* Organization:
*
* Copyright: 2019 Ming Zhi( woodhead99@gmail.com )
*
* License: Licensed under GPL-3.0. You may not use this file except in
* compliance with the License. You may find a copy of the
* License at 'http://www.gnu.org/licenses/gpl-3.0.html'
*
* =====================================================================================
*/
#pragma once
#include <dbus/dbus.h>
#include "defines.h"
namespace rpcf
{
extern gint32 CoCreateInstance( EnumClsid clsid,
CObjBase*& pObj, const IConfigDb* pCfg = nullptr );
extern gint32 CreateObjFast( EnumClsid iClsid,
CObjBase*& pObj, const IConfigDb* pCfg = nullptr );
struct IAutoPtr{};
// typedef DBusMessage* DMSGPTR;
// note: class T must be a class inherited from CObjBase
// c++11 required
template< EnumClsid iClsid, class T >
// typename TFilter = typename std::enable_if<
// std::is_base_of<CObjBase, T>::value ||
// std::is_same<DMSGPTR, T>::value,
// T>::type >
class CAutoPtr : public IAutoPtr
{
private:
T* m_pObj;
public:
CAutoPtr( bool bNew, IConfigDb* pCfg = nullptr )
{
m_pObj = nullptr;
static_assert( iClsid != Clsid_Invalid,
"class id cannot be invalid" );
if( bNew )
{
gint32 ret = NewObj( iClsid, pCfg );
if( ERROR( ret ) )
{
std::string strMsg = DebugMsg(
ret, std::string( "failed to create the object" ) );
throw std::invalid_argument( strMsg );
}
}
}
// NOTE: cannot convert between different object
// classes
CAutoPtr( T* pObj = nullptr, bool bAddRef = true )
{
static_assert( std::is_base_of< CObjBase, T >::value,
"CObjBase must be the super class" );
if( pObj != nullptr )
{
m_pObj = pObj;
if( m_pObj && bAddRef )
{
m_pObj->AddRef();
}
}
else
{
m_pObj = nullptr;
}
}
// copy constructor
template< EnumClsid iClsid2, class U,
class T1 = typename std::enable_if<
std::is_base_of< T, U >::value ||
std::is_base_of< U, T >::value, U >::type >
CAutoPtr( const CAutoPtr< iClsid2, U >& oInitPtr )
{
m_pObj = dynamic_cast< T* >( ( U* )oInitPtr );
if( m_pObj )
{
m_pObj->AddRef();
}
else
{
// we allow empty initialization
}
}
// defult copy constructor
CAutoPtr( const CAutoPtr& oInitPtr )
{
m_pObj = oInitPtr.m_pObj;
if( m_pObj )
{
m_pObj->AddRef();
}
}
CAutoPtr( CAutoPtr&& oInitPtr )
{
m_pObj = oInitPtr.m_pObj;
if( m_pObj )
oInitPtr.Detach();
}
// move constructor
template< EnumClsid iClsid2, class U,
class T1 = typename std::enable_if<
std::is_base_of< T, U >::value ||
std::is_base_of< U, T >::value, U >::type >
CAutoPtr( CAutoPtr< iClsid2, U >&& oInitPtr )
{
m_pObj = dynamic_cast< T* >( ( U* )oInitPtr );
if( m_pObj )
oInitPtr.Detach();
}
~CAutoPtr()
{
Clear();
}
T& operator*() const noexcept
{
return *m_pObj;
}
T* operator->() const noexcept
{
return m_pObj;
}
template< class U, class T1 = typename std::enable_if<
std::is_base_of< T, U >::value ||
std::is_base_of< U, T >::value, U >::type >
operator U*() const
{
// U must be either the base class of T or
// super class of T
static_assert( std::is_base_of< CObjBase, U >::value,
"CObjBase must be the base class of the type to cast" );
return dynamic_cast< U* >( m_pObj );
}
template< class T1, class U = typename std::enable_if<
std::is_base_of< T, T1 >::value ||
std::is_base_of< T1, T >::value, T1 >::type >
operator U&() const
{
// T1 must be either the base class of T or
// super class of T
static_assert( std::is_base_of< CObjBase, U >::value,
"CObjBase must be the base class of the type to cast" );
U* ptr = dynamic_cast< U* >( m_pObj );
if( ptr == nullptr )
{
std::string strMsg = DebugMsg(
-EINVAL, std::string( "failed to cast to new type" ) );
throw std::invalid_argument( strMsg );
}
return *ptr;
}
T* operator=( T* rhs )
{
if( m_pObj == rhs )
return m_pObj;
Clear();
if( rhs != nullptr )
{
m_pObj = rhs;
m_pObj->AddRef();
}
return m_pObj;
}
template< EnumClsid iClsid2, typename U,
class T2 = typename std::enable_if<
std::is_base_of< T, U >::value ||
std::is_base_of< U, T >::value, U >::type >
CAutoPtr< iClsid, T >& operator=( const CAutoPtr< iClsid2, U >& rhs )
{
static_assert( std::is_base_of<CObjBase, U>::value,
"CObjBase must be the base class of the type to cast" );
if( rhs.IsEmpty() )
{
Clear();
return *this;
}
if( m_pObj != nullptr
&& m_pObj->GetObjId() == rhs->GetObjId() )
return *this;
Clear();
const T* pObj = dynamic_cast< const T* >( ( U* )rhs );
if( pObj != nullptr )
{
// well, the data content is not changed,
// and the change in ref count is not that
// much concerned
m_pObj = const_cast< T* >( pObj );
m_pObj->AddRef();
return *this;
}
std::string strMsg = DebugMsg(
-EFAULT, "bad cast in operator=" );
throw std::runtime_error( strMsg );
}
CAutoPtr& operator=( const CAutoPtr& rhs )
{
if( rhs.IsEmpty() )
{
Clear();
return *this;
}
// self assignment
if( m_pObj != nullptr
&& m_pObj->GetObjId() == rhs->GetObjId() )
{
return *this;
}
Clear();
const T* pObj = rhs.m_pObj;
if( pObj != nullptr )
{
// well, the data content is not changed,
// and the change in ref count is not that
// much concerned
m_pObj = const_cast< T* >( pObj );
m_pObj->AddRef();
return *this;
}
std::string strMsg = DebugMsg(
-EFAULT, "bad cast in operator=" );
throw std::runtime_error( strMsg );
}
template< EnumClsid iClsid2, typename U,
class T2 = typename std::enable_if<
std::is_base_of< T, U >::value ||
std::is_base_of< U, T >::value, U >::type >
CAutoPtr< iClsid, T >& operator=( CAutoPtr< iClsid2, U >&& rhs )
{
static_assert( std::is_base_of<CObjBase, U>::value,
"CObjBase must be the base class of the type to cast" );
if( rhs.IsEmpty() )
{
Clear();
return *this;
}
// self assignment
if( m_pObj != nullptr
&& m_pObj->GetObjId() == rhs->GetObjId() )
{
rhs.Clear();
return *this;
}
Clear();
const T* pObj = dynamic_cast< const T* >( ( U* )rhs );
if( pObj != nullptr )
{
// well, the data content is not changed,
// and the change in ref count is not that
// much concerned
m_pObj = const_cast< T* >( pObj );
rhs.Detach();
return *this;
}
std::string strMsg = DebugMsg(
-EFAULT, "bad cast in operator=" );
throw std::runtime_error( strMsg );
}
bool operator==( const CAutoPtr& rhs ) const noexcept
{
if(rhs.IsEmpty() )
{
if( IsEmpty() )
return true;
return false;
}
else if( IsEmpty() )
{
return false;
}
guint64 qwId = rhs.m_pObj->GetObjId();
return ( m_pObj->GetObjId() == qwId );
}
bool operator!=( const CAutoPtr& rhs ) const noexcept
{ return !operator==( rhs ); }
bool operator==( const T* rhs ) const noexcept
{
if(rhs == nullptr )
{
if( IsEmpty() )
return true;
return false;
}
else if( IsEmpty() )
{
return false;
}
guint64 qwId = rhs.m_pObj->GetObjId();
return ( m_pObj->GetObjId() == qwId );
}
bool operator!=( const T* rhs ) const noexcept
{ return !operator==( rhs ); }
inline bool IsEmpty() const
{
return ( m_pObj == nullptr );
}
inline void Detach()
{ m_pObj = nullptr; }
inline void Clear()
{
if( m_pObj != nullptr )
{
m_pObj->Release();
Detach();
}
}
gint32 NewObj(
EnumClsid iNewClsid = Clsid_Invalid,
const IConfigDb* pCfg = nullptr )
{
Clear();
// if clsid is Clsid_Invalid, the object cannot
// be created dynamically.
EnumClsid iEffective = clsid( Invalid );
if( iNewClsid != clsid( Invalid ) )
{
iEffective = iNewClsid;
}
else if( iClsid != clsid( Invalid ) )
{
iEffective = iClsid;
}
if( iEffective == clsid( Invalid ) )
return 0;
CObjBase* pObj = nullptr;
gint32 ret = CreateObjFast(
iEffective, pObj, pCfg );
if( SUCCEEDED( ret ) )
{
m_pObj = dynamic_cast< T* >( pObj );
if( m_pObj == nullptr )
{
pObj->Release();
ret = -EFAULT;
}
return ret;
}
ret = CoCreateInstance( iEffective, pObj, pCfg );
if( SUCCEEDED( ret ) )
{
m_pObj = dynamic_cast< T* >( pObj );
if( m_pObj == nullptr )
{
pObj->Release();
std::string strMsg =
DebugMsg( -EBADE, "bad cast happens" );
printf( "%s", strMsg.c_str() );
ret = -ENOTSUP;
}
}
return ret;
}
gint32 Deserialize( const CBuffer& oBuf )
{
const SERI_HEADER_BASE* pHeader = oBuf;
SERI_HEADER_BASE oHeader( *pHeader );
oHeader.ntoh();
if( oHeader.dwClsid != iClsid )
return -ENOTSUP;
Clear();
gint32 ret = NewObj( iClsid );
if( ERROR( ret ) )
return ret;
return m_pObj->Deserialize( oBuf );
}
};
typedef CAutoPtr< clsid( Invalid ), CObjBase > ObjPtr;
typedef CAutoPtr< clsid( CBuffer ), CBuffer > BufPtr;
typedef CAutoPtr< clsid( Invalid ), IService > ServicePtr;
typedef CAutoPtr< clsid( CConfigDb2 ), IConfigDb > CfgPtr;
typedef CAutoPtr< clsid( Invalid ), IEventSink > EventPtr;
typedef CAutoPtr< clsid( Invalid ), IThread > ThreadPtr;
// typedef CAutoPtr< clsid( Invalid ), DBusMessage > DMsgPtr;
}
#include "dmsgptr.h"
namespace std
{
using namespace rpcf;
template<>
struct less<ObjPtr>
{
bool operator()(const ObjPtr& k1, const ObjPtr& k2) const
{
if( k2.IsEmpty() )
return false;
if( k1.IsEmpty() )
return true;
return k1->GetObjId() < k2->GetObjId();
}
};
template<>
struct less<EventPtr>
{
bool operator()(const EventPtr& k1, const EventPtr& k2) const
{
if( k2.IsEmpty() )
return false;
if( k1.IsEmpty() )
return true;
return k1->GetObjId() < k2->GetObjId();
}
};
};
namespace rpcf
{
// c++11 required
template <typename R, typename C, typename ... Types>
inline constexpr size_t GetArgCount( R(C::*f)(Types ...) )
{
return sizeof...(Types);
};
extern gint32 DeserializeObj(
const CBuffer& oBuf, ObjPtr& pObj );
}