2004-12-11 16:33:17 -05:00
|
|
|
\section{Runtime class information (aka RTTI) overview}\label{runtimeclassoverview}
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
Classes: \helpref{wxObject}{wxobject}, \helpref{wxClassInfo}{wxclassinfo}.
|
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
One of the failings of C++ used to be that no run-time information was provided
|
1998-05-20 10:25:30 -04:00
|
|
|
about a class and its position in the inheritance hierarchy.
|
1999-06-07 14:26:42 -04:00
|
|
|
Another, which still persists, is that instances of a class cannot be created
|
|
|
|
just by knowing the name of a class, which makes facilities such as persistent
|
|
|
|
storage hard to implement.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
Most C++ GUI frameworks overcome these limitations by means of a set of
|
2004-05-04 04:27:20 -04:00
|
|
|
macros and functions and wxWidgets is no exception. As it originated before the
|
2004-12-11 16:33:17 -05:00
|
|
|
addition of RTTI to the C++ standard and as support for it is still missing from
|
2004-05-04 04:27:20 -04:00
|
|
|
some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides its
|
1999-06-07 14:26:42 -04:00
|
|
|
own macro-based RTTI system.
|
|
|
|
|
|
|
|
In the future, the standard C++ RTTI will be used though and you're encouraged
|
2004-09-03 22:44:43 -04:00
|
|
|
to use whenever possible the \helpref{wxDynamicCast()}{wxdynamiccast} macro which,
|
1999-06-07 14:26:42 -04:00
|
|
|
for the implementations that support it, is defined just as dynamic\_cast<> and
|
2004-05-04 04:27:20 -04:00
|
|
|
uses wxWidgets RTTI for all the others. This macro is limited to wxWidgets
|
1999-06-07 14:26:42 -04:00
|
|
|
classes only and only works with pointers (unlike the real dynamic\_cast<> which
|
2000-07-15 15:51:35 -04:00
|
|
|
also accepts references).
|
1999-06-07 14:26:42 -04:00
|
|
|
|
2004-09-03 22:44:43 -04:00
|
|
|
Each class that you wish to be known to the type system should have
|
1998-05-20 10:25:30 -04:00
|
|
|
a macro such as DECLARE\_DYNAMIC\_CLASS just inside the class declaration.
|
|
|
|
The macro IMPLEMENT\_DYNAMIC\_CLASS should be in the implementation file.
|
1998-11-21 10:40:35 -05:00
|
|
|
Note that these are entirely optional; use them if you wish to check object
|
|
|
|
types, or create instances of classes using the class name. However,
|
|
|
|
it is good to get into the habit of adding these macros for all classes.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
2002-04-08 12:46:27 -04:00
|
|
|
Variations on these \helpref{macros}{rttimacros} are used for multiple inheritance, and abstract
|
1998-05-20 10:25:30 -04:00
|
|
|
classes that cannot be instantiated dynamically or otherwise.
|
|
|
|
|
|
|
|
DECLARE\_DYNAMIC\_CLASS inserts a static wxClassInfo declaration into the
|
|
|
|
class, initialized by IMPLEMENT\_DYNAMIC\_CLASS. When initialized, the
|
|
|
|
wxClassInfo object inserts itself into a linked list (accessed through
|
|
|
|
wxClassInfo::first and wxClassInfo::next pointers). The linked list
|
|
|
|
is fully created by the time all global initialisation is done.
|
|
|
|
|
|
|
|
IMPLEMENT\_DYNAMIC\_CLASS is a macro that not only initialises the static
|
|
|
|
wxClassInfo member, but defines a global function capable of creating a
|
|
|
|
dynamic object of the class in question. A pointer to this function is
|
|
|
|
stored in wxClassInfo, and is used when an object should be created
|
|
|
|
dynamically.
|
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
\helpref{wxObject::IsKindOf}{wxobjectiskindof} uses the linked list of
|
|
|
|
wxClassInfo. It takes a wxClassInfo argument, so use CLASSINFO(className)
|
|
|
|
to return an appropriate wxClassInfo pointer to use in this function.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
The function \helpref{wxCreateDynamicObject}{wxcreatedynamicobject} can be used
|
|
|
|
to construct a new object of a given type, by supplying a string name.
|
|
|
|
If you have a pointer to the wxClassInfo object instead, then you
|
2004-09-03 22:44:43 -04:00
|
|
|
can simply call \helpref{wxClassInfo::CreateObject}{wxclassinfocreateobject}.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
\subsection{wxClassInfo}\label{wxclassinfooverview}
|
|
|
|
|
2004-12-11 16:33:17 -05:00
|
|
|
\overview{Runtime class information (aka RTTI) overview}{runtimeclassoverview}
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
Class: \helpref{wxClassInfo}{wxclassinfo}
|
|
|
|
|
|
|
|
This class stores meta-information about classes. An application
|
|
|
|
may use macros such as DECLARE\_DYNAMIC\_CLASS and IMPLEMENT\_DYNAMIC\_CLASS
|
|
|
|
to record run-time information about a class, including:
|
|
|
|
|
|
|
|
\begin{itemize}\itemsep=0pt
|
|
|
|
\item its position in the inheritance hierarchy;
|
|
|
|
\item the base class name(s) (up to two base classes are permitted);
|
|
|
|
\item a string representation of the class name;
|
|
|
|
\item a function that can be called to construct an instance of this class.
|
|
|
|
\end{itemize}
|
|
|
|
|
|
|
|
The DECLARE\_... macros declare a static wxClassInfo variable in a class, which is initialized
|
|
|
|
by macros of the form IMPLEMENT\_... in the implementation C++ file. Classes whose instances may be
|
|
|
|
constructed dynamically are given a global constructor function which returns a new object.
|
|
|
|
|
|
|
|
You can get the wxClassInfo for a class by using the CLASSINFO macro, e.g. CLASSINFO(wxFrame).
|
|
|
|
You can get the wxClassInfo for an object using wxObject::GetClassInfo.
|
|
|
|
|
|
|
|
See also \helpref{wxObject}{wxobject} and \helpref{wxCreateDynamicObject}{wxcreatedynamicobject}.
|
|
|
|
|
2004-09-20 07:21:04 -04:00
|
|
|
\subsection{Example}\label{runtimeclassinformationexample}
|
1998-05-20 10:25:30 -04:00
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
In a header file frame.h:
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
1999-06-07 14:26:42 -04:00
|
|
|
class wxFrame : public wxWindow
|
1998-05-20 10:25:30 -04:00
|
|
|
{
|
1999-06-07 14:26:42 -04:00
|
|
|
DECLARE_DYNAMIC_CLASS(wxFrame)
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxString m_title;
|
1998-05-20 10:25:30 -04:00
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
public:
|
|
|
|
...
|
1998-05-20 10:25:30 -04:00
|
|
|
};
|
|
|
|
\end{verbatim}
|
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
In a C++ file frame.cpp:
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
|
|
|
|
|
1999-06-07 14:26:42 -04:00
|
|
|
wxFrame::wxFrame()
|
1998-05-20 10:25:30 -04:00
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
|