From d3c4db8f3444c8531cb5a02ce3c95d5673ffb1a5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Feb 2013 15:39:01 -0600 Subject: [PATCH] Implemented instance loader. --- data/inst/instanceloader.cpp | 60 ++++++++++++++++++++++++++++++++++++ data/inst/instanceloader.h | 25 ++++++++++++++- data/inst/instancetype.h | 8 +++-- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/data/inst/instanceloader.cpp b/data/inst/instanceloader.cpp index 59fed951..bc43c061 100644 --- a/data/inst/instanceloader.cpp +++ b/data/inst/instanceloader.cpp @@ -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; +} diff --git a/data/inst/instanceloader.h b/data/inst/instanceloader.h index 943a9b2c..1a8a598f 100644 --- a/data/inst/instanceloader.h +++ b/data/inst/instanceloader.h @@ -17,10 +17,14 @@ #define INSTANCELOADER_H #include +#include +#include class InstanceType; class Instance; +typedef QList 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 m_typeMap; }; #endif // INSTANCELOADER_H diff --git a/data/inst/instancetype.h b/data/inst/instancetype.h index 1f187310..ec3f5b87 100644 --- a/data/inst/instancetype.h +++ b/data/inst/instancetype.h @@ -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