| 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 |
|
|---|
| 29 |
#include <iomanip> |
|---|
| 30 |
#include <typeinfo> |
|---|
| 31 |
|
|---|
| 32 |
#include <jccl/Config/ConfigElement.h> |
|---|
| 33 |
|
|---|
| 34 |
#include <gadget/Type/AnalogProxy.h> |
|---|
| 35 |
#include <gadget/Type/DigitalProxy.h> |
|---|
| 36 |
#include <gadget/Type/PositionProxy.h> |
|---|
| 37 |
#include <gadget/Type/GloveProxy.h> |
|---|
| 38 |
#include <gadget/Type/GestureProxy.h> |
|---|
| 39 |
#include <gadget/Type/KeyboardMouseProxy.h> |
|---|
| 40 |
#include <gadget/Type/CommandProxy.h> |
|---|
| 41 |
#include <gadget/Type/StringProxy.h> |
|---|
| 42 |
#include <gadget/Util/Debug.h> |
|---|
| 43 |
|
|---|
| 44 |
#include <gadget/ProxyFactory.h> |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
namespace gadget |
|---|
| 48 |
{ |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
vprSingletonImpWithInitFunc( ProxyFactory, loadKnownProxies ); |
|---|
| 52 |
|
|---|
| 53 |
ProxyConstructorBase::ProxyConstructorBase() |
|---|
| 54 |
: boost::enable_shared_from_this<ProxyConstructorBase>() |
|---|
| 55 |
{ |
|---|
| 56 |
; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
ProxyConstructorBase::~ProxyConstructorBase() |
|---|
| 60 |
{ |
|---|
| 61 |
; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
template<typename PROXY> |
|---|
| 65 |
boost::shared_ptr<ProxyConstructorBase> ProxyConstructor<PROXY>::create() |
|---|
| 66 |
{ |
|---|
| 67 |
boost::shared_ptr< ProxyConstructor<PROXY> > px(new ProxyConstructor()); |
|---|
| 68 |
ProxyFactory::instance()->registerProxy(px); |
|---|
| 69 |
return px; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
ProxyFactory::~ProxyFactory() |
|---|
| 73 |
{ |
|---|
| 74 |
jccl::DependencyManager::instance()->unregisterChecker(&mDepChecker); |
|---|
| 75 |
mConstructors.clear(); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
void ProxyFactory::loadKnownProxies() |
|---|
| 80 |
{ |
|---|
| 81 |
ProxyConstructor<AnalogProxy>::create(); |
|---|
| 82 |
ProxyConstructor<DigitalProxy>::create(); |
|---|
| 83 |
ProxyConstructor<PositionProxy>::create(); |
|---|
| 84 |
ProxyConstructor<GloveProxy>::create(); |
|---|
| 85 |
|
|---|
| 86 |
ProxyConstructor<KeyboardMouseProxy>::create(); |
|---|
| 87 |
ProxyConstructor<CommandProxy>::create(); |
|---|
| 88 |
ProxyConstructor<StringProxy>::create(); |
|---|
| 89 |
|
|---|
| 90 |
jccl::DependencyManager::instance()->registerChecker(&mDepChecker); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
void ProxyFactory::registerProxy(boost::shared_ptr<ProxyConstructorBase> constructor) |
|---|
| 94 |
{ |
|---|
| 95 |
mConstructors.push_back(constructor); |
|---|
| 96 |
vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_LVL) |
|---|
| 97 |
<< "gadget::ProxyFactory: Constructor registered for: " |
|---|
| 98 |
<< std::setiosflags(std::ios::right) << std::setw(25) |
|---|
| 99 |
<< std::setfill(' ') << constructor->getElementType() |
|---|
| 100 |
<< std::resetiosflags(std::ios::right) |
|---|
| 101 |
|
|---|
| 102 |
<< " type: " << typeid(*constructor).name() << std::endl |
|---|
| 103 |
<< vprDEBUG_FLUSH; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
bool ProxyFactory::recognizeProxy(jccl::ConfigElementPtr element) |
|---|
| 109 |
{ |
|---|
| 110 |
return ! (findConstructor(element) == -1); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
Proxy* ProxyFactory::loadProxy(jccl::ConfigElementPtr element) |
|---|
| 117 |
{ |
|---|
| 118 |
vprASSERT(recognizeProxy(element)); |
|---|
| 119 |
|
|---|
| 120 |
int index = findConstructor(element); |
|---|
| 121 |
|
|---|
| 122 |
Proxy* new_proxy; |
|---|
| 123 |
boost::shared_ptr<ProxyConstructorBase> constructor = mConstructors[index]; |
|---|
| 124 |
|
|---|
| 125 |
vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL) |
|---|
| 126 |
<< "[gadget::ProxyFactory::loadProxy] Loading proxy: " |
|---|
| 127 |
<< element->getID() << " with: " |
|---|
| 128 |
<< typeid(*constructor).name() << std::endl << vprDEBUG_FLUSH; |
|---|
| 129 |
new_proxy = constructor->createProxy(element); |
|---|
| 130 |
return new_proxy; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
int ProxyFactory::findConstructor(jccl::ConfigElementPtr element) |
|---|
| 134 |
{ |
|---|
| 135 |
std::string element_type(element->getID()); |
|---|
| 136 |
|
|---|
| 137 |
for(unsigned i=0;i<mConstructors.size();i++) |
|---|
| 138 |
{ |
|---|
| 139 |
if(mConstructors[i]->getElementType() == element_type) |
|---|
| 140 |
{ |
|---|
| 141 |
return i; |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
return -1; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
} |
|---|
| 149 |
|
|---|