#ifndef CREATABLE_H #define CREATABLE_H /////////////////////////////////////////////////////////////////////// // creatable.h - declares creatable interface and object factory // // ver 1.0 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// /* Module Operations: ================== The creatable class defines an interface for objects that can be created with an instance of class objFactory. Creatable classes inherit from the creatable interface and implement three functions: - a constructor that registers the class with objFactory - a create function that returns a new instance of the class via a creatable pointer. - a function called classID() that returns a string holding the class name. Usually, we declare a static global instance of every class to be manufactured by objFactory. Because they are static, their con- structors are called before main is entered, so any instance of objFactory has all the classes registered when your code begins to execute. Public Interface of creatable and objFactory classes ==================================================== objFactory factory; myCreatable *pMyCr = dynamic_cast(factory.makeObj("myCreatable"); pMyCr->memberfun(); delete pMyCr; */ // ///////////////////////////////////////////////////////////////////////// // build process // ///////////////////////////////////////////////////////////////////////// // required files: // // creatable.h, creatable.cpp // // // // compiler command: // // cl /GX /GR /DTEST_CREATABLE creatable.cpp // // // // note: /GR enables RTTI which we need in order to use dynamic_cast. // // In the IDE you can enable RTTI on the Project Settings // // menu. Select C/C++ tab and select C++ language from the // // category combo box. Check the box for enabling RTTI. // // // ///////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.0 : 01 Dec 01 - first release */ // #pragma warning(disable : 4786) #include #include ///////////////////////////////////////////////////////////////////////// // creatable interface declaration typedef std::string classID; class creatable { public: virtual ~creatable() { } static creatable* create(); virtual classID id() const = 0; }; ///////////////////////////////////////////////////////////////////////// // createFunc is a typedef for a pointer to a function with the // signature: creatable* create() // This function returns a new instance of a creatable class. typedef creatable*(*createFunc)(); // Stroustrup, pg 420 ///////////////////////////////////////////////////////////////////////// // registryType is the type of a map which holds pairs where the key // is a class name string and value is a pointer to a create function typedef std::map registryType; ///////////////////////////////////////////////////////////////////////// // objFactory class declaration class objFactory { public: void reg(classID id, createFunc pFun); creatable* makeObj(classID id); private: static registryType registry; }; #endif