CoinUtils 2.11.10
Loading...
Searching...
No Matches
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 Eclipse Public License.
4//
5// $Id$
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#include <cstddef>
15#include <cstring>
16
17namespace Coin {
18
19//#########################################################################
20
158public:
161 {
162 }
163 virtual ~ReferencedObject() { assert(reference_count_ == 0); }
164 inline int ReferenceCount() const { return reference_count_; }
165 inline void AddRef() const { ++reference_count_; }
166 inline void ReleaseRef() const { --reference_count_; }
167
168private:
169 mutable int reference_count_;
170};
171
172//#########################################################################
173
174//#define IP_DEBUG_SMARTPTR
175#if COIN_IPOPT_CHECKLEVEL > 2
176#define IP_DEBUG_SMARTPTR
177#endif
178#ifdef IP_DEBUG_SMARTPTR
179#include "IpDebug.hpp"
180#endif
181
320template < class T >
321class SmartPtr {
322public:
329 T *GetRawPtr() const { return ptr_; }
330
335 bool IsValid() const { return ptr_ != NULL; }
336
341 bool IsNull() const { return ptr_ == NULL; }
342
343private:
348
351 {
352 if (ptr_) {
353 ptr_->ReleaseRef();
354 if (ptr_->ReferenceCount() == 0) {
355 delete ptr_;
356 }
357 ptr_ = NULL;
358 }
359 }
360
364 {
365 ReleasePointer_(); // Release any old pointer
366 if (rhs != NULL) {
367 rhs->AddRef();
368 ptr_ = rhs;
369 }
370 return *this;
371 }
372
376 {
378 return (*this);
379 }
380
382
383public:
384#define dbg_smartptr_verbosity 0
385
390 : ptr_(NULL)
391 {
392 }
393
396 : ptr_(NULL)
397 {
398 (void)SetFromSmartPtr_(copy);
399 }
400
402 SmartPtr(T *ptr)
403 : ptr_(NULL)
404 {
405 (void)SetFromRawPtr_(ptr);
406 }
407
411 {
413 }
415
420 T *operator->() const
421 {
422#if COIN_COINUTILS_CHECKLEVEL > 0
423 assert(ptr_);
424#endif
425 return ptr_;
426 }
427
430 T &operator*() const
431 {
432#if COIN_IPOPT_CHECKLEVEL > 0
433 assert(ptr_);
434#endif
435 return *ptr_;
436 }
437
441 {
442 return SetFromRawPtr_(rhs);
443 }
444
449 {
450 return SetFromSmartPtr_(rhs);
451 }
452
455 template < class U1, class U2 >
456 friend bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs);
457
460 template < class U1, class U2 >
461 friend bool operator==(const SmartPtr< U1 > &lhs, U2 *raw_rhs);
462
465 template < class U1, class U2 >
466 friend bool operator==(U1 *lhs, const SmartPtr< U2 > &raw_rhs);
467
470 template < class U1, class U2 >
471 friend bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs);
472
475 template < class U1, class U2 >
476 friend bool operator!=(const SmartPtr< U1 > &lhs, U2 *raw_rhs);
477
480 template < class U1, class U2 >
481 friend bool operator!=(U1 *lhs, const SmartPtr< U2 > &raw_rhs);
483};
484
485template < class U1, class U2 >
486bool ComparePointers(const U1 *lhs, const U2 *rhs)
487{
488 if (lhs == rhs) {
489 return true;
490 }
491 // If lhs and rhs point to the same object with different interfaces
492 // U1 and U2, we cannot guarantee that the value of the pointers will
493 // be equivalent. We can guarantee this if we convert to void*.
494 return static_cast< const void * >(lhs) == static_cast< const void * >(rhs);
495}
496
497} // namespace Coin
498
499//#############################################################################
500
504template < class U1, class U2 >
506{
507 return Coin::ComparePointers(lhs.GetRawPtr(), rhs.GetRawPtr());
508}
509
510template < class U1, class U2 >
511bool operator==(const Coin::SmartPtr< U1 > &lhs, U2 *raw_rhs)
512{
513 return Coin::ComparePointers(lhs.GetRawPtr(), raw_rhs);
514}
515
516template < class U1, class U2 >
517bool operator==(U1 *raw_lhs, const Coin::SmartPtr< U2 > &rhs)
518{
519 return Coin::ComparePointers(raw_lhs, rhs.GetRawPtr());
520}
521
522template < class U1, class U2 >
524{
525 return !operator==(lhs, rhs);
526}
527
528template < class U1, class U2 >
529bool operator!=(const Coin::SmartPtr< U1 > &lhs, U2 *raw_rhs)
530{
531 return !operator==(lhs, raw_rhs);
532}
533
534template < class U1, class U2 >
535bool operator!=(U1 *raw_lhs, const Coin::SmartPtr< U2 > &rhs)
536{
537 return !operator==(raw_lhs, rhs);
538}
540
541#define CoinReferencedObject Coin::ReferencedObject
542#define CoinSmartPtr Coin::SmartPtr
543#define CoinComparePointers Coin::ComparePointers
544
545#endif
546
547/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
548*/
bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
ReferencedObject class.
Template class for Smart Pointers.
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
T * ptr_
Actual raw pointer to the object.
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
SmartPtr< T > & operator=(const SmartPtr< T > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr.
friend bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Overloaded in-equality comparison operator, allows the user to compare the value of two SmartPtrs.
friend bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Overloaded equality comparison operator, allows the user to compare the value of two SmartPtrs.
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
T * GetRawPtr() const
Returns the raw pointer contained.
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
SmartPtr()
Default constructor, initialized to NULL.
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy.
void ReleasePointer_()
Release the currently referenced object.
SmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer.
bool IsNull() const
Returns true if the SmartPtr is NULL.
bool IsValid() const
Returns true if the SmartPtr is NOT NULL.
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
bool ComparePointers(const U1 *lhs, const U2 *rhs)