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

Revision 19729, 5.5 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_PROXY_FACTORY_H_
28 #define _GADGET_PROXY_FACTORY_H_
29 //#pragma once
30
31 #include <gadget/gadgetConfig.h>
32 #include <vector>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/enable_shared_from_this.hpp>
35
36 #include <vpr/Util/Singleton.h>
37 #include <jccl/Config/ConfigElementPtr.h>
38 #include <jccl/RTRC/DependencyManager.h>
39
40 #include <gadget/ProxyDepChecker.h>
41 #include <gadget/Type/Proxy.h>
42
43
44 namespace gadget
45 {
46
47 /** \class ProxyConstructorBase ProxyFactory.h gadget/ProxyFactory.h
48  *
49  * Base class for virtual construction of proxies.
50  * Implementations of this class are registered with the proxy factory
51  * for each proxy type in the system.
52  */
53 class ProxyConstructorBase
54    : public boost::enable_shared_from_this<ProxyConstructorBase>
55 {
56 protected:
57    /**
58     * Constructor.
59     * @post We have been registered with the proxy factory.
60     */
61    ProxyConstructorBase();
62
63 public:
64    virtual ~ProxyConstructorBase();
65
66    /**
67     * Creates the proxy.
68     * @return NULL if the proxy failed creation or configuration.
69     */
70    virtual Proxy* createProxy(jccl::ConfigElementPtr element) const = 0;
71
72    /** Gets the string definition of the type of element we can create. */
73    virtual std::string getElementType() const = 0;
74 };
75
76
77 /** \class ProxyConstructor ProxyFactory.h gadget/ProxyFactory.h
78  *
79  * Type-specific proxy constructor.
80  */
81 template <class PROXY>
82 class ProxyConstructor : public ProxyConstructorBase
83 {
84 public:
85    static boost::shared_ptr<ProxyConstructorBase> create();
86
87    virtual ~ProxyConstructor()
88    {
89       /* Do nothing. */ ;
90    }
91
92    /**
93     * Creates the proxy.
94     * @return NULL if proxy failed creation or configuration.
95     */
96    Proxy* createProxy(jccl::ConfigElementPtr element) const
97    {
98       PROXY* new_proxy = new PROXY;             // Create new proxy
99       bool success = new_proxy->config(element);  // Attempt to configure it
100                                                   // config calls inputmgr registrator
101
102       if(success)          // Configured succesfully
103       {
104          return new_proxy;
105       }
106       else                 // Failed
107       {
108   //XXX//       delete new_proxy;
109          return NULL;
110       }
111    }
112
113    std::string getElementType() const
114    {
115       return PROXY::getElementType();
116    }
117
118 protected:
119    ProxyConstructor()
120       : ProxyConstructorBase()
121    {
122       /* Do nothing. */ ;
123    }
124 };
125
126
127 /** \class ProxyFactory ProxyFactory.h gadget/ProxyFactory.h
128  *
129  * Object used for creating proxies.
130  * @note Singleton
131  */
132 class ProxyFactory
133 {
134 private:
135    /** Singleton so must be private. */
136    ProxyFactory() {;}
137
138    ~ProxyFactory();
139
140    /**
141     * Registers the proxies that the system knows about.
142     * @post All known proxies are registered with this factory.
143     */
144    void loadKnownProxies();
145
146 public:
147    /**
148     * Registers a proxy constructor object with this factory.  In
149     * Gadgeteer 1.0, the type of \p constructor was a raw pointer to a
150     * gadget::ProxyConstructorBase object.
151     *
152     * @param constructor The proxy constructor object that will be used later
153     *                    to create a specialization of gadget::Proxy.
154     *
155     * @see loadProxy()
156     *
157     * @since 1.1.2
158     */
159    void registerProxy(boost::shared_ptr<ProxyConstructorBase> constructor);
160
161    /**
162     * Queries if the factory knows about the given proxy.
163     *
164     * @pre \p element != NULL, element is a valid config element.
165     * @param element The element we are requesting about knowledge to create.
166     * @return true if the factory knows how to create the proxy; false if not.
167     */
168    bool recognizeProxy(jccl::ConfigElementPtr element);
169
170    /**
171     * Loads the specified proxy.
172     *
173     * @pre recognizeDevice(element) == true.
174     * @param element The specification of the proxy to load.
175     * @return NULL is returned if the proxy failed to load.
176     *         Otherwise, a pointer to the loaded proxy is returned.
177     */
178    Proxy* loadProxy(jccl::ConfigElementPtr element);
179
180 private:
181
182    /**
183     * Finds a constructor for the given proxy type.
184     * @return -1 is returned if the constructor is not found.
185     *         Otherwise, the index of the constructor is returned.
186     */
187    int findConstructor(jccl::ConfigElementPtr element);
188
189 private:
190    ProxyDepChecker mDepChecker;
191
192    /** List of all the proxy constructors. */
193    std::vector< boost::shared_ptr<ProxyConstructorBase> > mConstructors;
194
195    vprSingletonHeaderWithInitFunc(ProxyFactory,loadKnownProxies);
196 };
197
198 } // end namespace
199
200 #endif
Note: See TracBrowser for help on using the browser.