Click here to Skip to main content
15,891,597 members
Articles / Desktop Programming / MFC

Using the C++ RTTI/Reflection APIs in the VCF

Rate me:
Please Sign up or sign in to vote.
2.53/5 (11 votes)
4 Jun 200213 min read 85K   419   26  
A tutorial and brief explanation of RTTI in the VCF.
////RTTI.cpp

#include "FoundationKit.h"

using namespace VCF;


#define SIMPLECLASS_CLASSID		"FB685669-6D44-4ea7-8011-B513E3808002"

class SimpleClass : public VCF::Object {
public:
	BEGIN_CLASSINFO( SimpleClass, "SimpleClass", "VCF::Object", SIMPLECLASS_CLASSID )
	END_CLASSINFO( SimpleClass )

	SimpleClass(){};
	virtual ~SimpleClass(){ 

	}
};




#define LITTLELESSSIMPLECLASS_CLASSID		"F027BCD9-B8BF-4e52-A6E0-0EA3CC080B90"

class LittleLessSimpleClass : public SimpleClass {
public:
	BEGIN_CLASSINFO( LittleLessSimpleClass, "LittleLessSimpleClass", "SimpleClass", LITTLELESSSIMPLECLASS_CLASSID )
	PROPERTY( long, "size", LittleLessSimpleClass::getSize, LittleLessSimpleClass::setSize, pdLong )
	PROPERTY( bool, "cool", LittleLessSimpleClass::getCool, LittleLessSimpleClass::setCool, pdBool )
	PROPERTY( char, "char", LittleLessSimpleClass::getChar, LittleLessSimpleClass::setChar, pdChar )
	PROPERTY( String, "name", LittleLessSimpleClass::getName, LittleLessSimpleClass::setName, pdString )
	READONLY_OBJECT_PROPERTY( Object, "owner", LittleLessSimpleClass::owner )
	END_CLASSINFO( LittleLessSimpleClass )

	LittleLessSimpleClass():m_size(0),m_cool(true),m_char('f'),m_name("Howdy"),m_owner(NULL){};


	virtual ~LittleLessSimpleClass(){ 

	}


	long getSize() {
		return m_size;
	}

	void setSize( const long& size ) {
		m_size = size;
	}

	bool getCool() {
		return m_cool;
	}
	
	void setCool( const bool& cool ) {
		m_cool = cool;
	}

	char getChar() {
		return m_char;
	}
	
	void setChar( const char& ch ) {
		m_char = ch;
	}


	String getName() {
		return m_name;
	}
	
	void setName( const String& s ) {
		m_name = s;
	}

	Object* owner() {
		return m_owner;
	}
protected:
	long m_size;
	bool m_cool;
	char m_char;
	String m_name;
	Object* m_owner;
};





#define FooBarBazifier_CLASSID		"1658339E-5132-4007-A9B4-0DE58F89AEBE"


class FooBarBazifier : public Object {
public:
	BEGIN_CLASSINFO( FooBarBazifier, "FooBarBazifier", "VCF::Object", FooBarBazifier_CLASSID )
	METHOD_VOID( "printMe", FooBarBazifier, printMe, &FooBarBazifier::printMe )

	METHOD_2VOID( "add", FooBarBazifier, add, const double&, const double&, &FooBarBazifier::add, "dd" )

	METHOD_RETURN( "whereAmI", FooBarBazifier, whereAmI, String, &FooBarBazifier::whereAmI )

	METHOD_2RETURN( "addAndReturn", FooBarBazifier, addAndReturn, int, const double&, const double&, &FooBarBazifier::addAndReturn, "dd" )
	
	END_CLASSINFO( FooBarBazifier )

	FooBarBazifier() {

	}

	virtual ~FooBarBazifier() {

	}

	void printMe() {
		System::print( "print me!\n" );
	}                                                                 	

	String whereAmI() {
		return "Where am i ?";
	}

	void add( const double& d1, const double& d2 ) {
		System::print( "%.4f + %.4f = %.4f\n", d1, d2, d1+d2 );
	}

	int addAndReturn( const double& d1, const double& d2 ) {
		return (int)(d1 + d2);	
	}
};

void reportRTTIData( Object* object ) 
{
	Class* clazz = object->getClass();
	if ( NULL != clazz ) {
		System::print( "\n\nClass retreived, information: \n" );
		System::print( "Class name:\t%s\nSuper Class name:\t%s\n",
			clazz->getClassName().c_str(), clazz->getSuperClass()->getClassName().c_str() );
		
		System::print( "Number of properties known to RTTI: %d\n",
			clazz->getPropertyCount() );

		Enumerator<Property*>* properties = clazz->getProperties();
		while ( properties->hasMoreElements() ) {
			
			Property* property = properties->nextElement();
			VariantData data = *property->get();

			System::print( "\tProperty \"%s\", is read only: %s, value: %s\n", 
							property->getDisplayName().c_str(), 
							property->isReadOnly() ? "true" : "false", 
							data.toString().c_str() );

		}
		
		System::print( "Number of interfaces known to RTTI: %d\n",
			clazz->getInterfaceCount() );
		
		System::print( "Number of methods known to RTTI: %d\n",
			clazz->getMethodCount() );

		Enumerator<Method*>* methods = clazz->getMethods();
		while ( methods->hasMoreElements() ) {
			
			Method* method = methods->nextElement();
			
			System::print( "\tMethod name: \"%s\", number of arguments: %d\n",
							method->getName().c_str(), method->getArgCount() );
			

		}
	}


}


int main( int argc, char** argv ){

	printf( "abount to call FoundationKit::init()\n " );
	FoundationKit::init();

	REGISTER_CLASSINFO( SimpleClass );

	REGISTER_CLASSINFO( LittleLessSimpleClass );

	REGISTER_CLASSINFO( FooBarBazifier );

	System::print( "sizeof(SimpleClass) = %d\n", sizeof(SimpleClass) );
	System::print( "sizeof(LittleLessSimpleClass) = %d\n", sizeof(LittleLessSimpleClass) );

	//create an instance on the fly

	Object* simpleClassInstance = NULL;
	ClassRegistry::getClassRegistry()->createNewInstance( "SimpleClass", &simpleClassInstance );

	if ( NULL != simpleClassInstance ) {
		System::print( "simpleClassInstance created successfully - %s\n", 
						simpleClassInstance->toString().c_str() );

		reportRTTIData( simpleClassInstance );
	}
	

	delete simpleClassInstance;
	simpleClassInstance = NULL;

	ClassRegistry::getClassRegistry()->createNewInstanceFromClassID( LITTLELESSSIMPLECLASS_CLASSID, &simpleClassInstance );

	if ( NULL != simpleClassInstance ) {
		System::print( "LittleLessSimpleClass created successfully - %s\n", 
						simpleClassInstance->toString().c_str() );

		reportRTTIData( simpleClassInstance );
	}

	FooBarBazifier* ffb = new FooBarBazifier();
	Object* o = new Object();


	FooBarBazifier fb;

	reportRTTIData( &fb );


	FoundationKit::terminate();
	return 0;
}










By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Currently working on the Visual Component Framework, a really cool C++ framework. Currently the VCF has millions upon millions upon billions of Users. If I make anymore money from it I'll have to buy my own country.

Comments and Discussions