| 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 |
#include <gadget/gadgetConfig.h> |
|---|
| 28 |
#include <typeinfo> |
|---|
| 29 |
|
|---|
| 30 |
#include <gadget/Type/BaseTypeFactory.h> |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
#include <gadget/Type/Input.h> |
|---|
| 34 |
#include <gadget/Devices/Sim/SimInput.h> |
|---|
| 35 |
#include <gadget/Type/Analog.h> |
|---|
| 36 |
#include <gadget/Type/Digital.h> |
|---|
| 37 |
#include <gadget/Type/Glove.h> |
|---|
| 38 |
#include <gadget/Type/Position.h> |
|---|
| 39 |
#include <gadget/Type/KeyboardMouse.h> |
|---|
| 40 |
#include <gadget/Type/Command.h> |
|---|
| 41 |
#include <gadget/Type/String.h> |
|---|
| 42 |
#include <gadget/Type/InputMixer.h> |
|---|
| 43 |
#include <gadget/Util/Debug.h> |
|---|
| 44 |
#include <gadget/Type/InputBaseTypes.h> |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
#define REGISTER_CONSTRUCTOR_TYPE(INPUT_TYPE) \ |
|---|
| 48 |
BaseTypeConstructor< INPUT_TYPE::MixedPlaceholderType >* con_ ## INPUT_TYPE \ |
|---|
| 49 |
= new BaseTypeConstructor< INPUT_TYPE::MixedPlaceholderType >; \ |
|---|
| 50 |
if (NULL == con_ ## INPUT_TYPE) \ |
|---|
| 51 |
{ \ |
|---|
| 52 |
vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL) \ |
|---|
| 53 |
<< clrOutBOLD(clrRED,"ERROR:") << " Failed to load a known type " \ |
|---|
| 54 |
<< #INPUT_TYPE << std::endl << vprDEBUG_FLUSH; \ |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
namespace gadget |
|---|
| 58 |
{ |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
vprSingletonImpWithInitFunc( BaseTypeFactory, hackLoadKnownDevices ); |
|---|
| 62 |
|
|---|
| 63 |
BaseTypeFactory::~BaseTypeFactory() |
|---|
| 64 |
{ |
|---|
| 65 |
typedef std::vector<BaseTypeConstructorBase*>::iterator iter_type; |
|---|
| 66 |
for ( iter_type itr = mConstructors.begin(); itr != mConstructors.end(); ++itr ) |
|---|
| 67 |
{ |
|---|
| 68 |
if (NULL != *itr) |
|---|
| 69 |
{ |
|---|
| 70 |
delete *itr; |
|---|
| 71 |
*itr = NULL; |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
mConstructors.clear(); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
void BaseTypeFactory::hackLoadKnownDevices() |
|---|
| 82 |
{ |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
REGISTER_CONSTRUCTOR_TYPE(input_digital_t); |
|---|
| 90 |
REGISTER_CONSTRUCTOR_TYPE(input_analog_t); |
|---|
| 91 |
REGISTER_CONSTRUCTOR_TYPE(input_position_t); |
|---|
| 92 |
REGISTER_CONSTRUCTOR_TYPE(input_keyboard_t); |
|---|
| 93 |
REGISTER_CONSTRUCTOR_TYPE(input_string_t); |
|---|
| 94 |
REGISTER_CONSTRUCTOR_TYPE(input_command_t); |
|---|
| 95 |
REGISTER_CONSTRUCTOR_TYPE(input_glove_t); |
|---|
| 96 |
REGISTER_CONSTRUCTOR_TYPE(input_digital_analog_t); |
|---|
| 97 |
REGISTER_CONSTRUCTOR_TYPE(input_digital_position_t); |
|---|
| 98 |
REGISTER_CONSTRUCTOR_TYPE(input_analog_position_t); |
|---|
| 99 |
REGISTER_CONSTRUCTOR_TYPE(input_glove_digital_t); |
|---|
| 100 |
REGISTER_CONSTRUCTOR_TYPE(input_glove_digital_analog_position_t); |
|---|
| 101 |
REGISTER_CONSTRUCTOR_TYPE(input_digital_analog_position_t); |
|---|
| 102 |
REGISTER_CONSTRUCTOR_TYPE(siminput_input_position); |
|---|
| 103 |
REGISTER_CONSTRUCTOR_TYPE(siminput_input_digital); |
|---|
| 104 |
REGISTER_CONSTRUCTOR_TYPE(siminput_input_analog); |
|---|
| 105 |
REGISTER_CONSTRUCTOR_TYPE(siminput_input_digital_glove_t); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
void BaseTypeFactory::registerNetDevice(BaseTypeConstructorBase* constructor) |
|---|
| 109 |
{ |
|---|
| 110 |
vprASSERT(constructor != NULL); |
|---|
| 111 |
mConstructors.push_back(constructor); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
bool BaseTypeFactory::recognizeNetDevice(std::string base_type) |
|---|
| 117 |
{ |
|---|
| 118 |
return ! (findConstructor(base_type) == -1); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
Input* BaseTypeFactory::loadNetDevice(std::string base_type) |
|---|
| 125 |
{ |
|---|
| 126 |
vprASSERT(recognizeNetDevice(base_type)); |
|---|
| 127 |
|
|---|
| 128 |
int index = findConstructor(base_type); |
|---|
| 129 |
|
|---|
| 130 |
Input* new_dev; |
|---|
| 131 |
BaseTypeConstructorBase* constructor = mConstructors[index]; |
|---|
| 132 |
|
|---|
| 133 |
new_dev = constructor->createNetDevice(base_type); |
|---|
| 134 |
if(new_dev!=NULL) |
|---|
| 135 |
{ |
|---|
| 136 |
vprDEBUG(gadgetDBG_RIM,vprDBG_VERB_LVL) |
|---|
| 137 |
<< "[NetDevice Factory] Found the BaseType\n"<< vprDEBUG_FLUSH; |
|---|
| 138 |
} |
|---|
| 139 |
return new_dev; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
int BaseTypeFactory::findConstructor(std::string base_type) |
|---|
| 143 |
{ |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
for ( unsigned int i = 0; i < mConstructors.size(); ++i ) |
|---|
| 147 |
{ |
|---|
| 148 |
|
|---|
| 149 |
BaseTypeConstructorBase* construct = mConstructors[i]; |
|---|
| 150 |
vprASSERT(construct != NULL); |
|---|
| 151 |
if(construct->getInputTypeName() == base_type) |
|---|
| 152 |
{ |
|---|
| 153 |
return i; |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
return -1; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
void BaseTypeFactory::debugDump() |
|---|
| 162 |
{ |
|---|
| 163 |
vprDEBUG_OutputGuard(gadgetDBG_RIM, vprDBG_VERB_LVL, |
|---|
| 164 |
std::string("gadget::BaseTypeFactory::debugDump\n"), |
|---|
| 165 |
std::string("------ END DUMP ------\n")); |
|---|
| 166 |
|
|---|
| 167 |
vprDEBUG(gadgetDBG_RIM, vprDBG_VERB_LVL) << "num constructors:" |
|---|
| 168 |
<< mConstructors.size() << "\n" |
|---|
| 169 |
<< vprDEBUG_FLUSH; |
|---|
| 170 |
|
|---|
| 171 |
for ( unsigned int cNum = 0; cNum < mConstructors.size(); ++cNum ) |
|---|
| 172 |
{ |
|---|
| 173 |
BaseTypeConstructorBase* dev_constr = mConstructors[cNum]; |
|---|
| 174 |
vprDEBUG(gadgetDBG_RIM, vprDBG_VERB_LVL) |
|---|
| 175 |
<< cNum << ": Constructor:" << (void*)dev_constr |
|---|
| 176 |
<< " type:" << typeid(*dev_constr).name() << "\n" << vprDEBUG_FLUSH; |
|---|
| 177 |
vprDEBUG(gadgetDBG_RIM, vprDBG_VERB_LVL) << " recog:" |
|---|
| 178 |
<< dev_constr->getInputTypeName() << "\n" |
|---|
| 179 |
<< vprDEBUG_FLUSH; |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
} |
|---|
| 184 |
|
|---|