Ipopt 3.11.9
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// $Id: IpSmartPtr.hpp 2182 2013-03-30 20:02:18Z stefan $
6//
7// Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
8
9#ifndef __IPSMARTPTR_HPP__
10#define __IPSMARTPTR_HPP__
11
12#ifdef HAVE_CSTDDEF
13# include <cstddef>
14#else
15# ifdef HAVE_STDDEF_H
16# include <stddef.h>
17# else
18# error "don't have header file for stddef"
19# endif
20#endif
21
22#include "IpReferenced.hpp"
23
24#include "IpDebug.hpp"
25#if COIN_IPOPT_CHECKLEVEL > 2
26# define IP_DEBUG_SMARTPTR
27#endif
28#ifndef IPOPT_UNUSED
29# if defined(__GNUC__)
30# define IPOPT_UNUSED __attribute__((unused))
31# else
32# define IPOPT_UNUSED
33# endif
34#endif
35
36namespace Ipopt
37{
38
181 template<class T>
182 class SmartPtr : public Referencer
183 {
184 public:
185#define ipopt_dbg_smartptr_verbosity 0
186
191
193 SmartPtr(const SmartPtr<T>& copy);
194
196 template <class U>
197 SmartPtr(const SmartPtr<U>& copy);
198
200 SmartPtr(T* ptr);
201
207
212 T* operator->() const;
213
216 T& operator*() const;
217
221
226
230 template <class U>
232
235 template <class U1, class U2>
236 friend
237 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
238
241 template <class U1, class U2>
242 friend
243 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs);
244
247 template <class U1, class U2>
248 friend
249 bool operator==(U1* lhs, const SmartPtr<U2>& raw_rhs);
250
253 template <class U1, class U2>
254 friend
255 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
256
259 template <class U1, class U2>
260 friend
261 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs);
262
265 template <class U1, class U2>
266 friend
267 bool operator!=(U1* lhs, const SmartPtr<U2>& raw_rhs);
268
271 template <class U>
272 friend
273 bool operator<(const SmartPtr<U>& lhs, const SmartPtr<U>& rhs);
275
288 template <class U>
289 friend
290 U* GetRawPtr(const SmartPtr<U>& smart_ptr);
291
293 template <class U>
294 friend
296
301 template <class U>
302 friend
303 bool IsValid(const SmartPtr<U>& smart_ptr);
304
309 template <class U>
310 friend
311 bool IsNull(const SmartPtr<U>& smart_ptr);
313
314 private:
319
324
329
333 };
334
337 template <class U>
338 U* GetRawPtr(const SmartPtr<U>& smart_ptr);
339
340 template <class U>
341 SmartPtr<const U> ConstPtr(const SmartPtr<U>& smart_ptr);
342
343 template <class U>
344 bool IsNull(const SmartPtr<U>& smart_ptr);
345
346 template <class U>
347 bool IsValid(const SmartPtr<U>& smart_ptr);
348
349 template <class U1, class U2>
350 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
351
352 template <class U1, class U2>
353 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs);
354
355 template <class U1, class U2>
356 bool operator==(U1* lhs, const SmartPtr<U2>& raw_rhs);
357
358 template <class U1, class U2>
359 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
360
361 template <class U1, class U2>
362 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs);
363
364 template <class U1, class U2>
365 bool operator!=(U1* lhs, const SmartPtr<U2>& raw_rhs);
366
368
369
370 template <class T>
372 :
373 ptr_(0)
374 {
375#ifdef IP_DEBUG_SMARTPTR
376 DBG_START_METH("SmartPtr<T>::SmartPtr()", ipopt_dbg_smartptr_verbosity);
377#endif
378
379#ifndef NDEBUG
380 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
381#endif
382
383 }
384
385
386 template <class T>
388 :
389 ptr_(0)
390 {
391#ifdef IP_DEBUG_SMARTPTR
392 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<T>& copy)", ipopt_dbg_smartptr_verbosity);
393#endif
394
395#ifndef NDEBUG
396 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
397#endif
398
399 (void) SetFromSmartPtr_(copy);
400 }
401
402
403 template <class T>
404 template <class U>
406 :
407 ptr_(0)
408 {
409#ifdef IP_DEBUG_SMARTPTR
410 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<U>& copy)", ipopt_dbg_smartptr_verbosity);
411#endif
412
413#ifndef NDEBUG
414 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
415#endif
416
417 (void) SetFromSmartPtr_(GetRawPtr(copy));
418 }
419
420
421 template <class T>
423 :
424 ptr_(0)
425 {
426#ifdef IP_DEBUG_SMARTPTR
427 DBG_START_METH("SmartPtr<T>::SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
428#endif
429
430#ifndef NDEBUG
431 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
432#endif
433
434 (void) SetFromRawPtr_(ptr);
435 }
436
437 template <class T>
439 {
440#ifdef IP_DEBUG_SMARTPTR
441 DBG_START_METH("SmartPtr<T>::~SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
442#endif
443
444 ReleasePointer_();
445 }
446
447
448 template <class T>
450 {
451#ifdef IP_DEBUG_SMARTPTR
452 DBG_START_METH("T* SmartPtr<T>::operator->()", ipopt_dbg_smartptr_verbosity);
453#endif
454
455 // cannot deref a null pointer
456#if COIN_IPOPT_CHECKLEVEL > 0
457 assert(ptr_);
458#endif
459
460 return ptr_;
461 }
462
463
464 template <class T>
466 {
467#ifdef IP_DEBUG_SMARTPTR
468 DBG_START_METH("T& SmartPtr<T>::operator*()", ipopt_dbg_smartptr_verbosity);
469#endif
470
471 // cannot dereference a null pointer
472#if COIN_IPOPT_CHECKLEVEL > 0
473 assert(ptr_);
474#endif
475
476 return *ptr_;
477 }
478
479
480 template <class T>
482 {
483#ifdef IP_DEBUG_SMARTPTR
484 DBG_START_METH("SmartPtr<T>& SmartPtr<T>::operator=(T* rhs)", ipopt_dbg_smartptr_verbosity);
485#endif
486
487 return SetFromRawPtr_(rhs);
488 }
489
490
491 template <class T>
493 {
494#ifdef IP_DEBUG_SMARTPTR
496 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)",
498#endif
499
500 return SetFromSmartPtr_(rhs);
501 }
502
503
504 template <class T>
505 template <class U>
507 {
508#ifdef IP_DEBUG_SMARTPTR
510 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)",
512#endif
513
514 return SetFromSmartPtr_(GetRawPtr(rhs));
515 }
516
517
518 template <class T>
520 {
521#ifdef IP_DEBUG_SMARTPTR
523 "SmartPtr<T>& SmartPtr<T>::SetFromRawPtr_(T* rhs)", ipopt_dbg_smartptr_verbosity);
524#endif
525
526 if (rhs != 0)
527 rhs->AddRef(this);
528
529 // Release any old pointer
530 ReleasePointer_();
531
532 ptr_ = rhs;
533
534 return *this;
535 }
536
537 template <class T>
539 {
540#ifdef IP_DEBUG_SMARTPTR
542 "SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)",
544#endif
545
546 SetFromRawPtr_(GetRawPtr(rhs));
547
548 return (*this);
549 }
550
551
552 template <class T>
554 {
555#ifdef IP_DEBUG_SMARTPTR
557 "void SmartPtr<T>::ReleasePointer()",
559#endif
560
561 if (ptr_) {
562 ptr_->ReleaseRef(this);
563 if (ptr_->ReferenceCount() == 0)
564 delete ptr_;
565 }
566 }
567
568
569 template <class U>
570 U* GetRawPtr(const SmartPtr<U>& smart_ptr)
571 {
572#ifdef IP_DEBUG_SMARTPTR
574 "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
575 0);
576#endif
577
578 return smart_ptr.ptr_;
579 }
580
581 template <class U>
583 {
584 // compiler should implicitly cast
585 return GetRawPtr(smart_ptr);
586 }
587
588 template <class U>
589 bool IsValid(const SmartPtr<U>& smart_ptr)
590 {
591 return !IsNull(smart_ptr);
592 }
593
594 template <class U>
595 bool IsNull(const SmartPtr<U>& smart_ptr)
596 {
597#ifdef IP_DEBUG_SMARTPTR
599 "bool IsNull(const SmartPtr<T>& smart_ptr)",
600 0);
601#endif
602
603 return (smart_ptr.ptr_ == 0);
604 }
605
606
607 template <class U1, class U2>
608 bool ComparePointers(const U1* lhs, const U2* rhs)
609 {
610#ifdef IP_DEBUG_SMARTPTR
612 "bool ComparePtrs(const U1* lhs, const U2* rhs)",
614#endif
615
616 // Even if lhs and rhs point to the same object
617 // with different interfaces U1 and U2, we cannot guarantee that
618 // the value of the pointers will be equivalent. We can
619 // guarantee this if we convert to ReferencedObject* (see also #162)
620 const ReferencedObject* v_lhs = lhs;
621 const ReferencedObject* v_rhs = rhs;
622
623 return v_lhs == v_rhs;
624 }
625
626 template <class U1, class U2>
627 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)
628 {
629#ifdef IP_DEBUG_SMARTPTR
631 "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
633#endif
634
635 U1* raw_lhs = GetRawPtr(lhs);
636 U2* raw_rhs = GetRawPtr(rhs);
637 return ComparePointers(raw_lhs, raw_rhs);
638 }
639
640 template <class U1, class U2>
641 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs)
642 {
643#ifdef IP_DEBUG_SMARTPTR
645 "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
647#endif
648
649 U1* raw_lhs = GetRawPtr(lhs);
650 return ComparePointers(raw_lhs, raw_rhs);
651 }
652
653 template <class U1, class U2>
654 bool operator==(U1* raw_lhs, const SmartPtr<U2>& rhs)
655 {
656#ifdef IP_DEBUG_SMARTPTR
658 "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
660#endif
661
662 const U2* raw_rhs = GetRawPtr(rhs);
663 return ComparePointers(raw_lhs, raw_rhs);
664 }
665
666 template <class U1, class U2>
667 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)
668 {
669#ifdef IP_DEBUG_SMARTPTR
671 "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
673#endif
674
675 bool retValue = operator==(lhs, rhs);
676 return !retValue;
677 }
678
679 template <class U1, class U2>
680 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs)
681 {
682#ifdef IP_DEBUG_SMARTPTR
684 "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
686#endif
687
688 bool retValue = operator==(lhs, raw_rhs);
689 return !retValue;
690 }
691
692 template <class U1, class U2>
693 bool operator!=(U1* raw_lhs, const SmartPtr<U2>& rhs)
694 {
695#ifdef IP_DEBUG_SMARTPTR
697 "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
699#endif
700
701 bool retValue = operator==(raw_lhs, rhs);
702 return !retValue;
703 }
704
705 template <class T>
707 {
708#ifdef IP_DEBUG_REFERENCED
709 SmartPtr<T> tmp(a);
710 a = b;
711 b = tmp;
712#else
713 std::swap(a.prt_, b.ptr_);
714#endif
715 }
716
717 template <class T>
718 bool operator<(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
719 {
720 return lhs.ptr_ < rhs.ptr_;
721 }
722
723 template <class T>
724 bool operator> (const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
725 {
726 return rhs < lhs;
727 }
728
729 template <class T> bool
730 operator<=(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
731 {
732 return !( rhs < lhs );
733 }
734
735 template <class T> bool
736 operator>=(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
737 {
738 return !( lhs < rhs );
739 }
740} // namespace Ipopt
741
742#undef ipopt_dbg_smartptr_verbosity
743
744#endif
#define DBG_START_FUN(__func_name, __verbose_level)
Definition IpDebug.hpp:48
#define DBG_START_METH(__func_name, __verbose_level)
Definition IpDebug.hpp:49
#define IPOPT_UNUSED
#define ipopt_dbg_smartptr_verbosity
ReferencedObject class.
Psydo-class, from which everything has to inherit that wants to use be registered as a Referencer for...
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, 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.
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)