Ipopt  3.12.12
 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 
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>
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>
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>
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>
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>
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>
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>
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>
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>
483  {
484 #ifdef IP_DEBUG_SMARTPTR
486  "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T>& rhs)",
488 #endif
489 
490  return SetFromSmartPtr_(rhs);
491  }
492 
493 
494  template <class T>
495  template <class U>
497  {
498 #ifdef IP_DEBUG_SMARTPTR
500  "SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<U>& rhs)",
502 #endif
503 
504  return SetFromSmartPtr_(GetRawPtr(rhs));
505  }
506 
507 
508  template <class T>
510  {
511 #ifdef IP_DEBUG_SMARTPTR
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>
529  {
530 #ifdef IP_DEBUG_SMARTPTR
532  "SmartPtr<T>& SmartPtr<T>::SetFromSmartPtr_(const SmartPtr<T>& rhs)",
534 #endif
535 
536  SetFromRawPtr_(GetRawPtr(rhs));
537 
538  return (*this);
539  }
540 
541 
542  template <class T>
544  {
545 #ifdef IP_DEBUG_SMARTPTR
547  "void SmartPtr<T>::ReleasePointer()",
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
564  "T* GetRawPtr(const SmartPtr<T>& smart_ptr)",
565  0);
566 #endif
567 
568  return smart_ptr.ptr_;
569  }
570 
571  template <class U>
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
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
602  "bool ComparePtrs(const U1* lhs, const U2* rhs)",
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
621  "bool operator==(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
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
635  "bool operator==(SmartPtr<U1>& lhs, U2* rhs)",
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
648  "bool operator==(U1* raw_lhs, SmartPtr<U2>& rhs)",
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
661  "bool operator!=(const SmartPtr<U1>& lhs, const SmartPtr<U2>& rhs)",
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
674  "bool operator!=(SmartPtr<U1>& lhs, U2* rhs)",
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
687  "bool operator!=(U1* raw_lhs, SmartPtr<U2>& rhs)",
689 #endif
690 
691  bool retValue = operator==(raw_lhs, rhs);
692  return !retValue;
693  }
694 
695  template <class T>
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
~SmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
Definition: IpSmartPtr.hpp:428
bool IsValid(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:579
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
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
#define DBG_START_METH(__func_name, __verbose_level)
Definition: IpDebug.hpp:49
bool IsNull(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:585
bool operator>(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:714
bool operator>=(const SmartPtr< T > &lhs, const SmartPtr< T > &rhs)
Definition: IpSmartPtr.hpp:726
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: IpSmartPtr.hpp:598
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:172
ReferencedObject class.
bool operator!=(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:657
U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:560
T * ptr_
Actual raw pointer to the object.
Definition: IpSmartPtr.hpp:308
Psydo-class, from which everything has to inherit that wants to use be registered as a Referencer for...
#define ipopt_dbg_smartptr_verbosity
Definition: IpSmartPtr.hpp:175
#define DBG_START_FUN(__func_name, __verbose_level)
Definition: IpDebug.hpp:48
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 U * GetRawPtr(const SmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:572
SmartPtr()
Default constructor, initialized to NULL.
Definition: IpSmartPtr.hpp:361
void ReleasePointer_()
Release the currently referenced object.
Definition: IpSmartPtr.hpp:543
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
friend bool IsValid(const SmartPtr< U > &smart_ptr)
Returns true if the SmartPtr is NOT NULL.
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 SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Returns a const pointer.
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: IpSmartPtr.hpp:455
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: IpSmartPtr.hpp:439
#define IPOPT_UNUSED
Definition: IpSmartPtr.hpp:22
bool operator==(const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
Definition: IpSmartPtr.hpp:617
void swap(SmartPtr< T > &a, SmartPtr< T > &b)
Definition: IpSmartPtr.hpp:696