Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
opt
build
coinutils
coinutils-2.6.4
CoinUtils
src
CoinSmartPtr.hpp
Go to the documentation of this file.
1
// Copyright (C) 2004, 2006 International Business Machines and others.
2
// All Rights Reserved.
3
// This code is published under the Common Public License.
4
//
5
// $Id: CoinSmartPtr.hpp 1191 2009-07-25 08:38:12Z forrest $
6
//
7
// Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
8
// Removed lots of debugging stuff and reformatted: Laszlo Ladanyi, IBM
9
#ifndef CoinSmartPtr_hpp
10
#define CoinSmartPtr_hpp
11
12
#include <list>
13
#include <cassert>
14
15
namespace
Coin {
16
17
//#########################################################################
18
155
class
ReferencedObject
{
156
public
:
157
ReferencedObject
() :
reference_count_
(0) {}
158
virtual
~ReferencedObject
() { assert(
reference_count_
== 0); }
159
inline
int
ReferenceCount
()
const
{
return
reference_count_
; }
160
inline
void
AddRef
()
const
{ ++
reference_count_
; }
161
inline
void
ReleaseRef
()
const
{ --
reference_count_
; }
162
163
private
:
164
mutable
int
reference_count_
;
165
};
166
167
//#########################################################################
168
169
170
//#define IP_DEBUG_SMARTPTR
171
#if COIN_IPOPT_CHECKLEVEL > 2
172
# define IP_DEBUG_SMARTPTR
173
#endif
174
#ifdef IP_DEBUG_SMARTPTR
175
# include "IpDebug.hpp"
176
#endif
177
316
template
<
class
T>
317
class
SmartPtr
{
318
public
:
325
T*
GetRawPtr
()
const
{
return
ptr_
; }
326
331
bool
IsValid
()
const
{
return
ptr_
!= NULL; }
332
337
bool
IsNull
()
const
{
return
ptr_
== NULL; }
338
339
private
:
343
T*
ptr_
;
344
346
void
ReleasePointer_
() {
347
if
(
ptr_
) {
348
ptr_
->ReleaseRef();
349
if
(
ptr_
->ReferenceCount() == 0) {
350
delete
ptr_
;
351
}
352
ptr_
= NULL;
353
}
354
}
355
358
SmartPtr<T>
&
SetFromRawPtr_
(T* rhs){
359
ReleasePointer_
();
// Release any old pointer
360
if
(rhs != NULL) {
361
rhs->AddRef();
362
ptr_
= rhs;
363
}
364
return
*
this
;
365
}
366
369
inline
SmartPtr<T>
&
SetFromSmartPtr_
(
const
SmartPtr<T>
& rhs) {
370
SetFromRawPtr_
(rhs.
GetRawPtr
());
371
return
(*
this
);
372
}
373
375
376
public
:
377
#define dbg_smartptr_verbosity 0
378
382
SmartPtr
() :
ptr_
(NULL) {}
383
385
SmartPtr
(
const
SmartPtr<T>
& copy) :
ptr_
(NULL) {
386
(
void
)
SetFromSmartPtr_
(copy);
387
}
388
390
SmartPtr
(T* ptr) :
ptr_
(NULL) {
391
(
void
)
SetFromRawPtr_
(ptr);
392
}
393
396
~SmartPtr
() {
397
ReleasePointer_
();
398
}
400
405
T*
operator->
()
const
{
406
#if COIN_COINUTILS_CHECKLEVEL > 0
407
assert(
ptr_
);
408
#endif
409
return
ptr_
;
410
}
411
414
T&
operator*
()
const
{
415
#if COIN_IPOPT_CHECKLEVEL > 0
416
assert(
ptr_
);
417
#endif
418
return
*
ptr_
;
419
}
420
423
SmartPtr<T>
&
operator=
(T* rhs) {
424
return
SetFromRawPtr_
(rhs);
425
}
426
430
SmartPtr<T>
&
operator=
(
const
SmartPtr<T>
& rhs) {
431
return
SetFromSmartPtr_
(rhs);
432
}
433
436
template
<
class
U1,
class
U2>
437
friend
438
bool
operator==
(
const
SmartPtr<U1>
& lhs,
const
SmartPtr<U2>
& rhs);
439
442
template
<
class
U1,
class
U2>
443
friend
444
bool
operator==
(
const
SmartPtr<U1>
& lhs, U2* raw_rhs);
445
448
template
<
class
U1,
class
U2>
449
friend
450
bool
operator==
(U1* lhs,
const
SmartPtr<U2>
& raw_rhs);
451
454
template
<
class
U1,
class
U2>
455
friend
456
bool
operator!=
(
const
SmartPtr<U1>
& lhs,
const
SmartPtr<U2>
& rhs);
457
460
template
<
class
U1,
class
U2>
461
friend
462
bool
operator!=
(
const
SmartPtr<U1>
& lhs, U2* raw_rhs);
463
466
template
<
class
U1,
class
U2>
467
friend
468
bool
operator!=
(U1* lhs,
const
SmartPtr<U2>
& raw_rhs);
470
471
};
472
473
template
<
class
U1,
class
U2>
474
bool
ComparePointers
(
const
U1* lhs,
const
U2* rhs) {
475
if
(lhs == rhs) {
476
return
true
;
477
}
478
// If lhs and rhs point to the same object with different interfaces
479
// U1 and U2, we cannot guarantee that the value of the pointers will
480
// be equivalent. We can guarantee this if we convert to void*.
481
return
static_cast<
const
void
*
>
(lhs) == static_cast<const void*>(rhs);
482
}
483
484
}
// namespace Coin
485
486
//#############################################################################
487
491
template
<
class
U1,
class
U2>
492
bool
operator==
(
const
Coin::SmartPtr<U1>
& lhs,
const
Coin::SmartPtr<U2>
& rhs) {
493
return
Coin::ComparePointers
(lhs.
GetRawPtr
(), rhs.
GetRawPtr
());
494
}
495
496
template
<
class
U1,
class
U2>
497
bool
operator==
(
const
Coin::SmartPtr<U1>
& lhs, U2* raw_rhs) {
498
return
Coin::ComparePointers
(lhs.
GetRawPtr
(), raw_rhs);
499
}
500
501
template
<
class
U1,
class
U2>
502
bool
operator==
(U1* raw_lhs,
const
Coin::SmartPtr<U2>
& rhs) {
503
return
Coin::ComparePointers
(raw_lhs, rhs.
GetRawPtr
());
504
}
505
506
template
<
class
U1,
class
U2>
507
bool
operator!=
(
const
Coin::SmartPtr<U1>
& lhs,
const
Coin::SmartPtr<U2>
& rhs) {
508
return
!
operator==
(lhs, rhs);
509
}
510
511
template
<
class
U1,
class
U2>
512
bool
operator!=
(
const
Coin::SmartPtr<U1>
& lhs, U2* raw_rhs) {
513
return
!
operator==
(lhs, raw_rhs);
514
}
515
516
template
<
class
U1,
class
U2>
517
bool
operator!=
(U1* raw_lhs,
const
Coin::SmartPtr<U2>
& rhs) {
518
return
!
operator==
(raw_lhs, rhs);
519
}
521
522
#define CoinReferencedObject Coin::ReferencedObject
523
#define CoinSmartPtr Coin::SmartPtr
524
#define CoinComparePointers Coin::ComparePointers
525
526
#endif
Generated on Tue Mar 1 2016 22:31:56 by
1.8.4