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

Revision 20244, 5.5 kB (checked in by patrick, 1 year ago)

Merges from the trunk:

r20236: Fix memory leak in and DeviceFactory?.

r20237: Fix memory leak in [PositionProxy?] position filter code.

r20238: Fix a few more position filter memory leaks.

r20239: Test for NULL before deleting.

r20243: Moved destructor implementations [for BaseTypeFactory?,

DeviceFactory?, and PositionProxy?] into the .cpp file. There is no
need for these to be inlined, especially when they are virtual.

  • 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_NETDEVICE_FACTORY_H_
28 #define _GADGET_NETDEVICE_FACTORY_H_
29 //#pragma once
30
31 #include <gadget/gadgetConfig.h>
32 #include <vector>
33 #include <boost/concept_check.hpp>
34
35 #include <gadget/Type/Input.h>
36 #include <jccl/Config/ConfigElementPtr.h>
37 #include <vpr/Util/Singleton.h>
38
39 #include <vpr/Util/Debug.h>
40 #include <vpr/Util/Assert.h>
41
42 namespace gadget
43 {
44
45 /** \class BaseTypeConstructorBase BaseTypeFactory.h gadget/Type/BaseTypeFactory.h
46  *
47  * Base class for virtual construction of devices.
48  * Implementations of this class are registered with the device factory
49  * for each device in the system.
50  */
51 class BaseTypeConstructorBase
52 {
53 public:
54    /**
55     * Constructor.
56     * @post Device is registered.
57     */
58    BaseTypeConstructorBase()
59    {;}
60
61    virtual ~BaseTypeConstructorBase()
62    {;}
63
64    /** Creates the device. */
65    virtual Input* createNetDevice(std::string baseType)
66    {
67       boost::ignore_unused_variable_warning(baseType);
68       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
69          << "ERROR: DeviceConstructorBase::createDevice: Should never be called"
70          << std::endl << vprDEBUG_FLUSH;
71       return NULL;
72    }
73
74    /** Gets the name of the type of element we can create. */
75    virtual std::string getInputTypeName()
76    {
77       return std::string("BaseConstructor: Invalid type");
78    }
79 };
80
81 /** \class BaseTypeFactory BaseTypeFactory.h gadget/Type/BaseTypeFactory.h
82  *
83  * Object used for creating devices.
84  */
85 class GADGET_CLASS_API BaseTypeFactory
86 {
87 private:
88    // Singleton so must be private
89    BaseTypeFactory()
90    {
91       mConstructors = std::vector<BaseTypeConstructorBase*>(0);
92       vprASSERT(mConstructors.size() == 0);
93    }
94
95    ~BaseTypeFactory();
96
97    // This should be replaced with device plugins.
98    /**
99     * @post Devices are loaded that the system knows about.
100     */
101    void hackLoadKnownDevices();
102
103 public:
104    void registerNetDevice(BaseTypeConstructorBase* constructor);
105
106    /**
107     * Queries if the factory knows about the given device.
108     *
109     * @pre base_type is a valid configuration element type.
110     * @param base_type The base type of the config element element we are
111     *                  requesting about knowledge to create.
112     *
113     * @return true if the factory knows how to create the device; false if not.
114     */
115    bool recognizeNetDevice(std::string base_type);
116
117    /**
118     * Loads the specified device.
119     *
120     * @pre recognizeNetDevice(base_type) == true.
121     *
122     * @param base_type The base type of the specification of the device to
123     *                  load.
124     *
125     * @return NULL is returned if the device failed to load.
126     *         Otherwise, a pointer to the loaded device is returned.
127     *
128     * @see recognizeNetDevice
129     */
130    Input* loadNetDevice(std::string base_type);
131
132 private:
133    /**
134     * Finds a constructor for the given device type.
135     * @return -1 is returned if the constructor is not found.
136     *         Otherwise, the index of the constructor is returned.
137     */
138    int   findConstructor(std::string base_type);
139
140    void debugDump();
141
142 private:
143    std::vector<BaseTypeConstructorBase*> mConstructors;  /**<  List of the device constructors */
144
145    vprSingletonHeaderWithInitFunc(BaseTypeFactory, hackLoadKnownDevices);
146 };
147
148 /** \class BaseTypeConstructor BaseTypeFactory.h gadget/Type/BaseTypeFactory.h
149  *
150  * Type-specific input device creator.
151  */
152 template <class DEV>
153 class BaseTypeConstructor : public BaseTypeConstructorBase, public DEV
154 {
155 public:
156    BaseTypeConstructor()
157    {
158       vprASSERT(BaseTypeFactory::instance() != NULL);
159       BaseTypeFactory::instance()->registerNetDevice(this);
160    }
161
162    virtual ~BaseTypeConstructor()
163    {
164       /* Do nothing. */ ;
165    }
166
167    Input* createNetDevice(std::string baseType)
168    {
169       boost::ignore_unused_variable_warning(baseType);
170       DEV* new_dev = new DEV;
171       //bool success = new_dev->config(element);
172       //if(success)
173       //{
174          return new_dev;
175       //}
176       //else
177       //{
178       //   delete new_dev;
179       //   return NULL;
180       //}
181    }
182
183    virtual std::string getInputTypeName()
184    {
185       return DEV::getInputTypeName();
186    }
187
188    /**
189     * Invokes the global scope delete operator.  This is required for proper
190     * releasing of memory in DLLs on Win32.
191     */
192    void operator delete(void* p)
193    {
194       ::operator delete(p);
195    }
196
197 protected:
198    /**
199     * Deletes this object.  This is an implementation of the pure virtual
200     * gadget::Input::destroy() method.
201     */
202    virtual void destroy()
203    {
204       delete this;
205    }
206 };
207
208 } // end namespace gadget
209
210 #endif
Note: See TracBrowser for help on using the browser.