Ipopt 3.12.10

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 #include "IpReferenced.hpp"
13
14 #include "IpDebug.hpp"
15 #if COIN_IPOPT_CHECKLEVEL > 2
16 # define IP_DEBUG_SMARTPTR
17 #endif
18 #ifndef IPOPT_UNUSED
19 # if defined(__GNUC__)
20 # define IPOPT_UNUSED __attribute__((unused))
21 # else
22 # define IPOPT_UNUSED
23 # endif
24 #endif
25
26 namespace Ipopt
27 {
28
171 template<class T>
172 class SmartPtr : public Referencer
173 {
174 public:
175 #define ipopt_dbg_smartptr_verbosity 0
176
180 SmartPtr();
181
183 SmartPtr(const SmartPtr<T>& copy);
184
186 template <class U>
187 SmartPtr(const SmartPtr<U>& copy);
188
190 SmartPtr(T* ptr);
191
195 ~SmartPtr();
197
202 T* operator->() const;
203
206 T& operator*() const;
207
210 SmartPtr<T>& operator=(T* rhs);
211
215 SmartPtr<T>& operator=(const SmartPtr<T>& rhs);
216
220 template <class U>
221 SmartPtr<T>& operator=(const SmartPtr<U>& rhs);
222
225 template <class U1, class U2>
226 friend
227 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
228
231 template <class U1, class U2>
232 friend
233 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs);
234
237 template <class U1, class U2>
238 friend
239 bool operator==(U1* lhs, const SmartPtr<U2>& raw_rhs);
240
243 template <class U1, class U2>
244 friend
245 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
246
249 template <class U1, class U2>
250 friend
251 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs);
252
255 template <class U1, class U2>
256 friend
257 bool operator!=(U1* lhs, const SmartPtr<U2>& raw_rhs);
258
261 template <class U>
262 friend
263 bool operator<(const SmartPtr<U>& lhs, const SmartPtr<U>& rhs);
265
278 template <class U>
279 friend
280 U* GetRawPtr(const SmartPtr<U>& smart_ptr);
281
283 template <class U>
284 friend
285 SmartPtr<const U> ConstPtr(const SmartPtr<U>& smart_ptr);
286
291 template <class U>
292 friend
293 bool IsValid(const SmartPtr<U>& smart_ptr);
294
299 template <class U>
300 friend
301 bool IsNull(const SmartPtr<U>& smart_ptr);
303
304 private:
308 T* ptr_;
309
313 SmartPtr<T>& SetFromRawPtr_(T* rhs);
314
318 SmartPtr<T>& SetFromSmartPtr_(const SmartPtr<T>& rhs);
319
321 void ReleasePointer_();
323 };
324
327 template <class U>
328 U* GetRawPtr(const SmartPtr<U>& smart_ptr);
329
330 template <class U>
331 SmartPtr<const U> ConstPtr(const SmartPtr<U>& smart_ptr);
332
333 template <class U>
334 bool IsNull(const SmartPtr<U>& smart_ptr);
335
336 template <class U>
337 bool IsValid(const SmartPtr<U>& smart_ptr);
338
339 template <class U1, class U2>
340 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
341
342 template <class U1, class U2>
343 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs);
344
345 template <class U1, class U2>
346 bool operator==(U1* lhs, const SmartPtr<U2>& raw_rhs);
347
348 template <class U1, class U2>
349 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs);
350
351 template <class U1, class U2>
352 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs);
353
354 template <class U1, class U2>
355 bool operator!=(U1* lhs, const SmartPtr<U2>& raw_rhs);
356
358
359
360 template <class T>
361 SmartPtr<T>::SmartPtr()
362 :
363 ptr_(0)
364 {
365 #ifdef IP_DEBUG_SMARTPTR
366 DBG_START_METH("SmartPtr<T>::SmartPtr()", ipopt_dbg_smartptr_verbosity);
367 #endif
368
369 #ifndef NDEBUG
370 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
371 #endif
372
373 }
374
375
376 template <class T>
377 SmartPtr<T>::SmartPtr(const SmartPtr<T>& copy)
378 :
379 ptr_(0)
380 {
381 #ifdef IP_DEBUG_SMARTPTR
382 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<T>& copy)", ipopt_dbg_smartptr_verbosity);
383 #endif
384
385 #ifndef NDEBUG
386 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
387 #endif
388
389 (void) SetFromSmartPtr_(copy);
390 }
391
392
393 template <class T>
394 template <class U>
395 SmartPtr<T>::SmartPtr(const SmartPtr<U>& copy)
396 :
397 ptr_(0)
398 {
399 #ifdef IP_DEBUG_SMARTPTR
400 DBG_START_METH("SmartPtr<T>::SmartPtr(const SmartPtr<U>& copy)", ipopt_dbg_smartptr_verbosity);
401 #endif
402
403 #ifndef NDEBUG
404 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
405 #endif
406
407 (void) SetFromSmartPtr_(GetRawPtr(copy));
408 }
409
410
411 template <class T>
412 SmartPtr<T>::SmartPtr(T* ptr)
413 :
414 ptr_(0)
415 {
416 #ifdef IP_DEBUG_SMARTPTR
417 DBG_START_METH("SmartPtr<T>::SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
418 #endif
419
420 #ifndef NDEBUG
421 const ReferencedObject* IPOPT_UNUSED trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = ptr_;
422 #endif
423
424 (void) SetFromRawPtr_(ptr);
425 }
426
427 template <class T>
428 SmartPtr<T>::~SmartPtr()
429 {
430 #ifdef IP_DEBUG_SMARTPTR
431 DBG_START_METH("SmartPtr<T>::~SmartPtr(T* ptr)", ipopt_dbg_smartptr_verbosity);
432 #endif
433
434 ReleasePointer_();
435 }
436
437
438 template <class T>
439 T* SmartPtr<T>::operator->() const
440 {
441 #ifdef IP_DEBUG_SMARTPTR
442 DBG_START_METH("T* SmartPtr<T>::operator->()", ipopt_dbg_smartptr_verbosity);
443 #endif
444
445 // cannot deref a null pointer
446 #if COIN_IPOPT_CHECKLEVEL > 0
447 assert(ptr_);
448 #endif
449
450 return ptr_;
451 }
452
453
454 template <class T>
455 T& SmartPtr<T>::operator*() const
456 {
457 #ifdef IP_DEBUG_SMARTPTR
458 DBG_START_METH("T& SmartPtr<T>::operator*()", ipopt_dbg_smartptr_verbosity);
459 #endif
460
461 // cannot dereference a null pointer
462 #if COIN_IPOPT_CHECKLEVEL > 0
463 assert(ptr_);
464 #endif
465
466 return *ptr_;
467 }
468
469
470 template <class T>
471 SmartPtr<T>& SmartPtr<T>::operator=(T* rhs)
472 {
473 #ifdef IP_DEBUG_SMARTPTR
474 DBG_START_METH("SmartPtr<T>& SmartPtr<T>::operator=(T* rhs)", ipopt_dbg_smartptr_verbosity);
475 #endif
476
477 return SetFromRawPtr_(rhs);
478 }
479
480
481 template <class T>
482 SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)
483 {
484 #ifdef IP_DEBUG_SMARTPTR
485 DBG_START_METH(
486 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)",
487 ipopt_dbg_smartptr_verbosity);
488 #endif
489
490 return SetFromSmartPtr_(rhs);
491 }
492
493
494 template <class T>
495 template <class U>
496 SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)
497 {
498 #ifdef IP_DEBUG_SMARTPTR
499 DBG_START_METH(
500 "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)",
501 ipopt_dbg_smartptr_verbosity);
502 #endif
503
504 return SetFromSmartPtr_(GetRawPtr(rhs));
505 }
506
507
508 template <class T>
509 SmartPtr<T>& SmartPtr<T>::SetFromRawPtr_(T* rhs)
510 {
511 #ifdef IP_DEBUG_SMARTPTR
512 DBG_START_METH(
513 "SmartPtr<T>& SmartPtr<T>::SetFromRawPtr_(T* rhs)", ipopt_dbg_smartptr_verbosity);
514 #endif
515
516 if (rhs != 0)
517 rhs->AddRef(this);
518
519 // Release any old pointer
520 ReleasePointer_();
521
522 ptr_ = rhs;
523
524 return *this;
525 }
526
527 template <class T>
528 SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)
529 {
530 #ifdef IP_DEBUG_SMARTPTR
531 DBG_START_METH(
532 "SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)",
533 ipopt_dbg_smartptr_verbosity);
534 #endif
535
536 SetFromRawPtr_(GetRawPtr(rhs));
537
538 return (*this);
539 }
540
541
542 template <class T>
543 void SmartPtr<T>::ReleasePointer_()
544 {
545 #ifdef IP_DEBUG_SMARTPTR
546 DBG_START_METH(
547 "void SmartPtr<T>::ReleasePointer()",
548 ipopt_dbg_smartptr_verbosity);
549 #endif
550
551 if (ptr_) {
552 ptr_->ReleaseRef(this);
553 if (ptr_->ReferenceCount() == 0)
554 delete ptr_;
555 }
556 }
557
558
559 template <class U>
560 U* GetRawPtr(const SmartPtr<U>& smart_ptr)
561 {
562 #ifdef IP_DEBUG_SMARTPTR
563 DBG_START_FUN(
564 "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
565 0);
566 #endif
567
568 return smart_ptr.ptr_;
569 }
570
571 template <class U>
572 SmartPtr<const U> ConstPtr(const SmartPtr<U>& smart_ptr)
573 {
574 // compiler should implicitly cast
575 return GetRawPtr(smart_ptr);
576 }
577
578 template <class U>
579 bool IsValid(const SmartPtr<U>& smart_ptr)
580 {
581 return !IsNull(smart_ptr);
582 }
583
584 template <class U>
585 bool IsNull(const SmartPtr<U>& smart_ptr)
586 {
587 #ifdef IP_DEBUG_SMARTPTR
588 DBG_START_FUN(
589 "bool IsNull(const SmartPtr<T>& smart_ptr)",
590 0);
591 #endif
592
593 return (smart_ptr.ptr_ == 0);
594 }
595
596
597 template <class U1, class U2>
598 bool ComparePointers(const U1* lhs, const U2* rhs)
599 {
600 #ifdef IP_DEBUG_SMARTPTR
601 DBG_START_FUN(
602 "bool ComparePtrs(const U1* lhs, const U2* rhs)",
603 ipopt_dbg_smartptr_verbosity);
604 #endif
605
606 // Even if lhs and rhs point to the same object
607 // with different interfaces U1 and U2, we cannot guarantee that
608 // the value of the pointers will be equivalent. We can
609 // guarantee this if we convert to ReferencedObject* (see also #162)
610 const ReferencedObject* v_lhs = lhs;
611 const ReferencedObject* v_rhs = rhs;
612
613 return v_lhs == v_rhs;
614 }
615
616 template <class U1, class U2>
617 bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)
618 {
619 #ifdef IP_DEBUG_SMARTPTR
620 DBG_START_FUN(
621 "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
622 ipopt_dbg_smartptr_verbosity);
623 #endif
624
625 U1* raw_lhs = GetRawPtr(lhs);
626 U2* raw_rhs = GetRawPtr(rhs);
627 return ComparePointers(raw_lhs, raw_rhs);
628 }
629
630 template <class U1, class U2>
631 bool operator==(const SmartPtr<U1>& lhs, U2* raw_rhs)
632 {
633 #ifdef IP_DEBUG_SMARTPTR
634 DBG_START_FUN(
635 "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
636 ipopt_dbg_smartptr_verbosity);
637 #endif
638
639 U1* raw_lhs = GetRawPtr(lhs);
640 return ComparePointers(raw_lhs, raw_rhs);
641 }
642
643 template <class U1, class U2>
644 bool operator==(U1* raw_lhs, const SmartPtr<U2>& rhs)
645 {
646 #ifdef IP_DEBUG_SMARTPTR
647 DBG_START_FUN(
648 "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
649 ipopt_dbg_smartptr_verbosity);
650 #endif
651
652 const U2* raw_rhs = GetRawPtr(rhs);
653 return ComparePointers(raw_lhs, raw_rhs);
654 }
655
656 template <class U1, class U2>
657 bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)
658 {
659 #ifdef IP_DEBUG_SMARTPTR
660 DBG_START_FUN(
661 "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
662 ipopt_dbg_smartptr_verbosity);
663 #endif
664
665 bool retValue = operator==(lhs, rhs);
666 return !retValue;
667 }
668
669 template <class U1, class U2>
670 bool operator!=(const SmartPtr<U1>& lhs, U2* raw_rhs)
671 {
672 #ifdef IP_DEBUG_SMARTPTR
673 DBG_START_FUN(
674 "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
675 ipopt_dbg_smartptr_verbosity);
676 #endif
677
678 bool retValue = operator==(lhs, raw_rhs);
679 return !retValue;
680 }
681
682 template <class U1, class U2>
683 bool operator!=(U1* raw_lhs, const SmartPtr<U2>& rhs)
684 {
685 #ifdef IP_DEBUG_SMARTPTR
686 DBG_START_FUN(
687 "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
688 ipopt_dbg_smartptr_verbosity);
689 #endif
690
691 bool retValue = operator==(raw_lhs, rhs);
692 return !retValue;
693 }
694
695 template <class T>
696 void swap(SmartPtr<T>& a, SmartPtr<T>& b)
697 {
698 #ifdef IP_DEBUG_REFERENCED
699 SmartPtr<T> tmp(a);
700 a = b;
701 b = tmp;
702 #else
703 std::swap(a.prt_, b.ptr_);
704 #endif
705 }
706
707 template <class T>
708 bool operator<(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
709 {
710 return lhs.ptr_ < rhs.ptr_;
711 }
712
713 template <class T>
714 bool operator> (const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
715 {
716 return rhs < lhs;
717 }
718
719 template <class T> bool
720 operator<=(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
721 {
722 return !( rhs < lhs );
723 }
724
725 template <class T> bool
726 operator>=(const SmartPtr<T>& lhs, const SmartPtr<T>& rhs)
727 {
728 return !( lhs < rhs );
729 }
730 } // namespace Ipopt
731
732 #undef ipopt_dbg_smartptr_verbosity
733
734 #endif
Ipopt::SmartPtr::~SmartPtr
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
Definition: IpSmartPtr.hpp:428
Ipopt::IsValid
bool IsValid(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:579
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:471
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:509
DBG_START_METH
#define DBG_START_METH(__func_name, __verbose_level)
Definition: IpDebug.hpp:49
Ipopt::IsNull
bool IsNull(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:585
Ipopt::operator>
bool operator>(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:714
Ipopt::operator>=
bool operator>=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:726
Ipopt::ComparePointers
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: IpSmartPtr.hpp:598
Ipopt::SmartPtr
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:172
Ipopt::ReferencedObject
ReferencedObject class.
Definition: IpReferenced.hpp:174
Ipopt::operator!=
bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:657
Ipopt::GetRawPtr
U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:560
Ipopt::SmartPtr::ptr_
T * ptr_
Actual raw pointer to the object.
Definition: IpSmartPtr.hpp:308
Ipopt::Referencer
Psydo-class, from which everything has to inherit that wants to use be registered as a Referencer for...
Definition: IpReferenced.hpp:27
ipopt_dbg_smartptr_verbosity
#define ipopt_dbg_smartptr_verbosity
Definition: IpSmartPtr.hpp:175
IpDebug.hpp
DBG_START_FUN
#define DBG_START_FUN(__func_name, __verbose_level)
Definition: IpDebug.hpp:48
IpReferenced.hpp
Ipopt::SmartPtr::IsNull
friend bool IsNull(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NULL.
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...
Ipopt::SmartPtr::GetRawPtr
friend U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
Ipopt::ConstPtr
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:572
Ipopt::SmartPtr::SmartPtr
SmartPtr()
Default constructor, initialized to NULL.
Definition: IpSmartPtr.hpp:361
Ipopt::SmartPtr::ReleasePointer_
void ReleasePointer_()
Release the currently referenced object.
Definition: IpSmartPtr.hpp:543
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:528
Ipopt::SmartPtr::IsValid
friend bool IsValid(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NOT NULL.
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...
Ipopt::SmartPtr::ConstPtr
friend SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Returns a const pointer.
Ipopt::SmartPtr::operator*
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: IpSmartPtr.hpp:455
Ipopt::SmartPtr::operator->
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: IpSmartPtr.hpp:439
IPOPT_UNUSED
#define IPOPT_UNUSED
Definition: IpSmartPtr.hpp:22
Ipopt::operator==
bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:617
Ipopt::swap
void swap(SmartPtr< T > &a, SmartPtr< T > &b)
Definition: IpSmartPtr.hpp:696