Implemented instance loader.

This commit is contained in:
Andrew 2013-02-18 15:39:01 -06:00
parent 15c7efffa1
commit d3c4db8f34
3 changed files with 90 additions and 3 deletions

View File

@ -15,7 +15,67 @@
#include "instanceloader.h"
#include "instancetype.h"
InstanceLoader::InstanceLoader(QObject *parent) :
QObject(parent)
{
}
InstanceLoader::InstTypeError InstanceLoader::registerInstanceType(InstanceType *type)
{
// Check to see if the type ID exists.
if (m_typeMap.contains(type->typeID()))
return TypeIDExists;
// Set the parent to this.
type->setParent(this);
// Add it to the map.
m_typeMap.insert(type->typeID(), type);
return NoError;
}
InstanceLoader::InstTypeError InstanceLoader::createInstance(Instance *inst,
const InstanceType *type,
const QString &instDir)
{
// Check if the type is registered.
if (!type || findType(type->typeID()) != type)
return TypeNotRegistered;
// Create the instance.
return type->createInstance(inst, instDir);
}
InstanceLoader::InstTypeError InstanceLoader::loadInstance(Instance *inst,
const InstanceType *type,
const QString &instDir)
{
// Check if the type is registered.
if (!type || findType(type->typeID()) != type)
return TypeNotRegistered;
return type->loadInstance(inst, instDir);
}
const InstanceType *InstanceLoader::findType(const QString &id)
{
if (!m_typeMap.contains(id))
return NULL;
else
return m_typeMap[id];
}
InstTypeList InstanceLoader::typeList()
{
InstTypeList typeList;
for (auto iter = m_typeMap.begin(); iter != m_typeMap.end(); iter++)
{
typeList.append(*iter);
}
return typeList;
}

View File

@ -17,10 +17,14 @@
#define INSTANCELOADER_H
#include <QObject>
#include <QMap>
#include <QList>
class InstanceType;
class Instance;
typedef QList<const InstanceType *> InstTypeList;
/*!
* \brief The InstanceLoader is a singleton that manages all of the instance types and handles loading and creating instances.
* Instance types are registered with the instance loader through its registerInstType() function.
@ -41,6 +45,7 @@ public:
* TypeNotRegistered is returned by createInstance() and loadInstance() when the given type is not registered.
* InstExists is returned by createInstance() if the given instance directory is already an instance.
* NotAnInstance is returned by loadInstance() if the given instance directory is not a valid instance.
* WrongInstType is returned by loadInstance() if the given instance directory's type doesn't match the given type.
*/
enum InstTypeError
{
@ -51,7 +56,8 @@ public:
TypeNotRegistered,
InstExists,
NotAnInstance
NotAnInstance,
WrongInstType
};
/*!
@ -84,11 +90,28 @@ public:
* \return An InstTypeError error code.
* TypeNotRegistered if the given type is not registered with the InstanceLoader.
* NotAnInstance if the given instance directory isn't a valid instance.
* WrongInstType if the given instance directory's type isn't the same as the given type.
*/
InstTypeError loadInstance(Instance *inst, const InstanceType *type, const QString &instDir);
/*!
* \brief Finds an instance type with the given ID.
* If one cannot be found, returns NULL.
* \param id The ID of the type to find.
* \return The type with the given ID. NULL if none were found.
*/
const InstanceType *findType(const QString &id);
/*!
* \brief Gets a list of the registered instance types.
* \return A list of instance types.
*/
InstTypeList typeList();
private:
explicit InstanceLoader(QObject *parent = 0);
QMap<QString, InstanceType *> m_typeMap;
};
#endif // INSTANCELOADER_H

View File

@ -34,6 +34,8 @@ class InstanceType : public QObject
public:
explicit InstanceType(QObject *parent = 0);
friend class InstanceLoader;
/*!
* \brief Gets the ID for this instance type.
* By default this is the name of the Instance class that this type
@ -57,6 +59,7 @@ public:
*/
virtual QString description() const = 0;
protected:
/*!
* \brief Creates an instance and stores it in inst.
* \param inst Pointer to store the created instance in.
@ -65,7 +68,7 @@ public:
* TypeNotRegistered if the given type is not registered with the InstanceLoader.
* InstExists if the given instance directory is already an instance.
*/
virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) = 0;
virtual InstanceLoader::InstTypeError createInstance(Instance *inst, const QString &instDir) const = 0;
/*!
* \brief Loads an instance from the given directory.
@ -74,8 +77,9 @@ public:
* \return An InstTypeError error code.
* TypeNotRegistered if the given type is not registered with the InstanceLoader.
* NotAnInstance if the given instance directory isn't a valid instance.
* WrongInstType if the given instance directory's type isn't an instance of this type.
*/
virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) = 0;
virtual InstanceLoader::InstTypeError loadInstance(Instance *inst, const QString &instDir) const = 0;
};
#endif // INSTANCETYPE_H