Ipopt Documentation  
 
Loading...
Searching...
No Matches
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
17namespace Ipopt
18{
19
163template<class T>
164class SmartPtr: public Referencer
165{
166public:
167#define ipopt_dbg_smartptr_verbosity 0
168
171
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
199
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
299 const SmartPtr<U>& lhs,
300 const SmartPtr<U>& rhs
301 );
303
306
317 template<class U>
318 friend U* GetRawPtr(
320 );
321
323 template<class U>
326 );
327
333 template<class U>
334 friend
337 );
338
344 template<class U>
345 friend
346 bool IsNull(
348 );
350
351private:
354
356
362 T* rhs
363 );
364
370 const SmartPtr<T>& rhs
371 );
372
376};
377
380template<class U>
381U* GetRawPtr(
383);
384
385template<class U>
388);
389
390template<class U>
391bool IsNull(
393);
394
395template<class U>
396bool IsValid(
398);
399
400template<class U1, class U2>
401bool operator==(
402 const SmartPtr<U1>& lhs,
403 const SmartPtr<U2>& rhs
404);
405
406template<class U1, class U2>
407bool operator==(
408 const SmartPtr<U1>& lhs,
409 U2* raw_rhs
410);
411
412template<class U1, class U2>
413bool operator==(
414 U1* lhs,
415 const SmartPtr<U2>& raw_rhs
416);
417
418template<class U1, class U2>
419bool operator!=(
420 const SmartPtr<U1>& lhs,
421 const SmartPtr<U2>& rhs);
422
423template<class U1, class U2>
424bool operator!=(
425 const SmartPtr<U1>& lhs,
426 U2* raw_rhs
427);
428
429template<class U1, class U2>
430bool operator!=(
431 U1* lhs,
432 const SmartPtr<U2>& raw_rhs
433);
434
436
437template<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
448#endif
449}
450
451template<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
464 ptr_;
465#endif
466
468}
469
470template<class T>
471template<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
484 ptr_;
485#endif
486
488}
489
490template<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
503 ptr_;
504#endif
505
507}
508
509template<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
519template<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
534template<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
549template<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
561template<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
575template<class T>
576template<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
590template<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
613template<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
629template<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
648template<class U>
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
662template<class U>
665)
666{
667 // compiler should implicitly cast
668 return GetRawPtr(smart_ptr);
669}
670
671template<class U>
674)
675{
676 return !IsNull(smart_ptr);
677}
678
679template<class U>
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
693template<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
715template<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
730}
731
732template<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
746}
747
748template<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);
762}
763
764template<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
780template<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
793 return !retValue;
794}
795
796template<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
809 return !retValue;
810}
811
812template<class T>
813void swap(
814 SmartPtr<T>& a,
816)
817{
818#ifdef IP_DEBUG_REFERENCED
820 a = b;
821 b = tmp;
822#else
823 std::swap(a.prt_, b.ptr_);
824#endif
825}
826
827template<class T>
829 const SmartPtr<T>& lhs,
830 const SmartPtr<T>& rhs
831)
832{
833 return lhs.ptr_ < rhs.ptr_;
834}
835
836template<class T>
838 const SmartPtr<T>& lhs,
839 const SmartPtr<T>& rhs
840)
841{
842 return rhs < lhs;
843}
844
845template<class T> bool operator<=(
846 const SmartPtr<T>& lhs,
847 const SmartPtr<T>& rhs
848)
849{
850 return !(rhs < lhs);
851}
852
853template<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
#define DBG_START_FUN(__func_name, __verbose_level)
Definition IpDebug.hpp:37
#define DBG_START_METH(__func_name, __verbose_level)
Definition IpDebug.hpp:38
#define ipopt_dbg_smartptr_verbosity
#define IPOPT_UNUSED
Definition IpTypes.h:32
Templated class which stores one entry for the CachedResult class.
DependentResult()
Default Constructor.
Storing the reference count of all the smart pointers that currently reference it.
Pseudo-class, from which everything has to inherit that wants to use be registered as a Referencer fo...
Template class for Smart Pointers.
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
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...
SmartPtr< T > & SetFromSmartPtr_(const SmartPtr< T > &rhs)
Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object...
SmartPtr()
Default constructor, initialized to NULL.
SmartPtr(const SmartPtr< U > &copy)
Copy constructor, initialized from copy of type U.
friend U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
friend SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Returns a const pointer.
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...
friend bool IsNull(const SmartPtr< U > &smart_ptr)
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.
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.
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.
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...
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...
SmartPtr(T *ptr)
Constructor, initialized from T* ptr.
SmartPtr< T > & operator=(const SmartPtr< T > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr.
~SmartPtr()
Destructor, automatically decrements the reference count and 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.
friend bool IsValid(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NOT NULL.
void ReleasePointer_()
Release the currently referenced object.
SmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
SmartPtr< T > & operator=(const SmartPtr< U > &rhs)
Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr of...
SmartPtr(const SmartPtr< T > &copy)
Copy constructor, initialized from copy of type T.
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
T * ptr_
Actual raw pointer to the object.
This file contains a base class for all exceptions and a set of macros to help with exceptions.
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
bool IsValid(const SmartPtr< U > &smart_ptr)
U * GetRawPtr(const SmartPtr< U > &smart_ptr)
bool ComparePointers(const U1 *lhs, const U2 *rhs)
bool operator<=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
void swap(SmartPtr< T > &a, SmartPtr< T > &b)
bool operator>(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
bool IsNull(const SmartPtr< U > &smart_ptr)
bool operator>=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
bool operator<(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)