Beispiel #1
0
bool VCProject::writeTemplate(const std::string& filePath, const LabelHandlerFnMap& handlers) const
{
  // Open the template
  pugi::xml_document projDoc;
  pugi::xml_parse_result result = projDoc.load_file(filePath.c_str());
  sbValidate(result, "Failed to open template file: " + filePath);
  pugi::xml_node projRoot = projDoc.first_child();

  for (pugi::xml_node child = projRoot.first_child(); child; child = child.next_sibling()) {
    // Check each child for a VSImporterLabel attribute
    pugi::xml_attribute sblabelAttr = child.attribute("VSImporterLabel");
    std::string sblabelValue = sblabelAttr.value();

    // Get rid of the attribute, if one was found
    if (sblabelAttr) {
      child.remove_attribute(sblabelAttr);
    } else {
      continue;
    }

    // Handle the label
    auto nodeHandlerIt = handlers.find(sblabelValue);
    if (nodeHandlerIt != handlers.end()) {
      callMemberFunction(this, nodeHandlerIt->second)(child);
    } else {
      SBLog::warning() << "Unrecognized VSImporterLabel attribute \"" << sblabelValue << "\" in " << filePath << std::endl;
    }
  }

  // Output tree
  return projDoc.save_file(filePath.c_str(), "  ");
}
/**
 * ---o
 * [1,2,3].map(fn(key,value){return sum+value;}) => [1,3,5]
 */
Object * Collection::rt_map(Runtime & runtime,ObjPtr function){
    // Create new, empty Collection
    Object * obj=callMemberFunction(runtime,this,Consts::IDENTIFIER_fn_constructor,ParameterValues());
    ERef<Collection> newCollectionRef=dynamic_cast<Collection*>(obj);
    if(newCollectionRef.isNull()){
        runtime.error("Collection.map(..) No Contructor found!");
        return NULL;
    }
    for( ERef<Iterator> it=getIterator(); ! it->end() ; it->next()){
        ObjRef key=it->key();
        ObjRef value=it->value();

        ObjRef newValue=runtime.executeFunction(function.get(),NULL,ParameterValues(key,value));
        if(!newValue.isNull())
            newCollectionRef->setValue(key.get(),newValue.get());
    }
    return newCollectionRef.detachAndDecrease();
}
/*!	---o ???	*/
Object * Collection::rt_extract(Runtime & runtime,identifierId functionId,bool decision/*=true*/){
    ERef<Iterator> it=getIterator();

    ObjRef currentValue=NULL;
    while (! it->end()) {
        ObjRef value=it->value();

        if (currentValue.isNull()) {
            currentValue=value;
        } else {
            ObjRef result=callMemberFunction(runtime,value.get(),functionId,ParameterValues(currentValue.get()));
            if(result.toBool()==decision)
                currentValue=value;
        }
        it->next();
    }
    // detach object from "currentValue" without deleting it.
    return currentValue.detachAndDecrease();
}
//! (static)
Object * callMemberFunction(Runtime & rt, ObjPtr obj, const std::string & fnName, const ParameterValues & params) {
	return callMemberFunction(rt, obj, stringToIdentifierId(fnName), params);
}