Providing Run-Time Type Information

team bbl


In common with most frameworks, wxWidgets provides methods to define more RTTI than C++'s own RTTI. This is useful for making run-time decisions based on an object's type and for error reporting as we have seen in the last section, and it also enables you to create objects dynamically simply by providing a string containing the name of the class. Only classes that have wxObject as an ancestor class can have wxWidgets RTTI.

If you don't need the dynamic creation ability, use the macro DECLARE_CLASS(class) in the class declaration and IMPLEMENT_CLASS(class, base Class) in the class implementation file. If you need dynamic creation, use DECLARE_DYNAMIC_CLASS(class) in the class declaration and IMPLEMENT_DYNAMIC_ CLASS(class, baseClass) in the class implementation file. In the dynamic case, you will also need to make sure there is a default constructor for the class; otherwise the compiler will complain when it comes across the function that wxWidgets generates to create an object of this class.

Here's an example of using RTTI to allow dynamic creation of objects.

 class MyRecord: public wxObject { DECLARE_DYNAMIC_CLASS(MyRecord) public:     MyRecord() {}     MyRecord(const wxString& name) { m_name = name; }     void SetName(const wxString& name) { m_name = name; }     const wxString& GetName() const { return m_name; } private:     wxString m_name; }; IMPLEMENT_DYNAMIC_CLASS(MyRecord, wxObject) MyRecord* CreateMyRecord(const wxString& name) {     MyRecord* rec = wxDynamicCast(wxCreateDynamicObject(wxT("MyRecord")), MyRecord);     if (rec)         rec->SetName(name);     return rec; } 

When code calls CreateMyRecord with the name to be set, wxCreateDynamicObject creates the object, and wxDynamicCast confirms that it really is an object of type MyRecordit will return NULL if not. Although it might not appear useful at first sight, dynamic object creation is very handy when loading a complex file containing objects of different types. An object's data can be stored along with the name of its class, and when the file is read back, a new instance of the class can be created and then the object can read in its data.

There are other RTTI macros you can use, as follows.

CLASSINFO(class) returns a pointer to the wxClassInfo object associated with a class. You can use it with wxObject::IsKindOf to test the type of a class:

 if (obj->IsKindOf(CLASSINFO(MyRecord))) {     ... } 

Use DECLARE_ABSTRACT_CLASS(class) and IMPLEMENT_ABSTRACT_CLASS(class, baseClass) with abstract classes.

Use DECLARE_CLASS2(class) and IMPLEMENT_CLASS2(class, baseClass1, baseClass2) where there are two base classes.

Use DECLARE_APP(class) and IMPLEMENT_APP(class) to make the application class known to wxWidgets.

wxConstCast(ptr, class) is a macro that expands into const_cast<class *>(ptr) if the compiler supports const_cast or into an old, C-style cast otherwise.

wxDynamicCastThis(class) is equivalent to wxDynamicCast(this, class), but the latter provokes spurious compilation warnings from some compilers (because it tests whether this pointer is non-NULL, which is always true), so this macro should be used to avoid them.

wxStaticCast(ptr, class) checks that the cast is valid in debug mode (an assert failure will result if wxDynamicCast(ptr, class) == NULL) and then returns the result of executing an equivalent of static_cast<class*>(ptr).

wx_const_cast(T, x) is the same as const_cast<T>(x) if the compiler supports const cast or (T)x for old compilers. Unlike wxConstCast, this casts it to the type T, not to T*, and also the order of arguments is the same as for the standard cast.

wx_reinterpret_cast(T, x) is the same as reinterpret_cast<T>(x) if the compiler supports reinterpret cast or (T)x for old compilers.

wx_static_cast(T, x) is the same as static_cast<T>(x) if the compiler supports static cast or (T)x for old compilers. Unlike wxStaticCast, no checks are done, and the meaning of the macro arguments is exactly the same as for the standard static castthat is, T is the full type name and a * is not appended to it.

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net