1 \section{\class{wxScopedPtr
}}\label{wxscopedptr
}
3 This is a simple scoped smart pointer implementation that is similar to
4 the
\urlref{Boost
}{http://www.boost.org/
} smart pointers but rewritten to
7 Since wxWidgets
2.9.0 there is also a templated version of this class
8 with the same name. See
\helpref{wxScopedPtr<T>
}{wxscopedptrtemplate
}.
10 A smart pointer holds a pointer to an object. The memory used by the object is
11 deleted when the smart pointer goes out of scope. This class is different from
12 the
\texttt{std::auto
\_ptr<>
} in so far as it doesn't provide copy constructor
13 nor assignment operator. This limits what you can do with it but is much less
14 surprizing than the ``destructive copy'' behaviour of the standard class.
18 Below is an example of using a wxWidgets scoped smart pointer and
22 class MyClass
{ /* ... */
};
24 // declare a smart pointer to a MyClass called wxMyClassPtr
25 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
26 // declare a smart pointer to an array of chars
27 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
31 // define the first pointer class, must be complete
32 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
33 // define the second pointer class
34 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
36 // create an object with a new pointer to MyClass
37 wxMyClassPtr theObj(new MyClass());
38 // reset the pointer (deletes the previous one)
39 theObj.reset(new MyClass());
44 // create an object with a new array of chars
45 wxCharArray theCharObj(new char
[100]);
51 \wxheading{Declaring new smart pointer types
}
53 To declare the smart pointer class
\texttt{CLASSNAME
} containing pointes to a
54 (possibly incomplete) type
\texttt{TYPE
} you should use
57 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
58 CLASSNAME ); // name of the class
61 And later, when
\texttt{TYPE
} is fully defined, you must also use
64 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
66 to implement the scoped pointer class.
68 The first argument of these macro is the pointer type, the second is the name
69 of the new smart pointer class being created. Below we will use wxScopedPtr to
70 represent the scoped pointer class, but the user may create the class with any
73 Alternatively, if you don't have to separate the point of declaration and
74 definition of this class and if you accept the standard naming convention, that
75 is that the scoped pointer for the class
\texttt{Foo
} is called
76 \texttt{FooPtr
}, you can use a single macro which replaces two macros above:
79 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
82 Once again, in this cass
\texttt{CLASSNAME
} will be
\texttt{TYPEPtr
}.
84 \wxheading{Include files
}
90 \helpref{wxScopedArray
}{wxscopedarray
}\rtfsp
92 \latexignore{\rtfignore{\wxheading{Members
}}}
94 \membersection{wxScopedPtr::wxScopedPtr
}\label{wxscopedptrctor
}
96 \func{}{explicit wxScopedPtr
}{\param{type
}{ * T = NULL
}}
98 Creates the smart pointer with the given pointer or none if
{\tt NULL
}. On
99 compilers that support it, this uses the explicit keyword.
102 \membersection{wxScopedPtr::
\destruct{wxScopedPtr
}}\label{wxscopedptrdtor
}
104 \func{}{\destruct{wxScopedPtr
}}{\void}
106 Destructor frees the pointer help by this object if it is not
{\tt NULL
}.
109 \membersection{wxScopedPtr::release
}\label{wxscopedptrrelease
}
111 \func{T *
}{release
}{\void}
113 Returns the currently hold pointer and resets the smart pointer object to
114 {\tt NULL
}. After a call to this function the caller is responsible for
115 deleting the pointer.
118 \membersection{wxScopedPtr::reset
}\label{wxscopedptrreset
}
120 \func{\void}{reset
}{\param{T
}{ p * = NULL
}}
122 Deletes the currently held pointer and sets it to
{\it p
} or to NULL if no
123 arguments are specified. This function does check to make sure that the
124 pointer you are assigning is not the same pointer that is already stored.
127 \membersection{wxScopedPtr::operator *
}\label{wxscopedptrptr
}
129 \func{const T\&
}{operator *
}{\void}
131 This operator works like the standard C++ pointer operator to return the object
132 being pointed to by the pointer. If the pointer is NULL or invalid this will
136 \membersection{wxScopedPtr::operator -$>$
}\label{wxscopedptrref
}
138 \func{const T*
}{operator -$>$
}{\void} % TODO
140 This operator works like the standard C++ pointer operator to return the pointer
141 in the smart pointer or NULL if it is empty.
144 \membersection{wxScopedPtr::get
}\label{wxscopedptrget
}
146 \func{const T*
}{get
}{\void}
148 This operator gets the pointer stored in the smart pointer or returns NULL if
152 \membersection{wxScopedPtr::swap
}\label{wxscopedptrswap
}
154 \func{\void}{swap
}{\param{wxScopedPtr
}{ \& other
}}
156 Swap the pointer inside the smart pointer with
{\it other
}. The pointer being
157 swapped must be of the same type (hence the same class name).
162 %%%%%%% wxScopedTiedPtr %%%%%%%
163 \section{\class{wxScopedTiedPtr
}}\label{wxscopedtiedptr
}
165 This is a variation on the topic of
\helpref{wxScopedPtr
}{wxscopedptr
}. This
166 class is also a smart pointer but in addition it ``ties'' the pointer value to
167 another variable. In other words, during the life time of this class the value
168 of that variable is set to be the same as the value of the pointer itself and
169 it is reset to its old value when the object is destroyed. This class is
170 especially useful when converting the existing code (which may already store
171 the pointers value in some variable) to the smart pointers.
175 \wxheading{Derives from
}
177 \helpref{wxScopedPtr
}{wxscopedptr
}
179 \wxheading{Include files
}
183 \latexignore{\rtfignore{\wxheading{Members
}}}
185 \membersection{wxScopedTiedPtr::wxScopedTiedPtr
}\label{wxscopedtiedptrctor
}
187 \func{}{wxScopedTiedPtr
}{\param{T **
}{ppTie
},
\param{T *
}{ptr
}}
189 Constructor creates a smart pointer initialized with
\arg{ptr
} and stores
190 \arg{ptr
} in the location specified by
\arg{ppTie
} which must not be
193 \membersection{wxScopedTiedPtr::
\destruct{wxScopedTiedPtr
}}\label{wxscopedtiedptrdtor
}
195 \func{}{\destruct{wxScopedTiedPtr
}}{\void}
197 Destructor frees the pointer help by this object and restores the value stored
198 at the tied location (as specified in the
\helpref{constructor
}{wxscopedtiedptrctor
})
201 Warning: this location may now contain an uninitialized value if it hadn't been
202 initialized previously, in particular don't count on it magically being