Clp 1.17.6

- 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 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
157 class ReferencedObject {
158 public:
159 ReferencedObject()
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
350 void ReleasePointer_()
351 {
352 if (ptr_) {
353 ptr_->ReleaseRef();
354 if (ptr_->ReferenceCount() == 0) {
355 delete ptr_;
356 }
357 ptr_ = NULL;
358 }
359 }
360
363 SmartPtr< T > &SetFromRawPtr_(T *rhs)
364 {
365 ReleasePointer_(); // Release any old pointer
366 if (rhs != NULL) {
367 rhs->AddRef();
368 ptr_ = rhs;
369 }
370 return *this;
371 }
372
375 inline SmartPtr< T > &SetFromSmartPtr_(const SmartPtr< T > &rhs)
376 {
377 SetFromRawPtr_(rhs.GetRawPtr());
378 return (*this);
379 }
380
382
383 public:
384 #define dbg_smartptr_verbosity 0
385
389 SmartPtr()
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
410 ~SmartPtr()
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
440 SmartPtr< T > &operator=(T *rhs)
441 {
442 return SetFromRawPtr_(rhs);
443 }
444
448 SmartPtr< T > &operator=(const SmartPtr< T > &rhs)
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 >
505 bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
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 >
523 bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
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 */
Coin::SmartPtr::ReleasePointer_
void ReleasePointer_()
Release the currently referenced object.
Definition: CoinSmartPtr.hpp:350
operator!=
bool operator!=(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
Definition: CoinSmartPtr.hpp:523
Coin::SmartPtr::~SmartPtr
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
Definition: CoinSmartPtr.hpp:410
Coin::ComparePointers
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: CoinSmartPtr.hpp:486
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:363
Coin::SmartPtr
Template class for Smart Pointers.
Definition: CoinSmartPtr.hpp:321
Coin::ReferencedObject::ReferenceCount
int ReferenceCount() const
Definition: CoinSmartPtr.hpp:164
Coin::SmartPtr::SmartPtr
SmartPtr()
Default constructor, initialized to NULL.
Definition: CoinSmartPtr.hpp:389
Coin::SmartPtr::ptr_
T * ptr_
Actual raw pointer to the object.
Definition: CoinSmartPtr.hpp:347
Coin::SmartPtr::GetRawPtr
T * GetRawPtr() const
Returns the raw pointer contained.
Definition: CoinSmartPtr.hpp:329
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:440
operator==
bool operator==(const Coin::SmartPtr< U1 > &lhs, const Coin::SmartPtr< U2 > &rhs)
Definition: CoinSmartPtr.hpp:505
Coin::SmartPtr::SmartPtr
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy.
Definition: CoinSmartPtr.hpp:395
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:166
Coin::SmartPtr::IsValid
bool IsValid() const
Returns true if the SmartPtr is NOT NULL.
Definition: CoinSmartPtr.hpp:335
Coin::ReferencedObject::reference_count_
int reference_count_
Definition: CoinSmartPtr.hpp:169
Coin::ReferencedObject::AddRef
void AddRef() const
Definition: CoinSmartPtr.hpp:165
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:375
Coin::SmartPtr::operator->
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: CoinSmartPtr.hpp:420
Coin::SmartPtr::SmartPtr
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
Definition: CoinSmartPtr.hpp:402
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:505
Coin::SmartPtr::operator*
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: CoinSmartPtr.hpp:430
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:448
Coin::SmartPtr::IsNull
bool IsNull() const
Returns true if the SmartPtr is NULL.
Definition: CoinSmartPtr.hpp:341
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:523
Coin::ReferencedObject::~ReferencedObject
virtual ~ReferencedObject()
Definition: CoinSmartPtr.hpp:163