2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2009-02-03 07:01:46 -05:00
|
|
|
// Name: wx/scopedptr.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxScopedPtr
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxScopedPtr
|
2008-03-08 09:43:31 -05:00
|
|
|
|
|
|
|
This is a simple scoped smart pointer implementation that is similar to
|
2008-04-10 17:16:38 -04:00
|
|
|
the Boost smart pointers (see http://www.boost.org) but rewritten
|
|
|
|
to use macros instead.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Since wxWidgets 2.9.0 there is also a templated version of this class
|
2008-04-07 06:37:32 -04:00
|
|
|
with the same name. See wxScopedPtr<T>.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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
|
2008-04-10 17:16:38 -04:00
|
|
|
the @c std::auto_ptr<> in so far as it doesn't provide copy constructor
|
2008-03-08 08:52:38 -05:00
|
|
|
nor assignment operator. This limits what you can do with it but is much less
|
2010-01-14 13:02:26 -05:00
|
|
|
surprising than the "destructive copy" behaviour of the standard class.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-10 17:16:38 -04:00
|
|
|
@b Example:
|
|
|
|
|
|
|
|
Below is an example of using a wxWidgets scoped smart pointer and pointer array.
|
|
|
|
|
|
|
|
@code
|
|
|
|
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] = "!";
|
|
|
|
@endcode
|
|
|
|
|
2008-10-19 11:30:20 -04:00
|
|
|
@section scopedptr_newpointers Declaring new smart pointer types
|
2008-04-10 17:16:38 -04:00
|
|
|
|
2013-10-26 14:51:16 -04:00
|
|
|
To declare the smart pointer class @c CLASSNAME containing pointer to
|
2008-04-10 17:16:38 -04:00
|
|
|
a (possibly incomplete) type @c TYPE you should use
|
|
|
|
@code
|
|
|
|
wxDECLARE_SCOPED_PTR( TYPE, // type of the values
|
|
|
|
CLASSNAME ); // name of the class
|
|
|
|
@endcode
|
|
|
|
And later, when @c TYPE is fully defined, you must also use
|
|
|
|
@code
|
|
|
|
wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
|
|
|
|
@endcode
|
|
|
|
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 @c Foo is called @c FooPtr,
|
|
|
|
you can use a single macro which replaces two macros above:
|
|
|
|
@code
|
|
|
|
wxDEFINE_SCOPED_PTR_TYPE( TYPE );
|
|
|
|
@endcode
|
|
|
|
Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
2008-04-10 17:16:38 -04:00
|
|
|
@category{smartpointers}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxScopedArray
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxScopedPtr
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
Creates the smart pointer with the given pointer or none if @NULL.
|
|
|
|
|
|
|
|
On compilers that support it, this uses the explicit keyword.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-10 17:16:38 -04:00
|
|
|
explicit wxScopedPtr(type* T = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor frees the pointer help by this object if it is not @NULL.
|
|
|
|
*/
|
|
|
|
~wxScopedPtr();
|
|
|
|
|
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
This operator gets the pointer stored in the smart pointer or returns
|
|
|
|
@NULL if there is none.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2010-07-25 15:45:24 -04:00
|
|
|
T* get() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
This operator works like the standard C++ pointer operator to return the object
|
2008-04-10 17:16:38 -04:00
|
|
|
being pointed to by the pointer.
|
|
|
|
|
2010-07-25 15:45:24 -04:00
|
|
|
If the internal pointer is @NULL this method will cause an assert in debug mode.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2010-07-25 15:45:24 -04:00
|
|
|
T& operator *() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2010-07-25 15:45:24 -04:00
|
|
|
Smart pointer member access. Returns pointer to its object.
|
|
|
|
|
|
|
|
If the internal pointer is @NULL this method will cause an assert in debug mode.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2010-07-25 15:45:24 -04:00
|
|
|
T* operator ->() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Returns the currently hold pointer and resets the smart pointer object to
|
2008-04-10 17:16:38 -04:00
|
|
|
@NULL.
|
|
|
|
|
|
|
|
@remarks
|
|
|
|
After a call to this function the caller is responsible for deleting the
|
|
|
|
pointer.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
T* release();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Deletes the currently held pointer and sets it to @a p or to @NULL if no
|
2008-04-10 17:16:38 -04:00
|
|
|
arguments are specified.
|
|
|
|
|
|
|
|
@note
|
|
|
|
This function does check to make sure that the pointer you are assigning
|
|
|
|
is not the same pointer that is already stored.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-10 17:16:38 -04:00
|
|
|
reset(T* p = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
Swap the pointer inside the smart pointer with @a other. The pointer being
|
2008-03-08 08:52:38 -05:00
|
|
|
swapped must be of the same type (hence the same class name).
|
|
|
|
*/
|
2008-04-10 17:16:38 -04:00
|
|
|
swap(wxScopedPtr& other);
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxScopedTiedPtr
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-10 17:16:38 -04:00
|
|
|
This is a variation on the topic of wxScopedPtr. This class is also a smart pointer
|
|
|
|
but in addition it "ties" the pointer value to another variable. In other words,
|
|
|
|
during the life time of this class the value of that variable is set to be the same
|
|
|
|
as the value of the pointer itself and it is reset to its old value when the object
|
|
|
|
is destroyed. This class is especially useful when converting the existing code
|
|
|
|
(which may already store the pointers value in some variable) to the smart pointers.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
2008-04-10 17:16:38 -04:00
|
|
|
@category{smartpointers}
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-10 18:04:29 -04:00
|
|
|
class wxScopedTiedPtr : public wxScopedPtr
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Constructor creates a smart pointer initialized with @a ptr and stores
|
2008-04-10 17:16:38 -04:00
|
|
|
@a ptr in the location specified by @a ppTie which must not be @NULL.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
wxScopedTiedPtr(T** ppTie, T* ptr);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-09 12:42:52 -04:00
|
|
|
Destructor frees the pointer help by this object and restores the value
|
|
|
|
stored at the tied location (as specified in the @ref wxScopedTiedPtr() constructor)
|
2008-03-08 08:52:38 -05:00
|
|
|
to the old value.
|
2008-04-10 17:16:38 -04:00
|
|
|
|
|
|
|
@warning
|
|
|
|
This location may now contain an uninitialized value if it hadn't been
|
|
|
|
initialized previously, in particular don't count on it magically being @NULL!
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
~wxScopedTiedPtr();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-10-09 12:42:52 -04:00
|
|
|
A scoped pointer template class.
|
2009-01-27 11:40:51 -05:00
|
|
|
|
2008-10-09 12:42:52 -04:00
|
|
|
It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros".
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2009-01-27 11:40:51 -05:00
|
|
|
Notice that objects of this class intentionally cannot be copied.
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
2008-04-10 17:16:38 -04:00
|
|
|
@category{smartpointers}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-07 06:37:32 -04:00
|
|
|
@see wxSharedPtr<T>, wxWeakRef<T>
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-06 20:17:06 -04:00
|
|
|
template<typename T>
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxScopedPtr<T>
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2009-01-27 11:40:51 -05:00
|
|
|
Constructor takes ownership of the pointer.
|
|
|
|
|
|
|
|
@param ptr
|
|
|
|
Pointer allocated with @c new or @NULL.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-06 20:17:06 -04:00
|
|
|
wxScopedPtr(T* ptr = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2009-01-27 11:40:51 -05:00
|
|
|
Destructor deletes the pointer.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-06 20:17:06 -04:00
|
|
|
~wxScopedPtr();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns pointer to object or @NULL.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
T* get() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Conversion to a boolean expression (in a variant which is not
|
2009-01-27 11:40:51 -05:00
|
|
|
convertible to anything but a boolean expression).
|
2008-04-10 17:16:38 -04:00
|
|
|
|
|
|
|
If this class contains a valid pointer it will return @true, if it contains
|
|
|
|
a @NULL pointer it will return @false.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
operator unspecified_bool_type() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
Returns a reference to the object.
|
|
|
|
|
2010-07-25 15:45:24 -04:00
|
|
|
If the internal pointer is @NULL this method will cause an assert in debug mode.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2010-07-25 15:45:24 -04:00
|
|
|
T& operator*() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2019-01-30 11:28:08 -05:00
|
|
|
Smart pointer member access. Returns pointer to object.
|
|
|
|
|
2010-07-25 15:45:24 -04:00
|
|
|
If the internal pointer is @NULL this method will cause an assert in debug mode.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-10 17:16:38 -04:00
|
|
|
T* operator->() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Releases the current pointer and returns it.
|
2008-04-10 17:16:38 -04:00
|
|
|
|
|
|
|
@remarks
|
2008-03-08 08:52:38 -05:00
|
|
|
Afterwards the caller is responsible for deleting
|
|
|
|
the data contained in the scoped pointer before.
|
|
|
|
*/
|
|
|
|
T* release();
|
|
|
|
|
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
Reset pointer to the value of @a ptr.
|
|
|
|
The previous pointer will be deleted.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void reset(T* ptr = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Swaps pointers.
|
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void swap(wxScopedPtr<T>& ot);
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|