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 A smart pointer holds a pointer to an object. The memory used by the object is
8 deleted when the smart pointer goes out of scope. This class is different from
9 the
\texttt{std::auto
\_ptr<>
} in so far as it doesn't provide copy constructor
10 nor assignment operator. This limits what you can do with it but is much less
11 surprizing than the ``destructive copy'' behaviour of the standard class.
15 Below is an example of using a wxWidgets scoped smart pointer and
19 class MyClass
{ /* ... */
};
21 // declare a smart pointer to a MyClass called wxMyClassPtr
22 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
23 // declare a smart pointer to an array of chars
24 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
28 // define the first pointer class, must be complete
29 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
30 // define the second pointer class
31 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
33 // create an object with a new pointer to MyClass
34 wxMyClassPtr theObj(new MyClass());
35 // reset the pointer (deletes the previous one)
36 theObj.reset(new MyClass());
41 // create an object with a new array of chars
42 wxCharArray theCharObj(new char
[100]);
48 \wxheading{Declaring new smart pointer types
}
50 To declare the smart pointer class
\texttt{CLASSNAME
} containing pointes to a
51 (possibly incomplete) type
\texttt{TYPE
} you should use
54 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
55 CLASSNAME ); // name of the class
58 And later, when
\texttt{TYPE
} is fully defined, you must also use
61 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
63 to implement the scoped pointer class.
65 The first argument of these macro is the pointer type, the second is the name
66 of the new smart pointer class being created. Below we will use wxScopedPtr to
67 represent the scoped pointer class, but the user may create the class with any
70 Alternatively, if you don't have to separate the point of declaration and
71 definition of this class and if you accept the standard naming convention, that
72 is that the scoped pointer for the class
\texttt{Foo
} is called
73 \texttt{FooPtr
}, you can use a single macro which replaces two macros above:
76 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
79 Once again, in this cass
\texttt{CLASSNAME
} will be
\texttt{TYPEPtr
}.
81 \wxheading{Include files
}
87 \helpref{wxScopedArray
}{wxscopedarray
}\rtfsp
89 \latexignore{\rtfignore{\wxheading{Members
}}}
91 \membersection{wxScopedPtr::wxScopedPtr
}
93 \func{}{explicit wxScopedPtr
}{\param{type
}{ * T = NULL
}}
95 Creates the smart pointer with the given pointer or none if
{\tt NULL
}. On
96 compilers that support it, this uses the explicit keyword.
99 \membersection{wxScopedPtr::
\destruct{wxScopedPtr
}}
101 \func{}{\destruct{wxScopedPtr
}}{\void}
103 Destructor frees the pointer help by this object if it is not
{\tt NULL
}.
106 \membersection{wxScopedPtr::release
}
108 \func{T *
}{release
}{\void}
110 Returns the currently hold pointer and resets the smart pointer object to
111 {\tt NULL
}. After a call to this function the caller is responsible for
112 deleting the pointer.
115 \membersection{wxScopedPtr::reset
}
117 \func{\void}{reset
}{\param{T
}{ p * = NULL
}}
119 Deletes the currently held pointer and sets it to
{\it p
} or to NULL if no
120 arguments are specified. This function does check to make sure that the
121 pointer you are assigning is not the same pointer that is already stored.
124 \membersection{wxScopedPtr::operator *
}
126 \func{const T\&
}{operator *
}{\void}
128 This operator works like the standard C++ pointer operator to return the object
129 being pointed to by the pointer. If the pointer is NULL or invalid this will
133 \membersection{wxScopedPtr::operator -$>$
} % TODO
135 \func{const T*
}{operator -$>$
}{\void} % TODO
137 This operator works like the standard C++ pointer operator to return the pointer
138 in the smart pointer or NULL if it is empty.
141 \membersection{wxScopedPtr::get
}
143 \func{const T*
}{get
}{\void}
145 This operator gets the pointer stored in the smart pointer or returns NULL if
149 \membersection{wxScopedPtr::swap
}
151 \func{\void}{swap
}{\param{wxScopedPtr
}{ \& other
}}
153 Swap the pointer inside the smart pointer with
{\it other
}. The pointer being
154 swapped must be of the same type (hence the same class name).
159 %%%%%%% wxScopedTiedPtr %%%%%%%
160 \section{\class{wxScopedTiedPtr
}}\label{wxscopedtiedptr
}
162 This is a variation on the topic of
\helpref{wxScopedPtr
}{wxscopedptr
}. This
163 class is also a smart pointer but in addition it ``ties'' the pointer value to
164 another variable. In other words, during the life time of this class the value
165 of that variable is set to be the same as the value of the pointer itself and
166 it is reset to its old value when the object is destroyed. This class is
167 especially useful when converting the existing code (which may already store
168 the pointers value in some variable) to the smart pointers.
172 \wxheading{Derives from
}
174 \helpref{wxScopedPtr
}{wxscopedptr
}
176 \wxheading{Include files
}
180 \latexignore{\rtfignore{\wxheading{Members
}}}
182 \membersection{wxScopedTiedPtr::wxScopedTiedPtr
}\label{wxscopedtiedptrctor
}
184 \func{}{wxScopedTiedPtr
}{\param{T **
}{ppTie
},
\param{T *
}{ptr
}}
186 Constructor creates a smart pointer initialized with
\arg{ptr
} and stores
187 \arg{ptr
} in the location specified by
\arg{ppTie
} which must not be
190 \membersection{wxScopedTiedPtr::
\destruct{wxScopedTiedPtr
}}\label{wxscopedtiedptrdtor
}
192 \func{}{\destruct{wxScopedTiedPtr
}}{\void}
194 Destructor frees the pointer help by this object and restores the value stored
195 at the tied location (as specified in the
\helpref{constructor
}{wxscopedtiedptrctor
})
198 Warning: this location may now contain an uninitialized value if it hadn't been
199 initialized previously, in particular don't count on it magically being