1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Modification History
*
* 2001-March-14 Jason Rohrer
* Created.
*
* 2001-March-17 Jason Rohrer
* Finished implementation.
*
* 2001-April-1 Jason Rohrer
* Fixed flag name.
*/
#ifndef OBJECT_3D_FACTORY_INCLUDED
#define OBJECT_3D_FACTORY_INCLUDED
#include "Object3D.h"
// Run-time type identification interface (RTTI)
#include <typeinfo>
// include these objects only if we are part of subreal
#ifdef SUBREAL
#include "subreal/entity/EntityObject3D.h"
#define FACTORY_ENTITY_OBJECT_FLAG 1
#endif
// the default flag
#define FACTORY_DEFAULT_OBJECT_FLAG 0
/**
* Class that maps Object3D integer subclass type flags to Object3D
* subclass instances.
*
* Motivation for this class:
* We have Object3D instances that exist simultaneously on opposite
* sides of the network (at both the client and server ends). Initially,
* we just let the objects get sent to the server via the base class
* Object3D serialization function. At that time, subclasses of Object3D
* did nothing more than init an Object3D in a particular way. This
* worked fine when we didn't have animations associated with various
* Object3D subclasses, but doesn't work now that we have animation
* code in various subclasses. During serialization (if the object
* is sent as a generic Object3D), the animation code is lost on
* the client end. We need a way for the client to pick the correct
* subclass to construct for deserialization. We can encode the
* various subtypes by integers, and then this factory class can
* be used to construct a class of appropriate subtype before
* deserialization.
*
* @author Jason Rohrer
*/
class Object3DFactory {
public:
/**
* Finds an integer subclass type flag for an object instance.
*
* @param inObject the object to determine a flag for. Must
* be destroyed by the caller.
*
* @return a type flag for inObject. 0 (the defautl Object3D
* baseclass flag) will be returned if subclass determination fails.
*/
static int object3DToInt( Object3D *inObject );
/**
* Constructs a new, unitialized Object3D (in other words,
* an Object3D ready for deserialization) of a subclass
* type matching inTypeFlag.
*
* @param inTypeFlag the type flag specifying the class
* of the returned object.
*
* @return an (unitialized) Object3D class instance with a
* subclass type corresponding to inTypeFlag. If no
* matching class is found, a default Object3D baseclass
* instance is returned. Must be destroyed by the caller.
*/
static Object3D *intToObject3D( int inTypeFlag );
};
inline int Object3DFactory::object3DToInt( Object3D *inObject ) {
// use RTTI to determine type of inObject
#ifdef SUBREAL
if( typeid( *inObject ) == typeid( EntityObject3D ) ) {
return FACTORY_ENTITY_OBJECT_FLAG;
}
#endif
// else return the default flag
return FACTORY_DEFAULT_OBJECT_FLAG;
}
inline Object3D *Object3DFactory::intToObject3D( int inTypeFlag ) {
switch( inTypeFlag ) {
case FACTORY_DEFAULT_OBJECT_FLAG:
return new Object3D();
break;
/* these objects are only defined if
* we are part of subreal
*/
#ifdef SUBREAL
case FACTORY_ENTITY_OBJECT_FLAG:
return new EntityObject3D();
break;
#endif
default:
// unknown object flag type
return new Object3D();
break;
}
}
#endif
|