Skip to content
This repository has been archived by the owner on Oct 9, 2018. It is now read-only.

Commit

Permalink
Added StandardComponentFactory, StandardStimulusFactory, and related …
Browse files Browse the repository at this point in the history
…classes. Removed unused ObjectFactory source files.
  • Loading branch information
cstawarz committed Mar 24, 2011
1 parent e771f6b commit de8941a
Show file tree
Hide file tree
Showing 14 changed files with 614 additions and 190 deletions.
9 changes: 9 additions & 0 deletions Core/ComponentRegistries/ComponentRegistry.h
Expand Up @@ -94,6 +94,12 @@ namespace mw {
ComponentFactory *factory);

shared_ptr<ComponentFactory> getFactory(std::string type_name);

template< template<typename> class FactoryTemplate, typename ComponentType >
void registerFactory() {
FactoryTemplate<ComponentType> *factory = new FactoryTemplate<ComponentType>();
registerFactory(factory->getComponentInfo().getSignature(), (ComponentFactory *)factory);
}


// Instance-oriented methods
Expand Down Expand Up @@ -231,6 +237,9 @@ namespace mw {
};


// TODO: Once existing code has been updated to use this typedef,
// it should be redefined as boost::shared_ptr<ComponentRegistry>
typedef ComponentRegistry* ComponentRegistryPtr;


}
Expand Down
78 changes: 66 additions & 12 deletions Core/PluginServices/ComponentFactory.cpp
Expand Up @@ -10,20 +10,47 @@
#include "ComponentFactory.h"
#include "GenericVariable.h"
#include "ComponentRegistry.h"
#include "ParameterValue.h"

using namespace mw;

