2008-02-18 19:04:03 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2008-02-27 01:48:16 -05:00
|
|
|
// Name: runtimeclass.h
|
2008-02-18 19:04:03 -05:00
|
|
|
// Purpose: topic overview
|
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-02-18 19:04:03 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-03-12 04:50:42 -04:00
|
|
|
/**
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-11 03:57:21 -04:00
|
|
|
@page overview_rtti Runtime Type Information (RTTI)
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2012-11-03 14:34:10 -04:00
|
|
|
@tableofcontents
|
2008-02-27 01:48:16 -05:00
|
|
|
|
2008-03-11 03:57:21 -04:00
|
|
|
One of the failings of C++ used to be that no runtime information was provided
|
2008-02-27 01:48:16 -05:00
|
|
|
about a class and its position in the inheritance hierarchy. 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.
|
|
|
|
|
|
|
|
Most C++ GUI frameworks overcome these limitations by means of a set of macros
|
|
|
|
and functions and wxWidgets is no exception. As it originated before the
|
|
|
|
addition of RTTI to the C++ standard and as support for it is still missing
|
|
|
|
from some (albeit old) compilers, wxWidgets doesn't (yet) use it, but provides
|
2011-04-30 06:57:04 -04:00
|
|
|
its own macro-based RTTI system.
|
2008-02-27 01:48:16 -05:00
|
|
|
|
|
|
|
In the future, the standard C++ RTTI will be used though and you're encouraged
|
|
|
|
to use whenever possible the wxDynamicCast macro which, for the implementations
|
|
|
|
that support it, is defined just as dynamic_cast and uses wxWidgets RTTI for
|
|
|
|
all the others. This macro is limited to wxWidgets classes only and only works
|
|
|
|
with pointers (unlike the real dynamic_cast which also accepts references).
|
|
|
|
|
|
|
|
Each class that you wish to be known to the type system should have a macro
|
2015-04-23 07:49:01 -04:00
|
|
|
such as wxDECLARE_DYNAMIC_CLASS just inside the class declaration. The macro
|
|
|
|
wxIMPLEMENT_DYNAMIC_CLASS should be in the implementation file. Note that these
|
2008-02-27 01:48:16 -05:00
|
|
|
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.
|
|
|
|
|
|
|
|
Variations on these macros are used for multiple inheritance, and abstract
|
|
|
|
classes that cannot be instantiated dynamically or otherwise.
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_DYNAMIC_CLASS inserts a static wxClassInfo declaration into the
|
|
|
|
class, initialized by wxIMPLEMENT_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.
|
2008-02-27 01:48:16 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static
|
2008-02-27 01:48:16 -05:00
|
|
|
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.
|
|
|
|
|
|
|
|
wxObject::IsKindOf 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.
|
|
|
|
|
|
|
|
The function 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 can simply call wxClassInfo::CreateObject.
|
|
|
|
|
2012-11-03 14:34:10 -04:00
|
|
|
@see wxObject
|
|
|
|
|
2008-02-27 01:48:16 -05:00
|
|
|
|
2008-03-03 07:22:53 -05:00
|
|
|
@section overview_rtti_classinfo wxClassInfo
|
2008-02-27 01:48:16 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
This class stores meta-information about classes. An application may use
|
|
|
|
macros such as wxDECLARE_DYNAMIC_CLASS and wxIMPLEMENT_DYNAMIC_CLASS to
|
|
|
|
record runtime information about a class, including:
|
2008-02-27 01:48:16 -05:00
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
@li Its position in the inheritance hierarchy.
|
2008-02-27 01:48:16 -05:00
|
|
|
@li The base class name(s) (up to two base classes are permitted).
|
|
|
|
@li A string representation of the class name.
|
|
|
|
@li A function that can be called to construct an instance of this class.
|
|
|
|
|
2015-09-07 00:07:06 -04:00
|
|
|
The wxDECLARE_... macros declare a static wxClassInfo variable in a class, which
|
|
|
|
is initialized by macros of the form wxIMPLEMENT_... in the implementation C++
|
2008-02-27 01:48:16 -05:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2008-03-03 07:22:53 -05:00
|
|
|
@section overview_rtti_example Example
|
2008-02-27 01:48:16 -05:00
|
|
|
|
|
|
|
In a header file frame.h:
|
|
|
|
|
|
|
|
@code
|
|
|
|
class wxFrame : public wxWindow
|
|
|
|
{
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_DYNAMIC_CLASS(wxFrame);
|
2008-02-27 01:48:16 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
wxString m_title;
|
|
|
|
|
|
|
|
public:
|
|
|
|
...
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
In a C++ file frame.cpp:
|
|
|
|
|
|
|
|
@code
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow);
|
2008-02-27 01:48:16 -05:00
|
|
|
|
|
|
|
wxFrame::wxFrame()
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
*/
|