Ipopt Documentation  
 
Loading...
Searching...
No Matches
Ipopt::SmartPtr< T > Class Template Reference

Template class for Smart Pointers. More...

#include <IpSmartPtr.hpp>

Inherits Ipopt::Referencer.

Public Member Functions

Constructors/Destructors
 SmartPtr ()
 Default constructor, initialized to NULL.
 
 SmartPtr (const SmartPtr< T > &copy)
 Copy constructor, initialized from copy of type T.
 
template<class U >
 SmartPtr (const SmartPtr< U > &copy)
 Copy constructor, initialized from copy of type U.
 
 SmartPtr (T *ptr)
 Constructor, initialized from T* ptr.
 
 ~SmartPtr ()
 Destructor, automatically decrements the reference count and deletes the object if necessary.
 

Friends

friend method declarations
template<class U >
UGetRawPtr (const SmartPtr< U > &smart_ptr)
 Returns the raw pointer contained.
 
template<class U >
SmartPtr< const UConstPtr (const SmartPtr< U > &smart_ptr)
 Returns a const pointer.
 
template<class U >
bool IsValid (const SmartPtr< U > &smart_ptr)
 Returns true if the SmartPtr is NOT NULL.
 
template<class U >
bool IsNull (const SmartPtr< U > &smart_ptr)
 Returns true if the SmartPtr is NULL.
 

Private Data/Methods

Tptr_
 Actual raw pointer to the object.
 
SmartPtr< T > & SetFromRawPtr_ (T *rhs)
 Set the value of the internal raw pointer from another raw pointer, releasing the previously referenced object if necessary.
 
SmartPtr< T > & SetFromSmartPtr_ (const SmartPtr< T > &rhs)
 Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object if necessary.
 
void ReleasePointer_ ()
 Release the currently referenced object.
 

Overloaded operators.

Toperator-> () const
 Overloaded arrow operator, allows the user to call methods using the contained pointer.
 
Toperator* () const
 Overloaded dereference operator, allows the user to dereference the contained pointer.
 
SmartPtr< T > & operator= (T *rhs)
 Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer.
 
SmartPtr< T > & operator= (const SmartPtr< T > &rhs)
 Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr.
 
template<class U >
SmartPtr< T > & operator= (const SmartPtr< U > &rhs)
 Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr of a different type.
 
template<class U1 , class U2 >
bool operator== (const SmartPtr< U1 > &lhs, const SmartPtr< U2 > &rhs)
 Overloaded equality comparison operator, allows the user to compare the value of two SmartPtrs.
 
template<class U1 , class U2 >
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 raw pointer.
 
template<class U1 , class U2 >
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 SmartPtr.
 
template<class U1 , class U2 >
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.
 
template<class U1 , class U2 >
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 raw pointer.
 
template<class U1 , class U2 >
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 raw pointer.
 
template<class U >
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.
 

Detailed Description

template<class T>
class Ipopt::SmartPtr< T >

Template class for Smart Pointers.

A SmartPtr behaves much like a raw pointer, but manages the lifetime of an object, deleting the object automatically. This class implements a reference-counting, intrusive smart pointer design, where all objects pointed to must inherit off of ReferencedObject, which stores the reference count. Although this is intrusive (native types and externally authored classes require wrappers to be referenced by smart pointers), it is a safer design. A more detailed discussion of these issues follows after the usage information.

Usage Example: Note: to use the SmartPtr, all objects to which you point MUST inherit off of ReferencedObject.

*
* In MyClass.hpp...
*
* #include "IpReferenced.hpp"

* namespace Ipopt {
*
*  class MyClass : public ReferencedObject // must derive from ReferencedObject
*    {
*      ...
*    }
* } // namespace Ipopt
*
*
* In my_usage.cpp...
*
* #include "IpSmartPtr.hpp"
* #include "MyClass.hpp"
*
* void func(AnyObject& obj)
*  {
*    SmartPtr<MyClass> ptr_to_myclass = new MyClass(...);
*    // ptr_to_myclass now points to a new MyClass,
*    // and the reference count is 1
*
*    ...
*
*    obj.SetMyClass(ptr_to_myclass);
*    // Here, let's assume that AnyObject uses a
*    // SmartPtr<MyClass> internally here.
*    // Now, both ptr_to_myclass and the internal
*    // SmartPtr in obj point to the same MyClass object
*    // and its reference count is 2.
*
*    ...
*
*    // No need to delete ptr_to_myclass, this
*    // will be done automatically when the
*    // reference count drops to zero.
*
*  }
*
* 

It is not necessary to use SmartPtr's in all cases where an object is used that has been allocated "into" a SmartPtr. It is possible to just pass objects by reference or regular pointers, even if lower down in the stack a SmartPtr is to be held on to. Everything should work fine as long as a pointer created by "new" is immediately passed into a SmartPtr, and if SmartPtr's are used to hold on to objects.

Other Notes: The SmartPtr implements both dereference operators -> & *. The SmartPtr does NOT implement a conversion operator to the raw pointer. Use the GetRawPtr() method when this is necessary. Make sure that the raw pointer is NOT deleted. The SmartPtr implements the comparison operators == & != for a variety of types. Use these instead of

