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#ifdef HAVE_CSTDDEF
11# include <cstddef>
12#else
13# ifdef HAVE_STDDEF_H
14# include <stddef.h>
15# else
16# error "don't have header file for stddef"
17# endif
18#endif
19
20#include "IpReferenced.hpp"
21
22#include "IpDebug.hpp"
23#if IPOPT_CHECKLEVEL > 2
24# define IP_DEBUG_SMARTPTR
25#endif
26
27namespace Ipopt
28{
29
173template<class T>
174class SmartPtr: public Referencer
175{
176public:
177#define ipopt_dbg_smartptr_verbosity 0
178
181
183
186 const SmartPtr<T>& copy
187 );
188
190 template<class U>
192 const SmartPtr<U>& copy
193 );
194
197 T* ptr
198 );
199
206
209
212 T* operator->() const;
213
217 T& operator*() const;
218
223 T* rhs
224 );
225
231 const SmartPtr<T>& rhs
232 );
233
238 template<class U>
239 // cppcheck-suppress operatorEq ; wrong cppcheck suggestion
241 const SmartPtr<U>& rhs);
242
246 template<class U1, class U2>
247 friend
249 const SmartPtr<U1>& lhs,
250 const SmartPtr<U2>& rhs
251 );
252
256 template<class U1, class U2>
257 friend
259 const SmartPtr<U1>& lhs,
260 U2* raw_rhs
261 );
262
266 template<class U1, class U2>
267 friend
269 U1* lhs,
270 const SmartPtr<U2>& raw_rhs
271 );
272
276 template<class U1, class U2>
277 friend
279 const SmartPtr<U1>& lhs,
280 const SmartPtr<U2>& rhs
281 );
282
286 template<class U1, class U2>
287 friend
289 const SmartPtr<U1>& lhs,
290 U2* raw_rhs
291 );
292
296 template<class U1, class U2>
297 friend
299 U1* lhs,
300 const SmartPtr<U2>& raw_rhs
301 );
302
306 template<class U>
307 friend
309 const SmartPtr<U>& lhs,
310 const SmartPtr<U>& rhs
311 );
313
316
327 template<class U>
328 friend U* GetRawPtr(
330 );
331
333 template<class U>
336 );
337
343 template<class U>
344 friend
347 );
348
354 template<class U>
355 friend
356 bool IsNull(
358 );
360
361private:
364
366
372 T* rhs
373 );
374
380 const SmartPtr<T>& rhs
381 );
382
386};
387
390template<class U>
391U* GetRawPtr(
393);
394
395template<class U>
398);
399
400template<class U>
401bool IsNull(
403);
404
405template<class U>
406bool IsValid(
408);
409
410template<class U1, class U2>
411bool operator==(
412 const SmartPtr<U1>& lhs,
413 const SmartPtr<U2>& rhs
414);
415
416template<class U1, class U2>
417bool operator==(
418 const SmartPtr<U1>& lhs,
419 U2* raw_rhs
420);
421
422template<class U1, class U2>
423bool operator==(
424 U1* lhs,
425 const SmartPtr<U2>& raw_rhs
426);
427
428template<class U1, class U2>
429bool operator!=(
430 const SmartPtr<U1>& lhs,
431 const SmartPtr<U2>& rhs);
432
433template<class U1, class U2>
434bool operator!=(
435 const SmartPtr<U1>& lhs,
436 U2* raw_rhs
437);
438
439template<class U1, class U2>
440bool operator!=(
441 U1* lhs,
442 const SmartPtr<U2>& raw_rhs
443);
444
446
447template<class T>
449 : ptr_(0)
450{
451#ifdef IP_DEBUG_SMARTPTR
452 DBG_START_METH("SmartPtr<T>::SmartPtr()", ipopt_dbg_smartptr_verbosity);
453#endif
454
455#ifndef NDEBUG
456 // cppcheck-suppress unreadVariable
458#endif
459}
460
461template<class T>
463 const SmartPtr<T>& copy
464)
465 : ptr_(0)
466{
467#ifdef IP_DEBUG_SMARTPTR
468 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<T>& copy)", ipopt_dbg_smartptr_verbosity);
469#endif
470
471#ifndef NDEBUG
472 // cppcheck-suppress unreadVariable
474 ptr_;
475#endif
476
478}
479
480template<class T>
481template<class U>
483 const SmartPtr<U>& copy
484)
485 : ptr_(0)
486{
487#ifdef IP_DEBUG_SMARTPTR
488 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<U>& copy)", ipopt_dbg_smartptr_verbosity);
489#endif
490
491#ifndef NDEBUG
492 // cppcheck-suppress unreadVariable
494 ptr_;
495#endif
496
498}
499
500template<class T>
502 T* ptr
503)
504 : ptr_(0)
505{
506#ifdef IP_DEBUG_SMARTPTR
507 DBG_START_METH("SmartPtr<T>::SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
508#endif
509
510#ifndef NDEBUG
511 // cppcheck-suppress unreadVariable
513 ptr_;
514#endif
515
517}
518
519template<class T>
521{
522#ifdef IP_DEBUG_SMARTPTR
523 DBG_START_METH("SmartPtr<T>::~SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
524#endif
525
526 ReleasePointer_();
527}
528
529template<class T>
531{
532#ifdef IP_DEBUG_SMARTPTR
533 DBG_START_METH("T* SmartPtr<T>::operator->()", ipopt_dbg_smartptr_verbosity);
534#endif
535
536 // cannot deref a null pointer
537#if IPOPT_CHECKLEVEL > 0
538 assert(ptr_);
539#endif
540
541 return ptr_;
542}
543
544template<class T>
546{
547#ifdef IP_DEBUG_SMARTPTR
548 DBG_START_METH("T& SmartPtr<T>::operator*()", ipopt_dbg_smartptr_verbosity);
549#endif
550
551 // cannot dereference a null pointer
552#if IPOPT_CHECKLEVEL > 0
553 assert(ptr_);
554#endif
555
556 return *ptr_;
557}
558
559template<class T>
561 T* rhs
562)
563{
564#ifdef IP_DEBUG_SMARTPTR
565 DBG_START_METH("SmartPtr<T>& SmartPtr<T>::operator=(T* rhs)", ipopt_dbg_smartptr_verbosity);
566#endif
567
568 return SetFromRawPtr_(rhs);
569}
570
571template<class T>
573 const SmartPtr<T>& rhs
574)
575{
576#ifdef IP_DEBUG_SMARTPTR
578 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)",
580#endif
581
582 return SetFromSmartPtr_(rhs);
583}
584
585template<class T>
586template<class U>
588 const SmartPtr<U>& rhs
589)
590{
591#ifdef IP_DEBUG_SMARTPTR
593 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)",
595#endif
596
597 return SetFromSmartPtr_(GetRawPtr(rhs));
598}
599
600template<class T>
602 T* rhs
603)
604{
605#ifdef IP_DEBUG_SMARTPTR
607 "SmartPtr<T>& SmartPtr<T>::SetFromRawPtr_(T* rhs)", ipopt_dbg_smartptr_verbosity);
608#endif
609
610 if( rhs != 0 )
611 {
612 rhs->AddRef(this);
613 }
614
615 // Release any old pointer
616 ReleasePointer_();
617
618 ptr_ = rhs;
619
620 return *this;
621}
622
623template<class T>
625 const SmartPtr<T>& rhs
626)
627{
628#ifdef IP_DEBUG_SMARTPTR
630 "SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)",
632#endif
633
634 SetFromRawPtr_(GetRawPtr(rhs));
635
636 return (*this);
637}
638
639template<class T>
641{
642#ifdef IP_DEBUG_SMARTPTR
644 "void SmartPtr<T>::ReleasePointer()",
646#endif
647
648 if( ptr_ )
649 {
650 ptr_->ReleaseRef(this);
651 if( ptr_->ReferenceCount() == 0 )
652 {
653 delete ptr_;
654 ptr_ = 0;
655 }
656 }
657}
658
659template<class U>
662)
663{
664#ifdef IP_DEBUG_SMARTPTR
666 "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
667 0);
668#endif
669
670 return smart_ptr.ptr_;
671}
672
673template<class U>
676)
677{
678 // compiler should implicitly cast
679 return GetRawPtr(smart_ptr);
680}
681
682template<class U>
685)
686{
687 return !IsNull(smart_ptr);
688}
689
690template<class U>
693)
694{
695#ifdef IP_DEBUG_SMARTPTR
697 "bool IsNull(const SmartPtr<T>& smart_ptr)",
698 0);
699#endif
700
701 return (smart_ptr.ptr_ == 0);
702}
703
704template<class U1, class U2>
706 const U1* lhs,
707 const U2* rhs
708)
709{
710#ifdef IP_DEBUG_SMARTPTR
712 "bool ComparePtrs(const U1* lhs, const U2* rhs)",
714#endif
715
716 // Even if lhs and rhs point to the same object
717 // with different interfaces U1 and U2, we cannot guarantee that
718 // the value of the pointers will be equivalent. We can
719 // guarantee this if we convert to ReferencedObject* (see also #162)
720 const ReferencedObject* v_lhs = lhs;
721 const ReferencedObject* v_rhs = rhs;
722
723 return v_lhs == v_rhs;
724}
725
726template<class U1, class U2>
728 const SmartPtr<U1>& lhs,
729 const SmartPtr<U2>& rhs
730)
731{
732#ifdef IP_DEBUG_SMARTPTR
734 "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
736#endif
737
741}
742
743template<class U1, class U2>
745 const SmartPtr<U1>& lhs,
746 U2* raw_rhs
747)
748{
749#ifdef IP_DEBUG_SMARTPTR
751 "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
753#endif
754
757}
758
759template<class U1, class U2>
761 U1* raw_lhs,
762 const SmartPtr<U2>& rhs
763)
764{
765#ifdef IP_DEBUG_SMARTPTR
767 "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
769#endif
770
771 const U2* raw_rhs = GetRawPtr(rhs);
773}
774
775template<class U1, class U2>
777 const SmartPtr<U1>& lhs,
778 const SmartPtr<U2>& rhs
779)
780{
781#ifdef IP_DEBUG_SMARTPTR
783 "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
785#endif
786
787 bool retValue = operator==(lhs, rhs);
788 return !retValue;
789}
790
791template<class U1, class U2>
793 const SmartPtr<U1>& lhs,
794 U2* raw_rhs
795)
796{
797#ifdef IP_DEBUG_SMARTPTR
799 "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
801#endif
802
804 return !retValue;
805}
806
807template<class U1, class U2>
809 U1* raw_lhs,
810 const SmartPtr<U2>& rhs
811)
812{
813#ifdef IP_DEBUG_SMARTPTR
815 "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
817#endif
818
820 return !retValue;
821}
822
823template<class T>
824void swap(
825 SmartPtr<T>& a,
827)
828{
829#ifdef IP_DEBUG_REFERENCED
831 a = b;
832 b = tmp;
833#else
834 std::swap(a.prt_, b.ptr_);
835#endif
836}
837
838template<class T>
840 const SmartPtr<T>& lhs,
841 const SmartPtr<T>& rhs
842)
843{
844 return lhs.ptr_ < rhs.ptr_;
845}
846
847template<class T>
849 const SmartPtr<T>& lhs,
850 const SmartPtr<T>& rhs
851)
852{
853 return rhs < lhs;
854}
855
856template<class T> bool operator<=(
857 const SmartPtr<T>& lhs,
858 const SmartPtr<T>& rhs
859)
860{
861 return !(rhs < lhs);
862}
863
864template<class T> bool operator>=(
865 const SmartPtr<T>& lhs,
866 const SmartPtr<T>& rhs
867)
868{
869 return !(lhs < rhs);
870}
871
872} // namespace Ipopt
873
874#undef ipopt_dbg_smartptr_verbosity
875
876#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)