From bf9b5c550234e2a47e2eaec7f88328250199dd98 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Wed, 26 Mar 2014 18:03:54 +0100 Subject: [PATCH] Extended PluginManager::create() to be able to work with references and ordered dictionaries --- src/libpython/core.cpp | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/libpython/core.cpp b/src/libpython/core.cpp index 32454095..a9736f63 100644 --- a/src/libpython/core.cpp +++ b/src/libpython/core.cpp @@ -527,13 +527,13 @@ static bp::object pluginmgr_createobject_2(PluginManager *mgr, const Class *cls, return cast(mgr->createObject(cls, props)); } -static ConfigurableObject *pluginmgr_create(PluginManager *manager, bp::dict dict) { +static ConfigurableObject *pluginmgr_create_helper(PluginManager *manager, bp::dict dict, std::map &objs) { Properties properties; - bp::list list = dict.items(); + bp::list items = dict.items(); std::map children; - for (int i=0; i(list[i]); + for (bp::stl_input_iterator it(items), end; it!=end; ++it) { + bp::tuple tuple = *it; std::string name = bp::extract(tuple[0]); bp::extract extractDict(tuple[1]); bp::extract extractString(tuple[1]); @@ -544,8 +544,13 @@ static ConfigurableObject *pluginmgr_create(PluginManager *manager, bp::dict dic SLog(EError, "'type' property must map to a string!"); else properties.setPluginName(extractString()); + } else if (name == "id") { + if (!extractString.check()) + SLog(EError, "'id' property must map to a string!"); + else + properties.setID(extractString()); } else if (extractDict.check()) { - children[name] = pluginmgr_create(manager, extractDict()); + children[name] = pluginmgr_create_helper(manager, extractDict(), objs); } else if (extractConfigurableObject.check()) { children[name] = extractConfigurableObject(); } else { @@ -554,10 +559,23 @@ static ConfigurableObject *pluginmgr_create(PluginManager *manager, bp::dict dic } ConfigurableObject *object; - if (properties.getPluginName() == "scene") + if (properties.getPluginName() == "scene") { object = new Scene(properties); - else + } else if (properties.getPluginName() == "ref") { + std::string id = properties.getID(); + if (id == "unnamed") + SLog(EError, "id parameter of reference is missing!"); + if (objs.find(id) == objs.end()) + SLog(EError, "Could not find referenced object with id \"%s\"", id.c_str()); + return objs[id]; + } else { object = manager->createObject(properties); + } + + if (properties.getID() != "unnamed") { + objs[properties.getID()] = object; + object->incRef(); + } for (std::map::iterator it = children.begin(); it != children.end(); ++it) { @@ -569,6 +587,15 @@ static ConfigurableObject *pluginmgr_create(PluginManager *manager, bp::dict dic return object; } +static ConfigurableObject *pluginmgr_create(PluginManager *manager, bp::dict dict) { + std::map objs; + ConfigurableObject *result = pluginmgr_create_helper(manager, dict, objs); + for (std::map::iterator it = objs.begin(); + it != objs.end(); ++it) + it->second->decRef(); + return result; +} + static bp::tuple mkCoordinateSystem(const Vector &n) { Vector s, t; coordinateSystem(n, s, t); @@ -1032,6 +1059,7 @@ void export_core() { coreModule.attr("Epsilon") = Epsilon; coreModule.attr("ShadowEpsilon") = ShadowEpsilon; coreModule.attr("DeltaEpsilon") = DeltaEpsilon; + coreModule.attr("SPECTRUM_SAMPLES") = SPECTRUM_SAMPLES; bp::class_("Class", bp::no_init) .def("getName", &Class::getName, BP_RETURN_CONSTREF)