How to add new object type ?
- think about name describing that type; let's suppose it is 'flower'
- add #define ITEM_FLOWER xxx in object.h where xxx is free number
- add entry to obj_type_list in object.cc; { "flower", ITEM_FLOWER }
- add virtual bool isFlower() { return false; } to object.h under
rest of isXxx entries
- add case ITEM_FLOWER: return new ObjFlower();
in object.cc inside createObject
- copy ObjTemplate class in objtypes.h and change all references from
Template -> Flower and ITEM_TEMPLATE -> ITEM_FLOWER
// note - this is for direct inheritance from Object class; if
// you want to inherit from some other class (ObjPlant) change all
// references from Object -> ObjPlant
- change object specific values; in ObjTemplate they are represented by
single entry 'int values'
- for every value add getXxx and setXxx functions
- inside objtypes.cc you have to create 4 functions
int ObjFlower::readFromTypeSpecific( InFile & );
// read all values
int ObjFlower::writeToTypeSpecific( OutFile & ) const;
// save all values
void ObjFlower::reportValues( String & str );
// print all values to string for OLC - with comments
char * ObjFlower::setValues( const String & arg );
// parse argument for 'value_name value' syntax, try to set it
// and report success or failure by returning pointer to comment
And thats all :)
If you think that it is very coumbersome comparing to previous switch/case
scheme you are not quite right. It is only a bit harder to construct, but
a lot easier to use, and allow for some previously unavailable tricks, like
inheritance from derived objects and any number of values for new objects.
Few notes:
- be sure to call superclass functions ! for classes derived directly
from Object it is not need, but as you will nest inheritance deeper
it will became obvious
- calling superclass functions is done at end of function with one exception -
reportValues; it is done this way to make output nicer.
- do not use Object::Object( Object & obj ) cloning contructor any longer !
use obj->clone() instead (too sad C++ do not support java Cloneable
interface :(