From 4562d98154897133fd9486455d33a7831ce82ce6 Mon Sep 17 00:00:00 2001 From: Mansard <nmansard@laas.fr> Date: Fri, 11 Feb 2011 10:58:39 +0100 Subject: [PATCH] Changed the call to create_entity: when the required creation names already exists, check is the existing object is of the same class. If it is, returns the existing object instead of creating a new oine. --- src/entity-py.cc | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/entity-py.cc b/src/entity-py.cc index e3e5522..976a2b8 100644 --- a/src/entity-py.cc +++ b/src/entity-py.cc @@ -20,8 +20,9 @@ #include <dynamic-graph/factory.h> #include <../src/convert-dg-to-py.hh> -#include "dynamic-graph/command.h" -#include "dynamic-graph/value.h" +#include <dynamic-graph/command.h> +#include <dynamic-graph/value.h> +#include <dynamic-graph/pool.h> using dynamicgraph::Entity; using dynamicgraph::SignalBase; @@ -51,13 +52,23 @@ namespace dynamicgraph { return NULL; Entity* obj = NULL; - try { - obj = dynamicgraph::g_factory.newEntity(std::string(className), - std::string(instanceName)); - } catch (std::exception& exc) { - PyErr_SetString(error, exc.what()); - return NULL; - } + /* Try to find if the corresponding object already exists. */ + if( dynamicgraph::g_pool.existEntity( instanceName,obj ) ) + { + if( obj->getClassName()!=className ) + throw ExceptionPython( ExceptionPython::CLASS_INCONSISTENT, + "Found an object with the same name but of different class." ); + } + else /* If not, create a new object. */ + { + try { + obj = dynamicgraph::g_factory.newEntity(std::string(className), + std::string(instanceName)); + } catch (std::exception& exc) { + PyErr_SetString(error, exc.what()); + return NULL; + } + } // Return the pointer as a PyCObject return PyCObject_FromVoidPtr((void*)obj, NULL); -- GitLab