// TrfObject.h #ifndef TrfObject_H #define TrfObject_H // Base class for TRF++ objects that are to be handled generically. // A method for run time type identification (RTTI) is provided. // // The methods get_type() and get_generic_type() respectively return // ID's for the full type and the type immediately below TrfObject in // the inheritance tree. For example SurfCylinder would return // the ID's for SurfCylinder and Surface, respectively. // // This class inherits from TrfObject which require a unique creator // be defined for each subclass. The address of this function is // used as the type ID. // // Here is an example of usage: #if 0 // Begin comment ---------------------------------------------------------------------- // MyClass.hpp class ObjData; class MyClass : public trf::TrfObject { public: // static methods // Return the type name. static TypeName get_type_name() { return "MyClass"; } // Return the creator. static ObjCreator get_creator(); // Return the type. static Type get_static_type() { return get_creator(); } public // methods // Return the generic type. // This is only neded at this level. Type get_generic_type() const { return get_static_type(); } // Return the type. // Not needed if this class is abstract. Type get_type() const { return get_static_type(); } // Write data to a data object. ObjData write_data() const; }; ---------------------------------------------------------------------- // MyClass.cpp // Define Creator. ObjType* create_MyClass(const ObjData& data) { ... } ObjCreator MyClass::get_creator() { return create_MyClass; } ObjData MyClass::write_data() const { ... } ---------------------------------------------------------------------- #endif // End comment // // The method get_static_type() is conventional. It allows // users to check the type: // // assert( myobj.get_type() == MyClass::get_static_type() ); // #include "objstream/ObjType.hpp" namespace trf { class TrfObject : public ObjType { public: // typedefs // Type ID. typedef ObjCreator Type; public: // static methods // Return the type. // This should be overridden in all subclasses. // It is not implemented here to encourage that. static Type get_static_type(); public: // methods // Destructor. virtual ~TrfObject(); // Return the type of the object. // This is the bottom of the inheritance tree. virtual Type get_type() const =0; // Return the generic type of the object (cluster, layer, ...). // This is the level after TrfObject in the inheritance tree. virtual Type get_generic_type() const =0; }; } // end namespace #endif