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 
156  public:
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 
347  if (ptr_) {
348  ptr_->ReleaseRef();
349  if (ptr_->ReferenceCount() == 0) {
350  delete ptr_;
351  }
352  ptr_ = NULL;
353  }
354  }
355 
359  ReleasePointer_(); // Release any old pointer
360  if (rhs != NULL) {
361  rhs->AddRef();
362  ptr_ = rhs;
363  }
364  return *this;
365  }
366 
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 
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 
424  return SetFromRawPtr_(rhs);
425  }
426 
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
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...
SmartPtr< T > & operator=(const SmartPtr< T > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr...
int ReferenceCount() const
bool IsNull() const
Returns true if the SmartPtr is NULL.
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
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...
bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
void ReleaseRef() const
Template class for Smart Pointers.
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
typedef void(COINLINKAGE_CB *clp_callback)(Clp_Simplex *model
typedef for user call back.
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy.
SmartPtr()
Default constructor, initialized to NULL.
ReferencedObject class.
bool IsValid() const
Returns true if the SmartPtr is NOT NULL.
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
bool ComparePointers(const U1 *lhs, const U2 *rhs)
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
SmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer...
void ReleasePointer_()
Release the currently referenced object.
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...
bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
T * ptr_
Actual raw pointer to the object.
T * GetRawPtr() const
Returns the raw pointer contained.