ObjectHierarchySample.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#include "QD3D.h"
 
/*
 * Quick Hack to dump the object hierarchy for QuickDraw 3D.
 *
 * This is a neat little snippet that shows how to use several
 * of the new object system calls (check out QD3D.h for the prototypes):
 *
 *
 *  Q3ObjectHierarchy_GetStringFromType
 *  ----------------------------------- 
 *
 *   TQ3Status Q3ObjectHierarchy_GetStringFromType(
 *      TQ3ObjectType               objectClassType, 
 *      TQ3ObjectClassNameString    objectClassString);
 *  
 *  Given a class type as return the associated string for the class name, 
 *  may return kQ3Failure if the type representing the class is invalid.
 *
 *
 *  struct TQ3SubClassData
 *  ----------------------
 *  typedef struct TQ3SubClassData {
 *      unsigned long       numClasses;     -- the # of subclass types found
 *                                          -- for a parent class
 *
 *      TQ3ObjectType       *classTypes;    -- an array containing the class
 *                                          -- types                        
 *  } TQ3SubClassData;
 *   
 *  TQ3SubClassData is used when querying the object system for
 *  the subclasses of a particular parent type, it is allocated 
 *  with a call to Q3ObjectClass_GetSubClassData, so every instance
 *  of this call must be matched with a call to Q3ObjectClass_EmptySubClassData
 *  or your application will leak.
 *
 *
 *  Q3ObjectClass_GetSubClassData
 *  -----------------------------
 *
 *  TQ3Status QD3D_CALL Q3ObjectClass_GetSubClassData(
 *      TQ3ObjectType               objectClassType, 
 *      TQ3SubClassData             *subClassData);
 *
 *  Given a parent type and an instance of the TQ3SubClassData struct fill
 *  it in with the number and class types of all of the subclasses immediately
 *  below the parent in the class hierarchy.  Return kQ3Success to indicate no
 *  errors occurred, else kQ3Failure.
 *
 *  NOTE:  This function will allocate memory for the classTypes array.  Be 
 *  sure to call Q3ObjectClass_EmptySubClassData to free this memory up.
 *
 *
 *  Q3ObjectClass_EmptySubClassData
 *  -------------------------------
 *
 *  TQ3Status  Q3ObjectClass_EmptySubClassData(
 *      TQ3SubClassData             *subClassData );
 *
 *  Given an instance of the TQ3SubClassData struct free all memory allocated 
 *  by the Q3ObjectClass_GetSubClassData call.
 *
 *  NOTE: This call MUST be made after a call to Q3ObjectClass_GetSubClassData
 *  to avoid memory leaks.
 *
 *  We just use metrowerks console library to dump out a text list of the
 *  hierarchy, with the appropriate level of indentation.
 *
 * Nick Thompson, nickt@apple.com, 10/96
 *
 */
 
/* prototypes  */
static void PrintClassAndRecurse( TQ3ObjectType objectClassType, int depth ) ;
 
/* entry point */
void main(void)
{
    printf ("QuickDrawª 3D Object Hierarchy\n\n");
 
    Q3Initialize() ;
    
    /*
     * The class "Object" is in fact a virtual base class, it is not possible
     * to instantiate this class.  At the root of the hierarchy are four classes
     * these are:
     *    View
     *    Pick
     *    Element
     *    Shared
     * So we can fake out object at the root of the hierarchy, and go from each
     * of the four named classes.
     */
    
    printf( "Root Object (virtual metaclass)\n" ) ;
    
    PrintClassAndRecurse( kQ3ObjectTypeView, 0 ) ;
    PrintClassAndRecurse( kQ3ObjectTypeElement, 0 ) ;
    PrintClassAndRecurse( kQ3ObjectTypePick, 0 ) ;
    PrintClassAndRecurse( kQ3ObjectTypeShared, 0 ) ;
    
    Q3Exit() ;
}
 
/* the guts... :) */
static void PrintClassAndRecurse( TQ3ObjectType objectClassType, int depth )
{
    TQ3SubClassData             mySubClassData ;
    TQ3ObjectClassNameString    myObjectClassString ;
    unsigned long               index ;
    
    depth++ ;
    if( objectClassType != kQ3ObjectTypeInvalid )
    {   
        
        Q3ObjectHierarchy_GetStringFromType( objectClassType, myObjectClassString) ;
        
        for(index = 0; index < depth; index++)
            printf(" ") ;
        
        printf("%s\n", myObjectClassString) ;
        
        Q3ObjectHierarchy_GetSubClassData( objectClassType, &mySubClassData) ;
        
        for( index = 0; index < mySubClassData.numClasses; index++ )
        {
            /* recurse on each subclass type */
            PrintClassAndRecurse( mySubClassData.classTypes[index], depth ) ;
        }
        
        Q3ObjectHierarchy_EmptySubClassData( &mySubClassData ) ;
    }
    depth-- ;
}