wxScopedPtr

This is a simple scoped smart pointer implementation that is similar to the Boost (#2) smart pointers but rewritten to use macros instead.

A smart pointer holds a pointer to an object. The memory used by the object is deleted when the smart pointer goes out of scope. This class is different from the std::auto_ptr<> in so far as it doesn't provide copy constructor nor assignment operator. This limits what you can do with it but is much less surprizing than the ``destructive copy'' behaviour of the standard class.

Example

Below is an example of using a wxWidgets scoped smart pointer and pointer array.

  class MyClass { /* ... */ };

  // declare a smart pointer to a MyClass called wxMyClassPtr
  wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
  // declare a smart pointer to an array of chars
  wxDECLARE_SCOPED_ARRAY(char, wxCharArray)

  ...

  // define the first pointer class, must be complete
  wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
  // define the second pointer class
  wxDEFINE_SCOPED_ARRAY(char, wxCharArray)

  // create an object with a new pointer to MyClass
  wxMyClassPtr theObj(new MyClass());
  // reset the pointer (deletes the previous one)
  theObj.reset(new MyClass());

  // access the pointer
  theObj->MyFunc();

  // create an object with a new array of chars
  wxCharArray theCharObj(new char[100]);

  // access the array
  theCharObj[0] = "!";

Declaring new smart pointer types

To declare the smart pointer class CLASSNAME containing pointes to a (possibly incomplete) type TYPE you should use

    wxDECLARE_SCOPED_PTR( TYPE,     // type of the values
                                CLASSNAME ); // name of the class

And later, when TYPE is fully defined, you must also use

    wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
to implement the scoped pointer class.

The first argument of these macro is the pointer type, the second is the name of the new smart pointer class being created. Below we will use wxScopedPtr to represent the scoped pointer class, but the user may create the class with any legal name.

Alternatively, if you don't have to separate the point of declaration and definition of this class and if you accept the standard naming convention, that is that the scoped pointer for the class Foo is called FooPtr, you can use a single macro which replaces two macros above:

    wxDEFINE_SCOPED_PTR_TYPE( TYPE );

Once again, in this cass CLASSNAME will be TYPEPtr.

Include files

<wx/ptr_scpd.h>

See also

wxScopedArray



wxScopedPtr::wxScopedPtr



explicit wxScopedPtr(type * T = NULL)

Creates the smart pointer with the given pointer or none if NULL. On compilers that support it, this uses the explicit keyword.



wxScopedPtr::~wxScopedPtr



~wxScopedPtr(void)

Destructor frees the pointer help by this object if it is not NULL.



wxScopedPtr::release



T * release(void)

Returns the currently hold pointer and resets the smart pointer object to NULL. After a call to this function the caller is responsible for deleting the pointer.



wxScopedPtr::reset



void reset(T p * = NULL)

Deletes the currently held pointer and sets it to p or to NULL if no arguments are specified. This function does check to make sure that the pointer you are assigning is not the same pointer that is already stored.



wxScopedPtr::operator *



const T& operator *(void)

This operator works like the standard C++ pointer operator to return the object being pointed to by the pointer. If the pointer is NULL or invalid this will crash.



wxScopedPtr::operator -$>$



const T* operator -$>$(void)

This operator works like the standard C++ pointer operator to return the pointer in the smart pointer or NULL if it is empty.



wxScopedPtr::get



const T* get(void)

This operator gets the pointer stored in the smart pointer or returns NULL if there is none.



wxScopedPtr::swap



void swap(wxScopedPtr & other)

Swap the pointer inside the smart pointer with other. The pointer being swapped must be of the same type (hence the same class name).

ymasuda 平成17年11月19日