diff options
author | Vicnet <vo.publique@gmail.com> | 2013-10-07 10:34:30 (GMT) |
---|---|---|
committer | Vicnet <vo.publique@gmail.com> | 2013-10-07 14:04:42 (GMT) |
commit | 3dfb76a56d2cc38bda988eeb2cdf51ccbfb4df15 (patch) | |
tree | 5c7f9af46a2b1da3aa55e3fd12ce663298336342 /src/control.cc | |
parent | 014f48c1e8a5daa600a89635d3998ee2feb08a84 (diff) |
add vector parameter to children()
Diffstat (limited to 'src/control.cc')
-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 |