namespace mw {
const mw::Component *InvalidObject = new mw::Component();
BEGIN_NAMESPACE(mw)


const mw::Component *InvalidObject = new mw::Component();


void ComponentFactory::processParameters(StdStringMap &parameters, ComponentRegistryPtr reg, ParameterValueMap &values)
{
requireAttributes(parameters, info.getRequiredParameters());

const ParameterInfoMap &infoMap = info.getParameters();

for (StdStringMap::iterator param = parameters.begin(); param != parameters.end(); param++) {
const std::string &name = (*param).first;
ParameterInfoMap::const_iterator iter = infoMap.find(name);

if ((iter == infoMap.end()) && !isInternalParameter(name)) {
std::string referenceID("<unknown object>");
if (parameters.find("reference_id") != parameters.end()) {
referenceID = parameters["reference_id"];
}
throw UnknownAttributeException(referenceID, name);
}

const std::string &value = (*param).second;
const ParameterInfo &info = (*iter).second;

values.insert(std::make_pair(name, ParameterValue(value, reg)));
}
}

void ComponentFactory::requireAttributes(std::map<std::string, std::string> parameters,
std::vector<std::string> requiredAttributes) {
for(std::vector<std::string>::const_iterator i = requiredAttributes.begin();

void ComponentFactory::requireAttributes(StdStringMap parameters, StdStringVector requiredAttributes)
{
for(StdStringVector::const_iterator i = requiredAttributes.begin();
i != requiredAttributes.end();
++i) {
std::map<std::string, std::string>::const_iterator attribute = parameters.find(*i);
StdStringMap::const_iterator attribute = parameters.find(*i);
if(attribute == parameters.end()) {
string reference_id("<unknown object>");
if(parameters.find("reference_id") != parameters.end()){
Expand All @@ -41,14 +68,41 @@ void ComponentFactory::requireAttributes(std::map<std::string, std::string> para
}
}

void ComponentFactory::checkAttribute(const shared_ptr<mw::Component> &component,
const std::string &refID,
const std::string &name,
const std::string &value) {

void ComponentFactory::checkAttribute(shared_ptr<mw::Component> component,
const std::string &refID,
const std::string &name,
const std::string &value)
{
if(component == 0) {
throw MissingReferenceException(refID, name, value);
} else if(component.get() == InvalidObject) {
throw InvalidReferenceException(refID, name, value);
}
}


END_NAMESPACE(mw)























78 changes: 64 additions & 14 deletions Core/PluginServices/ComponentFactory.h
Expand Up @@ -14,11 +14,16 @@

#include "Component.h"
#include "ComponentFactoryException.h"
#include "ComponentInfo.h"
#include <boost/shared_ptr.hpp>
#include <map>
#include <string>
#include <vector>
namespace mw {


BEGIN_NAMESPACE(mw)


extern const mw::Component *InvalidObject;

#define REQUIRE_ATTRIBUTES(parameters, attributes...) {\
Expand All @@ -45,27 +50,72 @@ using namespace boost;
using namespace std;

class ComponentRegistry; // forward declaration
class ParameterValue;
class Variable;


class ComponentFactory {

public:
typedef std::map<std::string, std::string> StdStringMap;
typedef std::vector<std::string> StdStringVector;

ComponentFactory() { }
virtual ~ComponentFactory() { }

const ComponentInfo& getComponentInfo() const {
return info;
}

virtual shared_ptr<mw::Component> createObject(StdStringMap parameters, ComponentRegistry *reg) {
return shared_ptr<mw::Component>();
}

protected:
virtual void requireAttributes(map<string, string> parameters,
vector<string> attributes);
virtual void checkAttribute(const shared_ptr<mw::Component> &component,
static bool isInternalParameter(const std::string &name) {
// Identify parameters added by the parser
return ((name == "reference_id") ||
(name == "type") ||
(name == "variable_assignment") ||
(name == "working_path") ||
(name == "xml_document_path"));
}

void processParameters(StdStringMap &parameters, ComponentRegistry *reg, Map<ParameterValue> &values);
virtual void requireAttributes(StdStringMap parameters, StdStringVector attributes);
virtual void checkAttribute(shared_ptr<mw::Component> component,
const string &refID,
const string &name,
const string &value);


public:
ComponentFactory(){ }
virtual ~ComponentFactory(){ }

virtual shared_ptr<mw::Component> createObject(std::map<std::string, std::string> parameters,
ComponentRegistry *reg){
return shared_ptr<mw::Component>();
}
ComponentInfo info;

};
}


END_NAMESPACE(mw)


#endif






















21 changes: 19 additions & 2 deletions Core/PluginServices/ComponentFactoryException.h
Expand Up @@ -7,9 +7,11 @@
*
*/

#include "MWorksMacros.h"

#ifndef COMPONENT_FACTORY_EXCEPTION_H_
#define COMPONENT_FACTORY_EXCEPTION_H_
namespace mw {
BEGIN_NAMESPACE(mw)
class ComponentFactoryException : public std::exception {
protected:
std::string _what;
Expand Down Expand Up @@ -45,6 +47,21 @@ class MissingAttributeException : public ComponentFactoryException {
}
};

class UnknownAttributeException : public ComponentFactoryException {
public:
UnknownAttributeException(const std::string &referenceID, const std::string &attributeName) :
ComponentFactoryException(referenceID)
{
_what = "Unknown attribute: \"" + attributeName + "\"";
}

virtual ~UnknownAttributeException() throw() {}

virtual const char* what() const throw() {
return _what.c_str();
}
};

class InvalidReferenceException : public ComponentFactoryException {
public:
InvalidReferenceException(const std::string &referenceID,
Expand Down Expand Up @@ -92,6 +109,6 @@ class MissingReferenceException : public ComponentFactoryException {
return _what.c_str();
}
};
}
END_NAMESPACE(mw)

#endif
92 changes: 92 additions & 0 deletions Core/PluginServices/ComponentInfo.h
@@ -0,0 +1,92 @@
/*
* ComponentInfo.h
* MWorksCore
*
* Created by Christopher Stawarz on 3/24/11.
* Copyright 2011 MIT. All rights reserved.
*
*/

#ifndef _MW_COMPONENT_INFO_H
#define _MW_COMPONENT_INFO_H

#include <string>
#include <vector>

#include "ParameterInfo.h"


BEGIN_NAMESPACE(mw)


class ComponentInfo {

public:
typedef std::vector<std::string> StdStringVector;

void setSignature(const std::string &newSignature) {
signature = newSignature;
}

void addParameter(const std::string &name, const std::string &defaultValue) {
addParameter(name, ParameterInfo(defaultValue));
}

void addParameter(const std::string &name, bool required = true) {
addParameter(name, ParameterInfo(required));
}

void addParameter(const std::string &name, const ParameterInfo &info) {
parameters[name] = info;
if (info.isRequired()) {
requiredParameters.push_back(name);
}
}

const std::string& getSignature() const {
return signature;
}

const ParameterInfoMap& getParameters() const {
return parameters;
}

const StdStringVector& getRequiredParameters() const {
return requiredParameters;
}

private:
std::string signature;
ParameterInfoMap parameters;
StdStringVector requiredParameters;

};


END_NAMESPACE(mw)


#endif























0 comments on commit de8941a

Please sign in to comment.