diff --git a/core/include/moveit/task_constructor/container.h b/core/include/moveit/task_constructor/container.h index f0e41bbd..e728e981 100644 --- a/core/include/moveit/task_constructor/container.h +++ b/core/include/moveit/task_constructor/container.h @@ -61,6 +61,12 @@ public: virtual bool remove(int pos); virtual void clear(); + /// declare all given variables of child and configure child to inherit them from parent + void exposePropertiesOfChild(int child, const std::initializer_list& names); + /// declare given child property with another name and configure child to inherit it from parent + void exposePropertyOfChildAs(int child, const std::string& child_property_name, + const std::string& parent_property_name); + void reset() override; void init(const moveit::core::RobotModelConstPtr& robot_model) override; /// validate connectivity of children (after init() was done) diff --git a/core/include/moveit/task_constructor/properties.h b/core/include/moveit/task_constructor/properties.h index 47b04333..42a44e90 100644 --- a/core/include/moveit/task_constructor/properties.h +++ b/core/include/moveit/task_constructor/properties.h @@ -199,6 +199,11 @@ public: const std::string& description = "") { return declare(name, typeid(T), description, default_value, &Property::serialize); } + /// declare all given properties also in other PropertyMap + void exposeTo(PropertyMap& other, const std::set& properties); + + /// declare given property name as other_name in other PropertyMap + void exposeTo(PropertyMap& other, const std::string& name, const std::string& other_name); bool hasProperty(const std::string &name) const; diff --git a/core/src/container.cpp b/core/src/container.cpp index 50caa019..566104be 100644 --- a/core/src/container.cpp +++ b/core/src/container.cpp @@ -181,6 +181,37 @@ void ContainerBase::clear() pimpl()->children_.clear(); } +void ContainerBase::exposePropertiesOfChild(int child, const std::initializer_list& names) +{ + auto impl = pimpl(); + // for negative child index, return last child for -1, next to last for -2, etc + ContainerBasePrivate::const_iterator child_it = impl->position(child < 0 ? child-1 : child); + if (child_it == impl->children().end()) + throw std::runtime_error("invalid child index"); + + auto &child_props = (*child_it)->properties(); + // declare variables + child_props.exposeTo(impl->properties_, names); + // configure inheritance + child_props.configureInitFrom(Stage::PARENT, names); +} + +void ContainerBase::exposePropertyOfChildAs(int child, const std::string& child_property_name, + const std::string& parent_property_name) +{ + auto impl = pimpl(); + // for negative child index, return last child for -1, next to last for -2, etc + ContainerBasePrivate::const_iterator child_it = impl->position(child < 0 ? child-1 : child); + if (child_it == impl->children().end()) + throw std::runtime_error("invalid child index"); + + auto &child_props = (*child_it)->properties(); + // declare variables + child_props.exposeTo(impl->properties_, child_property_name, parent_property_name); + // configure inheritance + child_props.property(child_property_name).configureInitFrom(Stage::PARENT, parent_property_name); +} + void ContainerBase::reset() { auto impl = pimpl(); diff --git a/core/src/properties.cpp b/core/src/properties.cpp index b83d22d4..eb4fd43a 100644 --- a/core/src/properties.cpp +++ b/core/src/properties.cpp @@ -131,6 +131,18 @@ Property& PropertyMap::property(const std::string &name) return it->second; } +void PropertyMap::exposeTo(PropertyMap& other, const std::set &properties) +{ + for (const std::string& name : properties) + exposeTo(other, name, name); +} + +void PropertyMap::exposeTo(PropertyMap& other, const std::string& name, const std::string& other_name) +{ + const Property& p = property(name); + other.declare(name, p.type_index_, p.description_, p.default_, p.serialize_); +} + void PropertyMap::configureInitFrom(Property::SourceId source, const std::set &properties) { for (auto &pair : props_) {