Ipopt Documentation  
IpSmartPtr.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2011 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
6 
7 #ifndef __IPSMARTPTR_HPP__
8 #define __IPSMARTPTR_HPP__
9 
10 #include "IpReferenced.hpp"
11 
12 #include "IpDebug.hpp"
13 #if IPOPT_CHECKLEVEL > 2
14 # define IP_DEBUG_SMARTPTR
15 #endif
16 
17 namespace Ipopt
18 {
19 
163 template<class T>
164 class SmartPtr: public Referencer
165 {
166 public:
167 #define ipopt_dbg_smartptr_verbosity 0
168 
170 
173 
176  const SmartPtr<T>& copy
177  );
178 
180  template<class U>
182  const SmartPtr<U>& copy
183  );
184 
187  T* ptr
188  );
189 
196 
198 
202  T* operator->() const;
203 
207  T& operator*() const;
208 
213  T* rhs
214  );
215 
221  const SmartPtr<T>& rhs
222  );
223 
228  template<class U>
229  // cppcheck-suppress operatorEq ; wrong cppcheck suggestion
231  const SmartPtr<U>& rhs);
232 
236  template<class U1, class U2>
237  friend
239  const SmartPtr<U1>& lhs,
240  const SmartPtr<U2>& rhs
241  );
242 
246  template<class U1, class U2>
247  friend
249  const SmartPtr<U1>& lhs,
250  U2* raw_rhs
251  );
252 
256  template<class U1, class U2>
257  friend
259  U1* lhs,
260  const SmartPtr<U2>& raw_rhs
261  );
262 
266  template<class U1, class U2>
267  friend
269  const SmartPtr<U1>& lhs,
270  const SmartPtr<U2>& rhs
271  );
272 
276  template<class U1, class U2>
277  friend
279  const SmartPtr<U1>& lhs,
280  U2* raw_rhs
281  );
282 
286  template<class U1, class U2>
287  friend
289  U1* lhs,
290  const SmartPtr<U2>& raw_rhs
291  );
292 
296  template<class U>
297  friend
298  bool operator<(
299  const SmartPtr<U>& lhs,
300  const SmartPtr<U>& rhs
301  );
303 
305 
317  template<class U>
318  friend U* GetRawPtr(
319  const SmartPtr<U>& smart_ptr
320  );
321 
323  template<class U>
325  const SmartPtr<U>& smart_ptr
326  );
327 
333  template<class U>
334  friend
335  bool IsValid(
336  const SmartPtr<U>& smart_ptr
337  );
338 
344  template<class U>
345  friend
346  bool IsNull(
347  const SmartPtr<U>& smart_ptr
348  );
350 
351 private:
353 
355  T* ptr_;
356 
362  T* rhs
363  );
364 
370  const SmartPtr<T>& rhs
371  );
372 
376 };
377 
379 template<class U>
381 U* GetRawPtr(
382  const SmartPtr<U>& smart_ptr
383 );
384 
385 template<class U>
387  const SmartPtr<U>& smart_ptr
388 );
389 
390 template<class U>
391 bool IsNull(
392  const SmartPtr<U>& smart_ptr
393 );
394 
395 template<class U>
396 bool IsValid(
397  const SmartPtr<U>& smart_ptr
398 );
399 
400 template<class U1, class U2>
401 bool operator==(
402  const SmartPtr<U1>& lhs,
403  const SmartPtr<U2>& rhs
404 );
405 
406 template<class U1, class U2>
407 bool operator==(
408  const SmartPtr<U1>& lhs,
409  U2* raw_rhs
410 );
411 
412 template<class U1, class U2>
413 bool operator==(
414  U1* lhs,
415  const SmartPtr<U2>& raw_rhs
416 );
417 
418 template<class U1, class U2>
419 bool operator!=(
420  const SmartPtr<U1>& lhs,
421  const SmartPtr<U2>& rhs);
422 
423 template<class U1, class U2>
424 bool operator!=(
425  const SmartPtr<U1>& lhs,
426  U2* raw_rhs
427 );
428 
429 template<class U1, class U2>
430 bool operator!=(
431  U1* lhs,
432  const SmartPtr<U2>& raw_rhs
433 );
434 
436 
437 template<class T>
439  : ptr_(0)
440 {
441 #ifdef IP_DEBUG_SMARTPTR
442  DBG_START_METH("SmartPtr<T>::SmartPtr()", ipopt_dbg_smartptr_verbosity);
443 #endif
444 
445 #ifndef NDEBUG
446  // cppcheck-suppress unreadVariable
447  const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
448 #endif
449 }
450 
451 template<class T>
453  const SmartPtr<T>& copy
454 )
455  : ptr_(0)
456 {
457 #ifdef IP_DEBUG_SMARTPTR
458  DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<T>& copy)", ipopt_dbg_smartptr_verbosity);
459 #endif
460 
461 #ifndef NDEBUG
462  // cppcheck-suppress unreadVariable
463  const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ =
464  ptr_;
465 #endif
466 
467  (void) SetFromSmartPtr_(copy);
468 }
469 
470 template<class T>
471 template<class U>
473  const SmartPtr<U>& copy
474 )
475  : ptr_(0)
476 {
477 #ifdef IP_DEBUG_SMARTPTR
478  DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<U>& copy)", ipopt_dbg_smartptr_verbosity);
479 #endif
480 
481 #ifndef NDEBUG
482  // cppcheck-suppress unreadVariable
483  const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ =
484  ptr_;
485 #endif
486 
487  (void) SetFromSmartPtr_(GetRawPtr(copy));
488 }
489 
490 template<class T>
492  T* ptr
493 )
494  : ptr_(0)
495 {
496 #ifdef IP_DEBUG_SMARTPTR
497  DBG_START_METH("SmartPtr<T>::SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
498 #endif
499 
500 #ifndef NDEBUG
501  // cppcheck-suppress unreadVariable
502  const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ =
503  ptr_;
504 #endif
505 
506  (void) SetFromRawPtr_(ptr);
507 }
508 
509 template<class T>
511 {
512 #ifdef IP_DEBUG_SMARTPTR
513  DBG_START_METH("SmartPtr<T>::~SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
514 #endif
515 
516  ReleasePointer_();
517 }
518 
519 template<class T>
521 {
522 #ifdef IP_DEBUG_SMARTPTR
523  DBG_START_METH("T* SmartPtr<T>::operator->()", ipopt_dbg_smartptr_verbosity);
524 #endif
525 
526  // cannot deref a null pointer
527 #if IPOPT_CHECKLEVEL > 0
528  assert(ptr_);
529 #endif
530 
531  return ptr_;
532 }
533 
534 template<class T>
536 {
537 #ifdef IP_DEBUG_SMARTPTR
538  DBG_START_METH("T& SmartPtr<T>::operator*()", ipopt_dbg_smartptr_verbosity);
539 #endif
540 
541  // cannot dereference a null pointer
542 #if IPOPT_CHECKLEVEL > 0
543  assert(ptr_);
544 #endif
545 
546  return *ptr_;
547 }
548 
549 template<class T>
551  T* rhs
552 )
553 {
554 #ifdef IP_DEBUG_SMARTPTR
555  DBG_START_METH("SmartPtr<T>& SmartPtr<T>::operator=(T* rhs)", ipopt_dbg_smartptr_verbosity);
556 #endif
557 
558  return SetFromRawPtr_(rhs);
559 }
560 
561 template<class T>
563  const SmartPtr<T>& rhs
564 )
565 {
566 #ifdef IP_DEBUG_SMARTPTR
568  "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)",
570 #endif
571 
572  return SetFromSmartPtr_(rhs);
573 }
574 
575 template<class T>
576 template<class U>
578  const SmartPtr<U>& rhs
579 )
580 {
581 #ifdef IP_DEBUG_SMARTPTR
583  "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)",
585 #endif
586 
587  return SetFromSmartPtr_(GetRawPtr(rhs));
588 }
589 
590 template<class T>
592  T* rhs
593 )
594 {
595 #ifdef IP_DEBUG_SMARTPTR
597  "SmartPtr<T>& SmartPtr<T>::SetFromRawPtr_(T* rhs)", ipopt_dbg_smartptr_verbosity);
598 #endif
599 
600  if( rhs != 0 )
601  {
602  rhs->AddRef(this);
603  }
604 
605  // Release any old pointer
606  ReleasePointer_();
607 
608  ptr_ = rhs;
609 
610  return *this;
611 }
612 
613 template<class T>
615  const SmartPtr<T>& rhs
616 )
617 {
618 #ifdef IP_DEBUG_SMARTPTR
620  "SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)",
622 #endif
623 
624  SetFromRawPtr_(GetRawPtr(rhs));
625 
626  return (*this);
627 }
628 
629 template<class T>
631 {
632 #ifdef IP_DEBUG_SMARTPTR
634  "void SmartPtr<T>::ReleasePointer()",
636 #endif
637 
638  if( ptr_ )
639  {
640  ptr_->ReleaseRef(this);
641  if( ptr_->ReferenceCount() == 0 )
642  {
643  delete ptr_;
644  }
645  }
646 }
647 
648 template<class U>
650  const SmartPtr<U>& smart_ptr
651 )
652 {
653 #ifdef IP_DEBUG_SMARTPTR
655  "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
656  0);
657 #endif
658 
659  return smart_ptr.ptr_;
660 }
661 
662 template<class U>
664  const SmartPtr<U>& smart_ptr
665 )
666 {
667  // compiler should implicitly cast
668  return GetRawPtr(smart_ptr);
669 }
670 
671 template<class U>
672 bool IsValid(
673  const SmartPtr<U>& smart_ptr
674 )
675 {
676  return !IsNull(smart_ptr);
677 }
678 
679 template<class U>
680 bool IsNull(
681  const SmartPtr<U>& smart_ptr
682 )
683 {
684 #ifdef IP_DEBUG_SMARTPTR
686  "bool IsNull(const SmartPtr<T>& smart_ptr)",
687  0);
688 #endif
689 
690  return (smart_ptr.ptr_ == 0);
691 }
692 
693 template<class U1, class U2>
695  const U1* lhs,
696  const U2* rhs
697 )
698 {
699 #ifdef IP_DEBUG_SMARTPTR
701  "bool ComparePtrs(const U1* lhs, const U2* rhs)",
703 #endif
704 
705  // Even if lhs and rhs point to the same object
706  // with different interfaces U1 and U2, we cannot guarantee that
707  // the value of the pointers will be equivalent. We can
708  // guarantee this if we convert to ReferencedObject* (see also #162)
709  const ReferencedObject* v_lhs = lhs;
710  const ReferencedObject* v_rhs = rhs;
711 
712  return v_lhs == v_rhs;
713 }
714 
715 template<class U1, class U2>
717  const SmartPtr<U1>& lhs,
718  const SmartPtr<U2>& rhs
719 )
720 {
721 #ifdef IP_DEBUG_SMARTPTR
723  "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
725 #endif
726 
727  U1* raw_lhs = GetRawPtr(lhs);
728  U2* raw_rhs = GetRawPtr(rhs);
729  return ComparePointers(raw_lhs, raw_rhs);
730 }
731 
732 template<class U1, class U2>
734  const SmartPtr<U1>& lhs,
735  U2* raw_rhs
736 )
737 {
738 #ifdef IP_DEBUG_SMARTPTR
740  "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
742 #endif
743 
744  U1* raw_lhs = GetRawPtr(lhs);
745  return ComparePointers(raw_lhs, raw_rhs);
746 }
747 
748 template<class U1, class U2>
750  U1* raw_lhs,
751  const SmartPtr<U2>& rhs
752 )
753 {
754 #ifdef IP_DEBUG_SMARTPTR
756  "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
758 #endif
759 
760  const U2* raw_rhs = GetRawPtr(rhs);
761  return ComparePointers(raw_lhs, raw_rhs);
762 }
763 
764 template<class U1, class U2>
766  const SmartPtr<U1>& lhs,
767  const SmartPtr<U2>& rhs
768 )
769 {
770 #ifdef IP_DEBUG_SMARTPTR
772  "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
774 #endif
775 
776  bool retValue = operator==(lhs, rhs);
777  return !retValue;
778 }
779 
780 template<class U1, class U2>
782  const SmartPtr<U1>& lhs,
783  U2* raw_rhs
784 )
785 {
786 #ifdef IP_DEBUG_SMARTPTR
788  "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
790 #endif
791 
792  bool retValue = operator==(lhs, raw_rhs);
793  return !retValue;
794 }
795 
796 template<class U1, class U2>
798  U1* raw_lhs,
799  const SmartPtr<U2>& rhs
800 )
801 {
802 #ifdef IP_DEBUG_SMARTPTR
804  "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
806 #endif
807 
808  bool retValue = operator==(raw_lhs, rhs);
809  return !retValue;
810 }
811 
812 template<class T>
813 void swap(
814  SmartPtr<T>& a,
815  SmartPtr<T>& b
816 )
817 {
818 #ifdef IP_DEBUG_REFERENCED
819  SmartPtr<T> tmp(a);
820  a = b;
821  b = tmp;
822 #else
823  std::swap(a.prt_, b.ptr_);
824 #endif
825 }
826 
827 template<class T>
829  const SmartPtr<T>& lhs,
830  const SmartPtr<T>& rhs
831 )
832 {
833  return lhs.ptr_ < rhs.ptr_;
834 }
835 
836 template<class T>
838  const SmartPtr<T>& lhs,
839  const SmartPtr<T>& rhs
840 )
841 {
842  return rhs < lhs;
843 }
844 
845 template<class T> bool operator<=(
846  const SmartPtr<T>& lhs,
847  const SmartPtr<T>& rhs
848 )
849 {
850  return !(rhs < lhs);
851 }
852 
853 template<class T> bool operator>=(
854  const SmartPtr<T>& lhs,
855  const SmartPtr<T>& rhs
856 )
857 {
858  return !(lhs < rhs);
859 }
860 
861 } // namespace Ipopt
862 
863 #undef ipopt_dbg_smartptr_verbosity
864 
865 #endif
Ipopt::SmartPtr::ReleasePointer_
void ReleasePointer_()
Release the currently referenced object.
Definition: IpSmartPtr.hpp:630
Ipopt::SmartPtr::SmartPtr
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
Definition: IpSmartPtr.hpp:491
Ipopt::SmartPtr::ConstPtr
friend SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Returns a const pointer.
Definition: IpSmartPtr.hpp:663
DBG_START_FUN
#define DBG_START_FUN(__func_name, __verbose_level)
Definition: IpDebug.hpp:37
Ipopt::operator>
bool operator>(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:837
Ipopt::SmartPtr::operator=
SmartPtr< T > & operator=(const SmartPtr< U > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr of...
Definition: IpSmartPtr.hpp:577
Ipopt::SmartPtr::SmartPtr
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy of type T.
Definition: IpSmartPtr.hpp:452
Ipopt::SmartPtr::operator!=
friend bool operator!=(const SmartPtr< U1 > &lhs, U2 *raw_rhs)
Overloaded in-equality comparison operator, allows the user to compare the value of a SmartPtr with a...
Definition: IpSmartPtr.hpp:781
Ipopt::SmartPtr::~SmartPtr
~SmartPtr()
Destructor, automatically decrements the reference count and deletes the object if necessary.
Definition: IpSmartPtr.hpp:510
Ipopt::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: IpSmartPtr.hpp:550
Ipopt::SmartPtr::operator<
friend bool operator<(const SmartPtr< U > &lhs, const SmartPtr< U > &rhs)
Overloaded less-than comparison operator, allows the user to compare the value of two SmartPtrs.
Ipopt
This file contains a base class for all exceptions and a set of macros to help with exceptions.
Definition: IpInexactAlgBuilder.hpp:14
Ipopt::Referencer
Pseudo-class, from which everything has to inherit that wants to use be registered as a Referencer fo...
Definition: IpReferenced.hpp:25
ipopt_dbg_smartptr_verbosity
#define ipopt_dbg_smartptr_verbosity
Definition: IpSmartPtr.hpp:167
Ipopt::SmartPtr::SetFromRawPtr_
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
Definition: IpSmartPtr.hpp:591
Ipopt::SmartPtr::ptr_
T * ptr_
Actual raw pointer to the object.
Definition: IpSmartPtr.hpp:355
Ipopt::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: IpSmartPtr.hpp:765
IPOPT_UNUSED
#define IPOPT_UNUSED
Definition: IpTypes.h:32
Ipopt::GetRawPtr
U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:649
DBG_START_METH
#define DBG_START_METH(__func_name, __verbose_level)
Definition: IpDebug.hpp:38
Ipopt::swap
void swap(SmartPtr< T > &a, SmartPtr< T > &b)
Definition: IpSmartPtr.hpp:813
Ipopt::SmartPtr
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:165
Ipopt::operator!=
bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:765
Ipopt::SmartPtr::IsNull
friend bool IsNull(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NULL.
Definition: IpSmartPtr.hpp:680
Ipopt::SmartPtr::GetRawPtr
friend U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
Definition: IpSmartPtr.hpp:649
Ipopt::SmartPtr::SmartPtr
SmartPtr()
Default constructor, initialized to NULL.
Definition: IpSmartPtr.hpp:438
Ipopt::SmartPtr::SmartPtr
SmartPtr(const SmartPtr< U > &copy)
Copy constructor, initialized from copy of type U.
Definition: IpSmartPtr.hpp:472
IpReferenced.hpp
Ipopt::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: IpSmartPtr.hpp:716
Ipopt::operator>=
bool operator>=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:853
Ipopt::ConstPtr
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:663
Ipopt::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: IpSmartPtr.hpp:614
Ipopt::ComparePointers
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: IpSmartPtr.hpp:694
Ipopt::SmartPtr::IsValid
friend bool IsValid(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NOT NULL.
Definition: IpSmartPtr.hpp:672
Ipopt::SmartPtr::operator==
friend bool operator==(const SmartPtr< U1 > &lhs, U2 *raw_rhs)
Overloaded equality comparison operator, allows the user to compare the value of a SmartPtr with a ra...
Definition: IpSmartPtr.hpp:733
Ipopt::SmartPtr::operator*
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: IpSmartPtr.hpp:535
Ipopt::IsValid
bool IsValid(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:672
Ipopt::SmartPtr::operator->
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: IpSmartPtr.hpp:520
Ipopt::IsNull
bool IsNull(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:680
Ipopt::SmartPtr::operator==
friend bool operator==(U1 *lhs, const SmartPtr< U2 > &raw_rhs)
Overloaded equality comparison operator, allows the user to compare the value of a raw pointer with a...
Definition: IpSmartPtr.hpp:749
Ipopt::operator<=
bool operator<=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:845
Ipopt::operator<
bool operator<(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:828
IpDebug.hpp
Ipopt::ReferencedObject
Storing the reference count of all the smart pointers that currently reference it.
Definition: IpReferenced.hpp:170
Ipopt::SmartPtr::operator!=
friend bool operator!=(U1 *lhs, const SmartPtr< U2 > &raw_rhs)
Overloaded in-equality comparison operator, allows the user to compare the value of a SmartPtr with a...
Definition: IpSmartPtr.hpp:797
Ipopt::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: IpSmartPtr.hpp:562
Ipopt::operator==
bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:716