*    if (GetRawPtr(smrt_ptr) == ptr) // Don't use this
*    

SmartPtr's, as currently implemented, do NOT handle circular references. For example: consider a higher level object using SmartPtrs to point to A and B, but A and B also point to each other (i.e. A has a SmartPtr to B and B has a SmartPtr to A). In this scenario, when the higher level object is finished with A and B, their reference counts will never drop to zero (since they reference each other) and they will not be deleted. This can be detected by memory leak tools like valgrind. If the circular reference is necessary, the problem can be overcome by a number of techniques:

1) A and B can have a method that "releases" each other, that is they set their internal SmartPtrs to NULL.

*        void AClass::ReleaseCircularReferences()
*          {
*          smart_ptr_to_B = NULL;
*          }
*        

Then, the higher level class can call these methods before it is done using A & B.

2) Raw pointers can be used in A and B to reference each other. Here, an implicit assumption is made that the lifetime is controlled by the higher level object and that A and B will both exist in a controlled manner. Although this seems dangerous, in many situations, this type of referencing is very controlled and this is reasonably safe.

3) This SmartPtr class could be redesigned with the Weak/Strong design concept. Here, the SmartPtr is identified as being Strong (controls lifetime of the object) or Weak (merely referencing the object). The Strong SmartPtr increments (and decrements) the reference count in ReferencedObject but the Weak SmartPtr does not. In the example above, the higher level object would have Strong SmartPtrs to A and B, but A and B would have Weak SmartPtrs to each other. Then, when the higher level object was done with A and B, they would be deleted. The Weak SmartPtrs in A and B would not decrement the reference count and would, of course, not delete the object. This idea is very similar to item (2), where it is implied that the sequence of events is controlled such that A and B will not call anything using their pointers following the higher level delete (i.e. in their destructors!). This is somehow safer, however, because code can be written (however expensive) to perform run-time detection of this situation. For example, the ReferencedObject could store pointers to all Weak SmartPtrs that are referencing it and, in its destructor, tell these pointers that it is dying. They could then set themselves to NULL, or set an internal flag to detect usage past this point.

Comments on Non-Intrusive Design: In a non-intrusive design, the reference count is stored somewhere other than the object being referenced. This means, unless the reference counting pointer is the first referencer, it must get a pointer to the referenced object from another smart pointer (so it has access to the reference count location). In this non-intrusive design, if we are pointing to an object with a smart pointer (or a number of smart pointers), and we then give another smart pointer the address through a RAW pointer, we will have two independent, AND INCORRECT, reference counts. To avoid this pitfall, we use an intrusive reference counting technique where the reference count is stored in the object being referenced.

Definition at line 164 of file IpSmartPtr.hpp.

Constructor & Destructor Documentation

◆ SmartPtr() [1/4]

template<class T >
Ipopt::SmartPtr< T >::SmartPtr ( )

Default constructor, initialized to NULL.

Definition at line 438 of file IpSmartPtr.hpp.

◆ SmartPtr() [2/4]

template<class T >
Ipopt::SmartPtr< T >::SmartPtr ( const SmartPtr< T > &  copy)

Copy constructor, initialized from copy of type T.

Definition at line 452 of file IpSmartPtr.hpp.

◆ SmartPtr() [3/4]

template<class T >
template<class U >
Ipopt::SmartPtr< T >::SmartPtr ( const SmartPtr< U > &  copy)

Copy constructor, initialized from copy of type U.

Definition at line 472 of file IpSmartPtr.hpp.

◆ SmartPtr() [4/4]

template<class T >
Ipopt::SmartPtr< T >::SmartPtr ( T ptr)

Constructor, initialized from T* ptr.

Definition at line 491 of file IpSmartPtr.hpp.

◆ ~SmartPtr()

template<class T >
Ipopt::SmartPtr< T >::~SmartPtr ( )

Destructor, automatically decrements the reference count and deletes the object if necessary.

Definition at line 510 of file IpSmartPtr.hpp.

Member Function Documentation

◆ operator->()

template<class T >
T * Ipopt::SmartPtr< T >::operator-> ( ) const

Overloaded arrow operator, allows the user to call methods using the contained pointer.

Definition at line 520 of file IpSmartPtr.hpp.

◆ operator*()

template<class T >
T & Ipopt::SmartPtr< T >::operator* ( ) const

Overloaded dereference operator, allows the user to dereference the contained pointer.

Definition at line 535 of file IpSmartPtr.hpp.

◆ operator=() [1/3]

template<class T >
SmartPtr< T > & Ipopt::SmartPtr< T >::operator= ( T rhs)

Overloaded equals operator, allows the user to set the value of the SmartPtr from a raw pointer.

Definition at line 550 of file IpSmartPtr.hpp.

◆ operator=() [2/3]

template<class T >
SmartPtr< T > & Ipopt::SmartPtr< T >::operator= ( const SmartPtr< T > &  rhs)

Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr.

Definition at line 562 of file IpSmartPtr.hpp.

◆ operator=() [3/3]

template<class T >
template<class U >
SmartPtr< T > & Ipopt::SmartPtr< T >::operator= ( const SmartPtr< U > &  rhs)

