[Classes] [Prev] [Next] [Methods]

CTypeInfo and CTypeBaseIterator

Methods Index

Heritage
Description
Usage
Environment
Private Data Members
Friends
Macros
Run-time Type Information Macros
Public Methods

Classes Derived from CTypeInfo: CTypeBaseIterator
Data Types
Private Data Members
Public Methods
Private Methods

CTypeInfo and CTypeBaseIterator


Heritage

Superclass: None

Subclasses: None


Description

This class provides methods that allow for run-time type checking of your objects. The main reasons for using run-time type checking are for implementing persistence of heterogeneous data files and maintaining object integrity. For example, if a method is passed an object, you need to check if the object's subclass is expected by the method. If the subclass has different methods than the expected superclass, you might want to use some of those methods. This makes run-time type checking of the object a very efficient technique.

See Also: For more information on runtime checking, see Bjarne Stroustrup's The C++ Programming Language, Second Edition, pages 442-452.


Usage

To add run-time type information to a class, you must use the PWRClassInfo and PWRRegisterClass macros as done in the following example for class CFoo:

FILE foo.h:

class CFoo : public CBar
{
PWRClassInfo

public:
... rest of class definition ...
};

FILE foo.cpp:

PWRRegisterClass1(CFoo, FooId, "CFoo", CBar);

... rest of class implementation ...
The "PWRRegisterClass" macros have the following interface:

	PwrRegisterMacro#(className, classNameString,
parent1, ..., parent#)

where # is the number of direct base classes of
the class.
To use type information in programs, the following additional macros are defined:

	PtrCast(type, pointer)
ExactPtrCast(type, pointer)

Environment

Used by all classes.


Private Data Members

const char* itsName The class name
const CTypeInfo** itsBases The class's superclasses
const int itsTypeId The class ID

Friends

friend class CTypeBaseIterator;


Macros

#define PWRClassInfo \
public: \
static const CTypeInfo itsTypeInfo; \
static const CTypeInfo** itsBases[]; \
virtual const CTypeInfo* GetRunTimeTypeInfo() const; \
static const CTypeInfo* GetStaticTypeInfo();
Class information to hold information for run time type ID.

#define PWRAddTypeMethods(theClass, theId, theClassName) \
const CTypeInfo theClass::itsTypeInfo(theId, theClassName,
theClass::itsBases); \
const CTypeInfo* theClass::GetRunTimeTypeInfo() const
{ return &theClass::itsTypeInfo; } \
const CTypeInfo* theClass::GetStaticTypeInfo()
{ return &theClass::itsTypeInfo; }
Adds the information to the internal data structure.

The following macros are to register classes for base checking:

#define PWRRegisterClass0(theClass, theId, theClassName) \
const CTypeInfo* theClass::itsBases[] = { 0 }; \
PWRAddTypeMethods(theClass, theId, theClassName)
#define PWRRegisterClass1(theClass, theId, theClassName, theBaseClass) \
const CTypeInfo* theClass::itsBases[] = \
{ &theBaseClass::itsTypeInfo, 0 }; \
PWRAddTypeMethods(theClass, theId, theClassName)
#define PWRRegisterClass2(theClass, theId, theClassName, b1, b2) \
const CTypeInfo* theClass::itsBases[] = \
{ &b1::itsTypeInfo, \
&b2::itsTypeInfo, 0 }; \
PWRAddTypeMethods(theClass, theId, theClassName)
#define PWRRegisterClass3(theClass, theId, theClassName, b1, b2, b3) \
const CTypeInfo* theClass::itsBases[] = \
{ &b1::itsTypeInfo, \
&b2::itsTypeInfo, \
&b3::itsTypeInfo, 0 }; \
PWRAddTypeMethods(theClass, theId, theClassName)
#define PWRRegisterClass4(theClass, theId, theClassName, b1, b2, b3, b4) \
const CTypeInfo* theClass::itsBases[] = \
{ &b1::itsTypeInfo, \
&b2::itsTypeInfo, \
&b3::itsTypeInfo, \
&b4::itsTypeInfo, 0 }; \
PWRAddTypeMethods(theClass, theId, theClassName)

Run-time Type Information Macros

#define ExactPtrCast(theType, thePointer);
#define PtrCast(theType, thePointer);
#define RTTIName GetRunTimeTypeInfo()->GetName();
#define RTTIIsA(__classname, __classptr) 
#define RTTIRogueWaveToPower(__classname, __classptr) 
	RWCollectable *aCol = doTo().find(...);
if (RTTIRogueWaveToPower(CPointRWC, aCol))
{
CPointRWC *aPoint = (CPointRWC*)aCol;
anOutStream << aPoint->RTTIName;
...
}

Note: To use the RTTIRogueWaveToPower macro, the ID must be within the valid range. If this is not the case, the result is undefined.


Public Methods

CTypeInfo(int theTypeId, 
const char* theClassName,
const CTypeInfo* theBases[]);
virtual ~CTypeInfo();
const char* GetName(void) const;
int GetId(void) const;
int IsSame(const CTypeInfo* theInfo) const;
int HasBase(const CTypeInfo* theBaseInfo) const;
int CanCast(const CTypeInfo* theInfo) const;
This class is free floating with no superclass. Therefore, there is no inherited functionality.


Classes Derived from CTypeInfo: CTypeBaseIterator


Data Types

typedef CTypeInfo * CTypeInfoPtr;
declare(RWGVector, CTypeInfoPtr)


Private Data Members

const CTypeInfo** itsBases Pointer to base type
int itsPosition Current position of the iterator
int itIsDeep; Determines whether the object has parents
size_t itsDeepSize; Determines the number of superclasses
size_t itsDeepIterator; Iterates the superclasses
RWGVector
(CTypeInfoPtr)
itsDeepVector; Contains a list of superclasses


Public Methods

CTypeBaseIterator(const CTypeInfo* theInfo, 
int deep = 1);
~CTypeBaseIterator(void);
const CTypeInfo* Next(void);
void Reset(void);

Private Methods

CTypeBaseIterator() {};
void BuildDeepTypeList(const CTypeInfo* theInfo);


[Classes] [Prev] [Next] [Methods]

support@xvt.com


Visit our online help desk for tech support

Copyright © 2009, Providence Software Solutions Inc. All rights reserved.