root/juggler/branches/2.2/modules/gadgeteer/gadget/Type/DeviceInterface.h

Revision 19729, 6.8 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 #ifndef _GADGET_DEVICE_INTERFACE_H_
28 #define _GADGET_DEVICE_INTERFACE_H_
29
30 #include <gadget/gadgetConfig.h>
31 #include <vector>
32 #include <gadget/Util/Debug.h>
33
34 #include <boost/concept_check.hpp>
35
36 namespace gadget
37 {
38
39 class Proxy;
40 class AnalogProxy;
41 class DigitalProxy;
42 class GestureProxy;
43 class GloveProxy;
44 class KeyboardMouseProxy;
45 class PositionProxy;
46 class StringProxy;
47 class CommandProxy;
48
49 /** \class BaseDeviceInterface DeviceInterface.h gadget/Type/DeviceInterface.h
50  *
51  * Base class for simplified proxy interfaces.  Device interfaces are wrappers
52  * that provide an easier way to access proxy objects from within user
53  * applications.  Users can simply declare a local interface variable and use
54  * it as a smart pointer for the proxy.
55  *
56  * @note The init() function should be called in the init() function of the
57  *       user application.
58  */
59 class GADGET_CLASS_API BaseDeviceInterface
60 {
61 public:
62    BaseDeviceInterface();
63
64    virtual ~BaseDeviceInterface();
65
66    /** Copy constructor. */
67    BaseDeviceInterface(const BaseDeviceInterface& other);
68
69    /**
70     * Initializes the object.
71     *
72     * @param proxyName String name of the proxy to connect to.
73     */
74    void init(const std::string& proxyName);
75
76    /**
77     * Refreshes the interface based on the current configuration.
78     *
79     * This method is called by refreshAllInterfaces when system
80     * @post (mProxyIndex == -1) ==> Proxy not initialized yet.<br>
81     *       (mProxyIndex != -1) ==> mProxyName has name of device && local
82     *       proxy pointer is set to the device.
83     */
84    virtual void refresh();
85
86    /** Returns the name of the proxy. */
87    std::string getProxyName() const
88    {
89       return mProxyName;
90    }
91
92    /** Identifies whether this device interface is connected to a proxy. */
93    bool isConnected() const
94    {
95       return (NULL != mProxyPtr);
96    }
97
98 protected:
99    Proxy*      mProxyPtr;   /**<  Ptr to the proxy */
100    std::string mProxyName;  /**< The name of the proxy (or alias) we are looking at */
101    bool        mNameSet;    /**< Has the user set a name?? */
102
103 public:
104    /**
105     * Refreshes all the known device interface objects.
106     *
107     * @since 1.1.20
108     *
109     * @note Prior to version 1.1.20, this function was called
110     *       refreshAllDevices().
111     */
112    static void refreshAllInterfaces();
113
114 private:    // Static information
115    /* We need to keep track of all the allocated device interfaces
116     * so we can update them when the system reconfigures itself
117     */
118    static void addDevInterface(BaseDeviceInterface* dev);
119    static void removeDevInterface(BaseDeviceInterface* dev);
120
121    static std::vector<BaseDeviceInterface*> mAllocatedDevices;
122 };
123
124
125 // ---- Type-specific interfaces ----
126
127 /** \class DeviceInterface DeviceInterface.h gadget/Type/DeviceInterface.h
128  *
129  * Type-specific device interface.
130  */
131 template<class PROXY_TYPE>
132 class DeviceInterface : public BaseDeviceInterface
133 {
134 public:
135    DeviceInterface(const DeviceInterface& other)
136       : BaseDeviceInterface(other)
137    {
138       if (other.mTypeSpecificProxy != NULL)
139       {
140          mTypeSpecificProxy = other.mTypeSpecificProxy;
141       }
142       else
143       {
144          mTypeSpecificProxy = &mDummyProxy;
145       }
146    }
147
148 public:
149    DeviceInterface()
150       : BaseDeviceInterface()
151    {
152       mTypeSpecificProxy = &mDummyProxy;
153    }
154
155    /**
156     * @name Smart pointer operator overloads.
157     *
158     * Device interfaces make use of the Smart Pointer design pattern.  Access
159     * to the contained device proxy must occur through one of these operators.
160     */
161    //@{
162    /**
163     * Member selection (via pointer) operator overload.
164     *
165     * @pre init() has been invoked.
166     *
167     * @see init()
168     */
169    PROXY_TYPE* operator->()
170    {
171       return mTypeSpecificProxy;
172    }
173
174    /**
175     * Dereference operator overload.
176     *
177     * @pre init() has been invoked.
178     *
179     * @see init()
180     */
181    PROXY_TYPE& operator*()
182    {
183       return *(mTypeSpecificProxy);
184    }
185    //@}
186
187    /** Returns the underlying proxy to which we are connected. */
188    PROXY_TYPE* getProxy()
189    {
190       return mTypeSpecificProxy;
191    }
192
193    /** Sets the proxy to an explicit proxy. */
194    void setProxy(PROXY_TYPE* proxy)
195    {
196       vprASSERT(NULL != proxy);
197       mProxyName = proxy->getName();    // Set the name
198       mNameSet = true;
199       this->refresh();
200
201       // Verify we found the correct proxy
202       vprASSERT(mTypeSpecificProxy == proxy && "Found incorrect proxy for dev interface");
203    }
204
205    virtual void refresh()
206    {
207       BaseDeviceInterface::refresh();
208       if(mProxyPtr != NULL)
209       {
210          mTypeSpecificProxy = dynamic_cast<PROXY_TYPE*>(mProxyPtr);
211          if(NULL == mTypeSpecificProxy)
212          {
213             vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
214                << "[gadget::DeviceInterface::refresh()] Tried to point at "
215                << "proxy of incorrect type named '" << mProxyName << "'"
216                << " it was type:" << typeid(mProxyPtr).name()
217                << std::endl << vprDEBUG_FLUSH;
218          }
219       }
220
221       // If either one of the proxy pointers are NULL, then use a dummy
222       if((NULL == mProxyPtr) || (NULL == mTypeSpecificProxy))
223       {
224          mTypeSpecificProxy = &mDummyProxy;
225       }
226    }
227
228 private:
229    PROXY_TYPE*    mTypeSpecificProxy;   /**< The proxy that is being wrapped */
230    PROXY_TYPE     mDummyProxy;
231 };
232
233
234 // --- Typedefs to the old types --- //
235 typedef DeviceInterface<AnalogProxy>      AnalogInterface;
236 typedef DeviceInterface<DigitalProxy>     DigitalInterface;
237 typedef DeviceInterface<GestureProxy>     GestureInterface;
238 typedef DeviceInterface<GloveProxy>       GloveInterface;
239 typedef DeviceInterface<KeyboardMouseProxy> KeyboardMouseInterface;
240 typedef DeviceInterface<PositionProxy>    PositionInterface;
241 typedef DeviceInterface<CommandProxy>     CommandInterface;
242 typedef DeviceInterface<StringProxy>      StringInterface;
243
244 } // end namespace
245
246 #endif
Note: See TracBrowser for help on using the browser.