44 #include "../../components_h.h" 46 #include "../../components.h" 51 static vector<ComponentListEntry>* componentListRunTime = NULL;
56 string (*getAttributeFunc)(
const string& attributeName))
66 if (componentListRunTime == NULL)
67 componentListRunTime =
new vector<ComponentListEntry>();
70 cle.componentName = name;
71 cle.Create = createFunc;
72 cle.GetAttribute = getAttributeFunc;
74 componentListRunTime->push_back(cle);
82 delete componentListRunTime;
83 componentListRunTime = NULL;
88 const string& componentNameAndOptionalArgs,
GXemul* gxemul)
94 size_t p = componentName.find(
'(');
95 if (p != string::npos && p > 0) {
96 componentName = componentName.substr(0, p);
98 string argstring = componentNameAndOptionalArgs.substr(p+1);
101 if (argstring[argstring.length()-1] !=
')') {
108 argstring = argstring.substr(0, argstring.length()-1);
116 for (
size_t i=0; i<assignments.size(); ++i) {
118 if (keyAndValue.size() != 2) {
132 while (componentList[i].componentName != NULL) {
133 if (componentName == componentList[i].componentName
134 #ifndef UNSTABLE_DEVEL
138 return componentList[i].
Create(args);
143 for (i=0; componentListRunTime != NULL && i<componentListRunTime->size(); ++i) {
144 if (componentName == (*componentListRunTime)[i].componentName
145 #ifndef UNSTABLE_DEVEL 146 && !(*componentListRunTime)[i].GetAttribute(
"stable").empty()
149 return (*componentListRunTime)[i].Create(args);
164 const string& key = it->first;
165 const string& value = it->second;
167 if (settings.find(key) == settings.end()) {
168 if (createArgs.
gxemul != NULL) {
170 ss <<
"Unknown setting '" << key <<
"'. " 171 "Available settings (with default values) are:\n";
172 for (ComponentCreationSettings::const_iterator it2 = defaultSettings.begin();
173 it2 != defaultSettings.end(); ++it2)
174 ss <<
" " << it2->first <<
" = " << it2->second <<
"\n";
182 settings[key] = value;
190 const string& attributeName)
200 for (i=0; componentListRunTime!=NULL && i<componentListRunTime->size(); ++i) {
201 if (name == (*componentListRunTime)[i].componentName)
202 return (*componentListRunTime)[i].GetAttribute(
211 const string& attributeName)
219 vector<string> result;
223 if ((!onlyTemplates ||
225 #ifndef UNSTABLE_DEVEL
233 for (i=0; componentListRunTime!=NULL && i<componentListRunTime->size(); ++i) {
234 if ((!onlyTemplates ||
235 (*componentListRunTime)[i].
GetAttribute(
"template") ==
"yes")
236 #ifndef UNSTABLE_DEVEL 237 && !(*componentListRunTime)[i].GetAttribute(
"stable").empty()
240 result.push_back((*componentListRunTime)[i].componentName);
252 static void Test_ComponentFactory_Nonexistant()
257 component.
IsNULL() ==
true);
260 static void Test_ComponentFactory_SimpleDummy()
265 component.
IsNULL() ==
false);
273 static void Test_ComponentFactory_FromTemplate()
278 component.
IsNULL() ==
false);
287 clone.IsNULL() ==
false);
290 clone->GetClassName(),
"machine");
292 clone->GetChildren().size() > 0);
295 static void Test_ComponentFactory_HasAttribute()
312 UNITTEST(Test_ComponentFactory_Nonexistant);
313 UNITTEST(Test_ComponentFactory_SimpleDummy);
314 UNITTEST(Test_ComponentFactory_FromTemplate);
315 UNITTEST(Test_ComponentFactory_HasAttribute);
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
static refcount_ptr< Component > CreateComponent(const string &componentNameAndOptionalArgs, GXemul *gxemul=NULL)
Creates a component given a short component name.
Components & GetChildren()
Gets pointers to child components.
string(* GetAttribute)(const string &attributeName)
static vector< string > SplitStringIntoVector(const string &str, const char splitter)
Splits a string with a certain delimiter into a vector of strings.
string GetClassName() const
Gets the class name of the component.
#define UNITTESTS(class)
Helper for unit test case execution.
map< string, string > ComponentCreationSettings
static bool HasAttribute(const string &name, const string &attributeName)
Checks if a component has a specific attribute.
const char * componentName
static void UnregisterAllComponentClasses()
Unregisters all manually registered component classes.
static vector< string > GetAllComponentNames(bool onlyTemplates)
Returns a vector of all available component names.
A factory which creates Component objects.
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.
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
refcount_ptr< Component > Clone() const
Clones the component and all its children.
ComponentCreationSettings componentCreationSettings
static string GetAttribute(const string &name, const string &attributeName)
Gets a specific attribute value for a component.
UI * GetUI()
Gets a pointer to the GXemul instance' active UI.
static bool GetCreationArgOverrides(ComponentCreationSettings &settings, const ComponentCreateArgs &createArgs)
Get override arguments for component creation.
#define UNITTEST(functionname)
Helper for unit test case execution.
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
refcount_ptr< Component >(* Create)(const ComponentCreateArgs &args)