Overloaded equals operator, allows the user to set the value of the SmartPtr from another SmartPtr of a different type.

Definition at line 577 of file IpSmartPtr.hpp.

◆ SetFromRawPtr_()

template<class T >
SmartPtr< T > & Ipopt::SmartPtr< T >::SetFromRawPtr_ ( T rhs)
private

Set the value of the internal raw pointer from another raw pointer, releasing the previously referenced object if necessary.

Definition at line 591 of file IpSmartPtr.hpp.

◆ SetFromSmartPtr_()

template<class T >
SmartPtr< T > & Ipopt::SmartPtr< T >::SetFromSmartPtr_ ( const SmartPtr< T > &  rhs)
private

Set the value of the internal raw pointer from a SmartPtr, releasing the previously referenced object if necessary.

Definition at line 614 of file IpSmartPtr.hpp.

◆ ReleasePointer_()

template<class T >
void Ipopt::SmartPtr< T >::ReleasePointer_ ( )
private

Release the currently referenced object.

Definition at line 630 of file IpSmartPtr.hpp.

Friends And Related Symbol Documentation

◆ operator== [1/3]

template<class T >
template<class U1 , class U2 >
bool operator== ( const SmartPtr< U1 > &  lhs,
const SmartPtr< U2 > &  rhs 
)
friend

Overloaded equality comparison operator, allows the user to compare the value of two SmartPtrs.

Definition at line 716 of file IpSmartPtr.hpp.

◆ operator== [2/3]

template<class T >
template<class U1 , class U2 >
bool operator== ( const SmartPtr< U1 > &  lhs,
U2 raw_rhs 
)
friend

Overloaded equality comparison operator, allows the user to compare the value of a SmartPtr with a raw pointer.

Definition at line 733 of file IpSmartPtr.hpp.

◆ operator== [3/3]

template<class T >
template<class U1 , class U2 >
bool operator== ( U1 lhs,
const SmartPtr< U2 > &  raw_rhs 
)
friend

Overloaded equality comparison operator, allows the user to compare the value of a raw pointer with a SmartPtr.

Definition at line 749 of file IpSmartPtr.hpp.

◆ operator!= [1/3]

template<class T >
template<class U1 , class U2 >
bool operator!= ( const SmartPtr< U1 > &  lhs,
const SmartPtr< U2 > &  rhs 
)
friend

Overloaded in-equality comparison operator, allows the user to compare the value of two SmartPtrs.

Definition at line 765 of file IpSmartPtr.hpp.

◆ operator!= [2/3]

template<class T >
template<class U1 , class U2 >
bool operator!= ( const SmartPtr< U1 > &  lhs,
U2 raw_rhs 
)
friend

Overloaded in-equality comparison operator, allows the user to compare the value of a SmartPtr with a raw pointer.

Definition at line 781 of file IpSmartPtr.hpp.

◆ operator!= [3/3]

template<class T >
template<class U1 , class U2 >
bool operator!= ( U1 lhs,
const SmartPtr< U2 > &  raw_rhs 
)
friend

Overloaded in-equality comparison operator, allows the user to compare the value of a SmartPtr with a raw pointer.

Definition at line 797 of file IpSmartPtr.hpp.

◆ operator<

template<class T >
template<class U >
bool operator< ( const SmartPtr< U > &  lhs,
const SmartPtr< U > &  rhs 
)
friend

Overloaded less-than comparison operator, allows the user to compare the value of two SmartPtrs.

◆ GetRawPtr

template<class T >
template<class U >
U * GetRawPtr ( const SmartPtr< U > &  smart_ptr)
friend

Returns the raw pointer contained.

Use to get the value of the raw ptr (i.e. to pass to other methods/functions, etc.) Note: This method does NOT copy, therefore, modifications using this value modify the underlying object contained by the SmartPtr, NEVER delete this returned value.

Definition at line 649 of file IpSmartPtr.hpp.

◆ ConstPtr

template<class T >
template<class U >
SmartPtr< const U > ConstPtr ( const SmartPtr< U > &  smart_ptr)
friend

Returns a const pointer.

Definition at line 663 of file IpSmartPtr.hpp.

◆ IsValid

template<class T >
template<class U >
bool IsValid ( const SmartPtr< U > &  smart_ptr)
friend

Returns true if the SmartPtr is NOT NULL.

Use this to check if the SmartPtr is not NULL. This is preferred to if(GetRawPtr(sp) != NULL)

Definition at line 672 of file IpSmartPtr.hpp.

◆ IsNull

template<class T >
template<class U >
bool IsNull ( const SmartPtr< U > &  smart_ptr)
friend

Returns true if the SmartPtr is NULL.

Use this to check if the SmartPtr is NULL. This is preferred to if(GetRawPtr(sp) == NULL)

Definition at line 680 of file IpSmartPtr.hpp.

Member Data Documentation

◆ ptr_

template<class T >
T* Ipopt::SmartPtr< T >::ptr_
private

Actual raw pointer to the object.

Definition at line 355 of file IpSmartPtr.hpp.


The documentation for this class was generated from the following file: