diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/control.cc | 63 | 
1 files changed, 47 insertions, 16 deletions
diff --git a/src/control.cc b/src/control.cc index 0887cc7..7293963 100644 --- a/src/control.cc +++ b/src/control.cc @@ -24,6 +24,7 @@   *   */ +#include <boost/foreach.hpp>  #include "module.h"  #include "node.h"  #include "evalcontext.h" @@ -33,6 +34,10 @@  #include <sstream>  #include "mathc99.h" + +#define foreach BOOST_FOREACH + +  class ControlModule : public AbstractModule  {  public: // types @@ -56,6 +61,8 @@ public: // methods  						 const Context *ctx, const EvalContext *evalctx);  	static const EvalContext* getLastModuleCtx(const EvalContext *evalctx); +	 +	static AbstractNode* getChild(const Value& value, const EvalContext* modulectx);  private: // data  	Type type; @@ -119,6 +126,36 @@ const EvalContext* ControlModule::getLastModuleCtx(const EvalContext *evalctx)  	return NULL;  } +// static +AbstractNode* ControlModule::getChild(const Value& value, const EvalContext* modulectx) +{ +	if (value.type()!=Value::NUMBER) { +		// Invalid parameter +		// (e.g. first child of difference is invalid) +		PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number.", value.toString()); +		return NULL; +	} +	double v; +	if (!value.getDouble(v)) { +		PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number.", value.toString()); +		return NULL; +	} +		 +	int n = trunc(v); +	if (n < 0) { +		PRINTB("WARNING: Negative children index (%d) not allowed", n); +		return NULL; // Disallow negative child indices +	} +	if (n>=(int)modulectx->numChildren()) { +		// How to deal with negative objects in this case? +		// (e.g. first child of difference is invalid) +		PRINTB("WARNING: Children index (%d) out of bounds (%d children)" +			, n % modulectx->numChildren()); +	} +	// OK +	return modulectx->getChild(n)->evaluate(modulectx); +} +  AbstractNode *ControlModule::instantiate(const Context* /*ctx*/, const ModuleInstantiation *inst, const EvalContext *evalctx) const  {  	AbstractNode *node = NULL; @@ -176,23 +213,17 @@ AbstractNode *ControlModule::instantiate(const Context* /*ctx*/, const ModuleIns  			// one (or more ignored) parameter  			const Value& value = evalctx->getArgValue(0);  			if (value.type() == Value::NUMBER) { -				double v; -				if (!value.getDouble(v)) { -					PRINTB("WARNING: Bad parameter type (%s) for children, only accept: empty, number.", value.toString()); -					return NULL; -				} -				int n = trunc(v); -				if (n < 0) { -					PRINTB("WARNING: Negative children index (%d) not allowed", n); -					return NULL; // Disallow negative child indices -				} -				if (n>=(int)modulectx->numChildren()) { -					// How to deal with negative objects in this case? -					// (e.g. first child of difference is invalid) -					PRINTB("WARNING: Children index (%d) out of bounds (%d children)" -							, n % modulectx->numChildren()); +				return getChild(value,modulectx); +			} +			if (value.type() == Value::VECTOR) { +				AbstractNode* node = new AbstractNode(inst); +				const Value::VectorType& vect = value.toVector(); +				foreach (const Value::VectorType::value_type& vectvalue, vect) { +					AbstractNode* childnode = getChild(vectvalue,modulectx); +					if (childnode==NULL) continue; // error +					node->children.push_back(childnode);  				} -				return modulectx->getChild(n)->evaluate(modulectx); +				return node;  			}  			else {  				// Invalid parameter  | 
