root/juggler/branches/2.2/modules/gadgeteer/gadget/ProxyFactory.cpp

Revision 19729, 4.4 kB (checked in by patrick, 2 years ago)

Copyright update.

  • 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
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 // Initialize the singleton ptr
51 vprSingletonImpWithInitFunc( ProxyFactory, loadKnownProxies );
52
53 ProxyConstructorBase::ProxyConstructorBase()
54    : boost::enable_shared_from_this<ProxyConstructorBase>()
55 {
56    /* Do nothing. */ ;
57 }
58
59 ProxyConstructorBase::~ProxyConstructorBase()
60 {
61    /* Do nothing. */ ;
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 // Register all the proxies that I know about
79 void ProxyFactory::loadKnownProxies()
80 {
81    ProxyConstructor<AnalogProxy>::create();
82    ProxyConstructor<DigitalProxy>::create();
83    ProxyConstructor<PositionProxy>::create();
84    ProxyConstructor<GloveProxy>::create();
85 //   ProxyConstructor<GestureProxy>::create();
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);     // Add the constructor to the list
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       //<< "   :" << (void*)constructor
102       << " type: " << typeid(*constructor).name() << std::endl
103       << vprDEBUG_FLUSH;
104 }
105
106 // Simply query all proxy constructors registered looking
107 // for one that knows how to load the proxy
108 bool ProxyFactory::recognizeProxy(jccl::ConfigElementPtr element)
109 {
110    return ! (findConstructor(element) == -1);
111 }
112
113 /**
114  * Loads the specified proxy.
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 } // End of gadget namespace
149
Note: See TracBrowser for help on using the browser.