CoinUtils 2.10.15

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
157 class ReferencedObject {
158 public:
159 ReferencedObject() : reference_count_(0) {}
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
348 void ReleasePointer_() {
349 if (ptr_) {
350 ptr_->ReleaseRef();
351 if (ptr_->ReferenceCount() == 0) {
352 delete ptr_;
353 }
354 ptr_ = NULL;
355 }
356 }
357
360 SmartPtr<T>& SetFromRawPtr_(T* rhs){
361 ReleasePointer_(); // Release any old pointer
362 if (rhs != NULL) {
363 rhs->AddRef();
364 ptr_ = rhs;
365 }
366 return *this;
367 }
368
371 inline SmartPtr<T>& SetFromSmartPtr_(const SmartPtr<T>& rhs) {
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
398 ~SmartPtr() {
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
425 SmartPtr<T>& operator=(T* rhs) {
426 return SetFromRawPtr_(rhs);
427 }
428
432 SmartPtr<T>& operator=(const SmartPtr<T>& rhs) {
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
Coin::SmartPtr::ReleasePointer_
void ReleasePointer_()
Release the currently referenced object.
Definition: CoinSmartPtr.hpp:348
operator!=
bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
Definition: CoinSmartPtr.hpp:509
Coin::SmartPtr::~SmartPtr
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
Definition: CoinSmartPtr.hpp:398
void
typedef void(COINLINKAGE_CB *clp_callback)(Clp_Simplex *model
typedef for user call back.
Coin::ComparePointers
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: CoinSmartPtr.hpp:476
Coin::SmartPtr
Template class for Smart Pointers.
Definition: CoinSmartPtr.hpp:319
Coin::ReferencedObject::ReferenceCount
int ReferenceCount() const
Definition: CoinSmartPtr.hpp:161
Coin::SmartPtr::SmartPtr
SmartPtr()
Default constructor, initialized to NULL.
Definition: CoinSmartPtr.hpp:384
Coin::SmartPtr::ptr_
T * ptr_
Actual raw pointer to the object.
Definition: CoinSmartPtr.hpp:345
Coin::SmartPtr::operator=
SmartPtr< T > & operator=(const SmartPtr< T > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr...
Definition: CoinSmartPtr.hpp:432
Coin::SmartPtr::GetRawPtr
T * GetRawPtr() const
Returns the raw pointer contained.
Definition: CoinSmartPtr.hpp:327
operator==
bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
Definition: CoinSmartPtr.hpp:494
Coin::SmartPtr::SmartPtr
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy.
Definition: CoinSmartPtr.hpp:387
Coin::ReferencedObject
ReferencedObject class.
Definition: CoinSmartPtr.hpp:157
Coin::ReferencedObject::ReferencedObject
ReferencedObject()
Definition: CoinSmartPtr.hpp:159
Coin::ReferencedObject::ReleaseRef
void ReleaseRef() const
Definition: CoinSmartPtr.hpp:163
Coin::SmartPtr::IsValid
bool IsValid() const
Returns true if the SmartPtr is NOT NULL.
Definition: CoinSmartPtr.hpp:333
Coin::SmartPtr::operator=
SmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer...
Definition: CoinSmartPtr.hpp:425
Coin::ReferencedObject::reference_count_
int reference_count_
Definition: CoinSmartPtr.hpp:166
Coin::ReferencedObject::AddRef
void AddRef() const
Definition: CoinSmartPtr.hpp:162
Coin::SmartPtr::operator->
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: CoinSmartPtr.hpp:407
Coin::SmartPtr::SmartPtr
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
Definition: CoinSmartPtr.hpp:392
Coin::SmartPtr::operator==
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...
Definition: CoinSmartPtr.hpp:494
Coin::SmartPtr::operator*
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: CoinSmartPtr.hpp:416
Coin::SmartPtr::SetFromRawPtr_
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
Definition: CoinSmartPtr.hpp:360
Coin::SmartPtr::IsNull
bool IsNull() const
Returns true if the SmartPtr is NULL.
Definition: CoinSmartPtr.hpp:339
Coin::SmartPtr::operator!=
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...
Definition: CoinSmartPtr.hpp:509
Coin::SmartPtr::SetFromSmartPtr_
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...
Definition: CoinSmartPtr.hpp:371
Coin::ReferencedObject::~ReferencedObject
virtual ~ReferencedObject()
Definition: CoinSmartPtr.hpp:160