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 ptr_ = 0;
645 }
646 }
647}
648
649template<class U>
652)
653{
654#ifdef IP_DEBUG_SMARTPTR
656 "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
657 0);
658#endif
659
660 return smart_ptr.ptr_;
661}
662
663template<class U>
666)
667{
668 // compiler should implicitly cast
669 return GetRawPtr(smart_ptr);
670}
671
672template<class U>
675)
676{
677 return !IsNull(smart_ptr);
678}
679
680template<class U>
683)
684{
685#ifdef IP_DEBUG_SMARTPTR
687 "bool IsNull(const SmartPtr<T>& smart_ptr)",
688 0);
689#endif
690
691 return (smart_ptr.ptr_ == 0);
692}
693
694template<class U1, class U2>
696 const U1* lhs,
697 const U2* rhs
698)
699{
700#ifdef IP_DEBUG_SMARTPTR
702 "bool ComparePtrs(const U1* lhs, const U2* rhs)",
704#endif
705
706 // Even if lhs and rhs point to the same object
707 // with different interfaces U1 and U2, we cannot guarantee that
708 // the value of the pointers will be equivalent. We can
709 // guarantee this if we convert to ReferencedObject* (see also #162)
710 const ReferencedObject* v_lhs = lhs;
711 const ReferencedObject* v_rhs = rhs;
712
713 return v_lhs == v_rhs;
714}
715
716template<class U1, class U2>
718 const SmartPtr<U1>& lhs,
719 const SmartPtr<U2>& rhs
720)
721{
722#ifdef IP_DEBUG_SMARTPTR
724 "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
726#endif
727
731}
732
733template<class U1, class U2>
735 const SmartPtr<U1>& lhs,
736 U2* raw_rhs
737)
738{
739#ifdef IP_DEBUG_SMARTPTR
741 "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
743#endif
744
747}
748
749template<class U1, class U2>
751 U1* raw_lhs,
752 const SmartPtr<U2>& rhs
753)
754{
755#ifdef IP_DEBUG_SMARTPTR
757 "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
759#endif
760
761 const U2* raw_rhs = GetRawPtr(rhs);
763}
764
765template<class U1, class U2>
767 const SmartPtr<U1>& lhs,
768 const SmartPtr<U2>& rhs
769)
770{
771#ifdef IP_DEBUG_SMARTPTR
773 "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
775#endif
776
777 bool retValue = operator==(lhs, rhs);
778 return !retValue;
779}
780
781template<class U1, class U2>
783 const SmartPtr<U1>& lhs,
784 U2* raw_rhs
785)
786{
787#ifdef IP_DEBUG_SMARTPTR
789 "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
791#endif
792
794 return !retValue;
795}
796
797template<class U1, class U2>
799 U1* raw_lhs,
800 const SmartPtr<U2>& rhs
801)
802{
803#ifdef IP_DEBUG_SMARTPTR
805 "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
807#endif
808
810 return !retValue;
811}
812
813template<class T>
814void swap(
815 SmartPtr<T>& a,
817)
818{
819#ifdef IP_DEBUG_REFERENCED
821 a = b;
822 b = tmp;
823#else
824 std::swap(a.prt_, b.ptr_);
825#endif
826}
827
828template<class T>
830 const SmartPtr<T>& lhs,
831 const SmartPtr<T>& rhs
832)
833{
834 return lhs.ptr_ < rhs.ptr_;
835}
836
837template<class T>
839 const SmartPtr<T>& lhs,
840 const SmartPtr<T>& rhs
841)
842{
843 return rhs < lhs;
844}
845
846template<class T> bool operator<=(
847 const SmartPtr<T>& lhs,
848 const SmartPtr<T>& rhs
849)
850{
851 return !(rhs < lhs);
852}
853
854template<class T> bool operator>=(
855 const SmartPtr<T>& lhs,
856 const SmartPtr<T>& rhs
857)
858{
859 return !(lhs < rhs);
860}
861
862} // namespace Ipopt
863
864#undef ipopt_dbg_smartptr_verbosity
865
866#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)