Clp  1.17.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: CoinSmartPtr.hpp 2083 2019-01-06 19:38:09Z unxusr $
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 
17 namespace Coin {
18 
19 //#########################################################################
20 
158 public:
160  : reference_count_(0)
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 
168 private:
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 
320 template < class T >
321 class SmartPtr {
322 public:
329  T *GetRawPtr() const { return ptr_; }
330 
335  bool IsValid() const { return ptr_ != NULL; }
336 
341  bool IsNull() const { return ptr_ == NULL; }
342 
343 private:
347  T *ptr_;
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  {
377  SetFromRawPtr_(rhs.GetRawPtr());
378  return (*this);
379  }
380 
382 
383 public:
384 #define dbg_smartptr_verbosity 0
385 
390  : ptr_(NULL)
391  {
392  }
393 
395  SmartPtr(const SmartPtr< T > &copy)
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  {
412  ReleasePointer_();
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 
485 template < class U1, class U2 >
486 bool 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 
504 template < class U1, class U2 >
506 {
507  return Coin::ComparePointers(lhs.GetRawPtr(), rhs.GetRawPtr());
508 }
509 
510 template < class U1, class U2 >
511 bool operator==(const Coin::SmartPtr< U1 > &lhs, U2 *raw_rhs)
512 {
513  return Coin::ComparePointers(lhs.GetRawPtr(), raw_rhs);
514 }
515 
516 template < class U1, class U2 >
517 bool operator==(U1 *raw_lhs, const Coin::SmartPtr< U2 > &rhs)
518 {
519  return Coin::ComparePointers(raw_lhs, rhs.GetRawPtr());
520 }
521 
522 template < class U1, class U2 >
524 {
525  return !operator==(lhs, rhs);
526 }
527 
528 template < class U1, class U2 >
529 bool operator!=(const Coin::SmartPtr< U1 > &lhs, U2 *raw_rhs)
530 {
531  return !operator==(lhs, raw_rhs);
532 }
533 
534 template < class U1, class U2 >
535 bool 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 */
void ReleasePointer_()
Release the currently referenced object.
bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
bool ComparePointers(const U1 *lhs, const U2 *rhs)
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
Template class for Smart Pointers.
int ReferenceCount() const
SmartPtr()
Default constructor, initialized to NULL.
T * ptr_
Actual raw pointer to the object.
T * GetRawPtr() const
Returns the raw pointer contained.
SmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer...
bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy.
ReferencedObject class.
void ReleaseRef() const
bool IsValid() const
Returns true if the SmartPtr is NOT NULL.
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
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...
T & operator*() const
Overloaded dereference operator, allows the user to dereference 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...
bool IsNull() const
Returns true if the SmartPtr is NULL.
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...