File: Primitive3DFactory.h

package info (click to toggle)
transcend 0.3.dfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,768 kB
  • sloc: cpp: 26,891; ansic: 693; sh: 210; makefile: 96; perl: 67
file content (129 lines) | stat: -rw-r--r-- 3,378 bytes parent folder | download | duplicates (30)
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-April-1   Jason Rohrer
 * Added subreal entity vane class to factory. 
 */

#ifndef PRIMITIVE_3D_FACTORY_INCLUDED
#define PRIMITIVE_3D_FACTORY_INCLUDED 

#include "Primitive3D.h"

// Run-time type identification interface (RTTI)
#include <typeinfo>


// include these primitives only if we are part of subreal
#ifdef SUBREAL
#include "subreal/entity/EntityBodyPrimitive3D.h"
#define FACTORY_ENTITY_BODY_PRIMITIVE_FLAG 1
#include "subreal/entity/EntityVanePrimitive3D.h"
#define FACTORY_ENTITY_VANE_PRIMITIVE_FLAG 2

#endif

// the default flag
#define FACTORY_DEFAULT_PRIMITIVE_FLAG 0


/**
 * Class that maps Primitive3D integer subclass type flags to Primitive3D
 * subclass instances.
 *
 * Motivation for this class:
 * We want to extend Object3D to support subclass typed serialization
 * and deserialization without placing too much of a burden on
 * future Object3D subclasses.  For instance, we don't want subclasses
 * to have to write their own de/serialization functions so that
 * particular Primitive3D subclasses are serialized correctly.
 * We can avoid this burden by writing the base Object3D serialization
 * code so that it uses this factory to transmit subclasses with
 * type informaton.
 *
 * @author Jason Rohrer
 */
class Primitive3DFactory {
	public:

		/**
		 * Finds an integer subclass type flag for a primitive instance.
		 *
		 * @param inPrimitive the primitive to determine a flag for.  Must
		 *   be destroyed by the caller.
		 *
		 * @return a type flag for inObject.  0 (the default Object3D
		 *   baseclass flag) will be returned if subclass determination fails.
		 */
		static int primitive3DToInt( Primitive3D *inPrimitive );

		
		/**
		 * Constructs a new, unitialized Primitive3D (in other words,
		 * an Primitive3D ready for deserialization) of a subclass
		 * type matching inTypeFlag.
		 *
		 * @param inTypeFlag the type flag specifying the class
		 *   of the returned primitive.
		 *
		 * @return an (unitialized) Primitive3D class instance with a
		 *   subclass type corresponding to inTypeFlag.  If no
		 *   matching class is found, a default Primitive3D baseclass
		 *   instance is returned.  Must be destroyed by the caller. 
		 */
		static Primitive3D *intToPrimitive3D( int inTypeFlag );
		
	};



inline int Primitive3DFactory::primitive3DToInt( Primitive3D *inPrimitive ) {

	// use RTTI to determine type of inPrimitive
	
#ifdef SUBREAL
	if( typeid( *inPrimitive ) == typeid( EntityBodyPrimitive3D ) ) {
		return FACTORY_ENTITY_BODY_PRIMITIVE_FLAG;
		}
	else if( typeid( *inPrimitive ) == typeid( EntityVanePrimitive3D ) ) {
		return FACTORY_ENTITY_VANE_PRIMITIVE_FLAG;
		}
#endif

	// else return the default flag
	return FACTORY_DEFAULT_PRIMITIVE_FLAG;
	}



inline Primitive3D *Primitive3DFactory::intToPrimitive3D( int inTypeFlag ) {
	switch( inTypeFlag ) {
		case FACTORY_DEFAULT_PRIMITIVE_FLAG:
			return new Primitive3D();
			break;

			/* these primitives are only defined if
			 * we are part of subreal
			 */
#ifdef SUBREAL
		case FACTORY_ENTITY_BODY_PRIMITIVE_FLAG:
			return new EntityBodyPrimitive3D();
			break;
		case FACTORY_ENTITY_VANE_PRIMITIVE_FLAG:
			return new EntityVanePrimitive3D();
			break;
#endif

		default:
			// unknown primitive flag type
			return new Primitive3D();
			break;
		}
			
	}


#endif