/tstype

Lightweight C++ RTTI library

Primary LanguageC++MIT LicenseMIT

TSType

What is it?

A lightweight RTTI lib for C++ that doesn't require C++'s RTTI or boost. It enables real reflection and is useful for inspecting 3rd party structs and classes without modifying 3rd party source files.

How do I use it?

Add the cpp and h source files to your project.

Header Changes

Add TSType declarations after the classes declarations you want to inspect:

TSDeclareType(SomeKindOfClass, SomeBaseClassType);

If there is no base class:

TSDeclareType(SomeKindOfClass, TSEmpty);

If the class is abstract:

TSDeclareAbstractType(SomeKindOfClass, SomeBaseClassType);

Source Changes

Implement the class in a c++ file:

TSImplementType(SomeKindOfClass, "complexity manager");

If the class is abstract:

TSImplementAbstractType(SomeKindOfClass, "complexity manager");

Describe each public field in the C++ source and add a default value:

TSField(TSString, SomeKindOfClass, fileName, "user.prefs");

Sample

#include "TSType.h"

class MostWanted 
{
public:
    std::vector< std::string > names;
};
TSDeclareType(MostWanted, TSEmpty); 
#include "MostWanted.h"

TSImplementType(MostWanted, "persons of interest");
TSField(TSStringArray, MostWanted, names, std::vector< std::string >());

void testTSType()
{
    printf("All known types:\n");
    TSType::print();
    
    printf("\nPrinting a description of the TSStringType object:\n");
    PrintObjectHierarchy(TSTypeType, TSStringType);
	
    MostWanted* foo = MostWantedType->create();
    foo->names.push_back("Bob");

    printf("\nPrinting a description of the MostWanted object:\n");
    PrintObjectHierarchy(MostWantedType, foo);

    MostWantedType->destroy(foo);
}

Output:

All known types:
    Type 
        MostWanted ( names )
        TSObject ( type )
            ContainerBase 
                GenericContainer 
            GenericReference ( id )
        TSType ( fields name description )
        Array 
            MostWantedArray 
            MostWantedPtrArray 
            TSObjectArray 
            TSObjectPtrArray 
            TSTypePtrArray 
                TSTypefields 
            ContainerBaseArray 
            ContainerBasePtrArray 
            GenericContainerArray 
            GenericContainerPtrArray 
            GenericReferenceArray 
            GenericReferencePtrArray 
            TSStringArray 
                MostWantednames 
            TSStringPtrArray 
        Pointer 
            MostWantedPtr 
            MostWantedArrayPtr 
            MostWantedPtrArrayPtr 
            TSObjectPtr 
            TSObjectArrayPtr 
            TSObjectPtrArrayPtr 
            TSTypePtr 
                TSObjecttype 
            TSTypePtrArrayPtr 
            ContainerBasePtr 
            ContainerBaseArrayPtr 
            ContainerBasePtrArrayPtr 
            GenericContainerPtr 
            GenericContainerArrayPtr 
            GenericContainerPtrArrayPtr 
            GenericReferencePtr 
            GenericReferenceArrayPtr 
            GenericReferencePtrArrayPtr 
            TSStringPtr 
            TSStringArrayPtr 
            TSStringPtrArrayPtr 
        TSString 
            TSTypename 
            TSTypedescription 
            GenericReferenceid 

Printing a description of the TSStringType object:
type instance
    fields
    name = "TSString"
    description = "text"

Printing a description of the MostWanted object:
persons of interest
    names
        text = "Bob"

Notes

Missing API for enumerating the methods of an object.

Default values are not evaluated until they are requested by calling field type class's setDefaultValue on the object instance.