ComponentFactory.h Source File

Back to the index.

ComponentFactory.h
Go to the documentation of this file.
1 #ifndef COMPONENTFACTORY_H
2 #define COMPONENTFACTORY_H
3 
4 /*
5  * Copyright (C) 2008-2010 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "misc.h"
32 
33 #include "Component.h"
34 #include "UnitTest.h"
35 
36 
37 /**
38  * \brief A factory which creates Component objects.
39  *
40  * The main usage of the %ComponentFactory is simply:
41  * "Hey, give me an XYZ component."
42  * and the %ComponentFactory returns a reference counted pointer to something.
43  * This can be a single Component, or it can be something more complex (a
44  * %Component with children, etc).
45  *
46  * This mechanism is also used for templates. If the user wishes to create
47  * a "testmips" machine, what actually happens is that CreateComponent() is
48  * called with "testmips" as the argument, and it returns an entire tree,
49  * which may contain something like:<pre>
50  * machine0 [testmips]
51  * |-- cpu0
52  * |-- ram0
53  * \-- framebuffer0
54  * </pre>
55  *
56  * (The example above is semi-bogus, but should illustrate the point of
57  * CreateComponent().)
58  */
60  : public UnitTestable
61 {
62 public:
63  /**
64  * \brief Creates a component given a short component name.
65  *
66  * componentNameAndOptionalArgs may be e.g.
67  * <tt>"testmips(cpu=R4400,cpus=4)"</tt>.
68  *
69  * @param componentNameAndOptionalArgs The component name, e.g. "dummy",
70  * optionally followed by arguments in parentheses.
71  * @param gxemul A pointer to a GXemul instance. May be NULL.
72  * @return A reference counted Component pointer. This is set to the
73  * newly created component on success. On failure it is set to
74  * NULL.
75  */
77  const string& componentNameAndOptionalArgs, GXemul* gxemul = NULL);
78 
79  /**
80  * \brief Gets a specific attribute value for a component.
81  *
82  * @param name The name of a component, e.g. "testmips".
83  * @param attributeName The attribute, e.g. "template" or "machine".
84  * @return A string containing the attribute value. This is an
85  * empty string if the component does not exist, or if the
86  * attribute was not set for the component.
87  */
88  static string GetAttribute(const string& name, const string&
89  attributeName);
90 
91  /**
92  * \brief Checks if a component has a specific attribute.
93  *
94  * @param name The name of a component, e.g. "testmips".
95  * @param attributeName The attribute, e.g. "template" or "machine".
96  * @return True if the name exists and the attribute is not an
97  * empty string, false otherwise
98  * (both if the name is not known, and if it is known but
99  * the attribute was not set).
100  */
101  static bool HasAttribute(const string& name, const string&
102  attributeName);
103 
104  /**
105  * \brief Returns a vector of all available component names.
106  *
107  * @param onlyTemplates If true, only those component names that
108  * are templates are returned.
109  * @return A vector of all available component names.
110  */
111  static vector<string> GetAllComponentNames(bool onlyTemplates);
112 
113  /**
114  * \brief Adds a new component class to the factory at runtime.
115  *
116  * Component classes added using this function are then available
117  * when using e.g. CreateComponent.
118  *
119  * @param name The name of the component class. Must be a const char*
120  * which lives for the lifetime of the program.
121  * @param createFunc A pointer to the component's Create function.
122  * @param getAttributeFunc A pointer to the component's GetAttribute
123  * function.
124  * @return True if the component class was registered, false
125  * if the name was already in use.
126  */
127  static bool RegisterComponentClass(const char* name,
128  refcount_ptr<Component> (*createFunc)(const ComponentCreateArgs& args),
129  string (*getAttributeFunc)(const string& attributeName));
130 
131  /**
132  * \brief Get override arguments for component creation.
133  */
135  const ComponentCreateArgs& createArgs);
136 
137  /**
138  * \brief Unregisters all manually registered component classes.
139  */
140  static void UnregisterAllComponentClasses();
141 
142 
143  /********************************************************************/
144 
145  static void RunUnitTests(int& nSucceeded, int& nFailures);
146 };
147 
148 
149 #endif // COMPONENTFACTORY_H
ComponentFactory::GetCreationArgOverrides
static bool GetCreationArgOverrides(ComponentCreationSettings &settings, const ComponentCreateArgs &createArgs)
Get override arguments for component creation.
Definition: ComponentFactory.cc:151
GXemul
The main emulator class.
Definition: GXemul.h:55
refcount_ptr< Component >
ComponentFactory::GetAllComponentNames
static vector< string > GetAllComponentNames(bool onlyTemplates)
Returns a vector of all available component names.
Definition: ComponentFactory.cc:212
ComponentFactory::RegisterComponentClass
static bool RegisterComponentClass(const char *name, refcount_ptr< Component >(*createFunc)(const ComponentCreateArgs &args), string(*getAttributeFunc)(const string &attributeName))
Adds a new component class to the factory at runtime.
Definition: ComponentFactory.cc:54
ComponentFactory
A factory which creates Component objects.
Definition: ComponentFactory.h:61
ComponentFactory::RunUnitTests
static void RunUnitTests(int &nSucceeded, int &nFailures)
ComponentFactory::UnregisterAllComponentClasses
static void UnregisterAllComponentClasses()
Unregisters all manually registered component classes.
Definition: ComponentFactory.cc:80
misc.h
UnitTest.h
ComponentFactory::CreateComponent
static refcount_ptr< Component > CreateComponent(const string &componentNameAndOptionalArgs, GXemul *gxemul=NULL)
Creates a component given a short component name.
Definition: ComponentFactory.cc:87
Component.h
settings
Definition: settings.cc:57
ComponentFactory::GetAttribute
static string GetAttribute(const string &name, const string &attributeName)
Gets a specific attribute value for a component.
Definition: ComponentFactory.cc:184
ComponentCreationSettings
map< string, string > ComponentCreationSettings
Definition: Component.h:46
UnitTestable
Base class for unit testable classes.
Definition: UnitTest.h:75
ComponentFactory::HasAttribute
static bool HasAttribute(const string &name, const string &attributeName)
Checks if a component has a specific attribute.
Definition: ComponentFactory.cc:205
ComponentCreateArgs
Definition: Component.h:49

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18