Cbc  2.9.9
 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 1520 2012-01-29 00:43:31Z tkr $
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  virtual ~ReferencedObject() { assert(reference_count_ == 0); }
161  inline int ReferenceCount() const { return reference_count_; }
162  inline void AddRef() const { ++reference_count_; }
163  inline void ReleaseRef() const { --reference_count_; }
164 
165  private:
166  mutable int reference_count_;
167  };
168 
169  //#########################################################################
170 
171 
172 //#define IP_DEBUG_SMARTPTR
173 #if COIN_IPOPT_CHECKLEVEL > 2
174 # define IP_DEBUG_SMARTPTR
175 #endif
176 #ifdef IP_DEBUG_SMARTPTR
177 # include "IpDebug.hpp"
178 #endif
179 
318  template <class T>
319  class SmartPtr {
320  public:
327  T* GetRawPtr() const { return ptr_; }
328 
333  bool IsValid() const { return ptr_ != NULL; }
334 
339  bool IsNull() const { return ptr_ == NULL; }
340 
341  private:
345  T* ptr_;
346 
349  if (ptr_) {
350  ptr_->ReleaseRef();
351  if (ptr_->ReferenceCount() == 0) {
352  delete ptr_;
353  }
354  ptr_ = NULL;
355  }
356  }
357 
361  ReleasePointer_(); // Release any old pointer
362  if (rhs != NULL) {
363  rhs->AddRef();
364  ptr_ = rhs;
365  }
366  return *this;
367  }
368 
372  SetFromRawPtr_(rhs.GetRawPtr());
373  return (*this);
374  }
375 
377 
378  public:
379 #define dbg_smartptr_verbosity 0
380 
384  SmartPtr() : ptr_(NULL) {}
385 
387  SmartPtr(const SmartPtr<T>& copy) : ptr_(NULL) {
388  (void) SetFromSmartPtr_(copy);
389  }
390 
392  SmartPtr(T* ptr) : ptr_(NULL) {
393  (void) SetFromRawPtr_(ptr);
394  }
395 
399  ReleasePointer_();
400  }
402 
407  T* operator->() const {
408 #if COIN_COINUTILS_CHECKLEVEL > 0
409  assert(ptr_);
410 #endif
411  return ptr_;
412  }
413 
416  T& operator*() const {
417 #if COIN_IPOPT_CHECKLEVEL > 0
418  assert(ptr_);
419 #endif
420  return *ptr_;
421  }
422 
426  return SetFromRawPtr_(rhs);
427  }
428 
433  return SetFromSmartPtr_(rhs);
434  }
435 
438  template <class U1, class U2>
439  friend
440  bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
441 
444  template <class U1, class U2>
445  friend
446  bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs);
447 
450  template <class U1, class U2>
451  friend
452  bool operator==(U1* lhs, const SmartPtr<U2>& raw_rhs);
453 
456  template <class U1, class U2>
457  friend
458  bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
459 
462  template <class U1, class U2>
463  friend
464  bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs);
465 
468  template <class U1, class U2>
469  friend
470  bool operator!=(U1* lhs, const SmartPtr<U2>& raw_rhs);
472 
473  };
474 
475  template <class U1, class U2>
476  bool ComparePointers(const U1* lhs, const U2* rhs) {
477  if (lhs == rhs) {
478  return true;
479  }
480  // If lhs and rhs point to the same object with different interfaces
481  // U1 and U2, we cannot guarantee that the value of the pointers will
482  // be equivalent. We can guarantee this if we convert to void*.
483  return static_cast<const void*>(lhs) == static_cast<const void*>(rhs);
484  }
485 
486 } // namespace Coin
487 
488 //#############################################################################
489 
493 template <class U1, class U2>
494 bool operator==(const Coin::SmartPtr<U1>& lhs, const Coin::SmartPtr<U2>& rhs) {
495  return Coin::ComparePointers(lhs.GetRawPtr(), rhs.GetRawPtr());
496 }
497 
498 template <class U1, class U2>
499 bool operator==(const Coin::SmartPtr<U1>& lhs, U2* raw_rhs) {
500  return Coin::ComparePointers(lhs.GetRawPtr(), raw_rhs);
501 }
502 
503 template <class U1, class U2>
504 bool operator==(U1* raw_lhs, const Coin::SmartPtr<U2>& rhs) {
505  return Coin::ComparePointers(raw_lhs, rhs.GetRawPtr());
506 }
507 
508 template <class U1, class U2>
509 bool operator!=(const Coin::SmartPtr<U1>& lhs, const Coin::SmartPtr<U2>& rhs) {
510  return ! operator==(lhs, rhs);
511 }
512 
513 template <class U1, class U2>
514 bool operator!=(const Coin::SmartPtr<U1>& lhs, U2* raw_rhs) {
515  return ! operator==(lhs, raw_rhs);
516 }
517 
518 template <class U1, class U2>
519 bool operator!=(U1* raw_lhs, const Coin::SmartPtr<U2>& rhs) {
520  return ! operator==(raw_lhs, rhs);
521 }
523 
524 #define CoinReferencedObject Coin::ReferencedObject
525 #define CoinSmartPtr Coin::SmartPtr
526 #define CoinComparePointers Coin::ComparePointers
527 
528 #endif
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.
typedef void(COINLINKAGE_CB *clp_callback)(Clp_Simplex *model
typedef for user call back.
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Template class for Smart Pointers.
int ReferenceCount() const
SmartPtr()
Default constructor, initialized to NULL.
T * ptr_
Actual raw pointer to the object.
SmartPtr< T > & operator=(const SmartPtr< T > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr...
T * GetRawPtr() const
Returns the raw pointer contained.
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 > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer...
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 > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
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...
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...