root/juggler/branches/2.2/modules/gadgeteer/gadget/Type/BaseTypeFactory.cpp

Revision 20689, 6.2 kB (checked in by patrick, 1 year ago)

MFT r20685, r20688: Derive from a known gadget::InputMixer<S,T> instantiation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26
27 #include <gadget/gadgetConfig.h>
28 #include <typeinfo>
29
30 #include <gadget/Type/BaseTypeFactory.h>
31
32 // Platform-independent devices.
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 // Initialize the singleton ptr
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  * Registers all the devices that I know about.
79  * @note This should really be moved to dynamic library loading code.
80  */
81 void BaseTypeFactory::hackLoadKnownDevices()
82 {
83    // NOTE: These will all given unused variable errors in compiling.
84    // That is okay, because the don't actually have to do anything.
85    // They just register themselves in their constructor.
86
87    // Platform-independent devices.
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);     // Add the constructor to the list
112 }
113
114 // Simply query all device constructors registered looking
115 // for one that knows how to load the device
116 bool BaseTypeFactory::recognizeNetDevice(std::string base_type)
117 {
118    return ! (findConstructor(base_type) == -1);
119 }
120
121 /**
122  * Loads the specified device.
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    //std::string element_type(element->getID());
145
146    for ( unsigned int i = 0; i < mConstructors.size(); ++i )
147    {
148       // Get next constructor
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 } // End of gadget namespace
184
Note: See TracBrowser for help on using the browser.