ContainerBase::exposePropertiesOfChild

This commit is contained in:
Robert Haschke 2018-02-22 12:41:25 +01:00
parent 8152614d33
commit e9d1742337
4 changed files with 54 additions and 0 deletions

View File

@ -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<std::string>& 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)

View File

@ -199,6 +199,11 @@ public:
const std::string& description = "") {
return declare(name, typeid(T), description, default_value, &Property::serialize<T>);
}
/// declare all given properties also in other PropertyMap
void exposeTo(PropertyMap& other, const std::set<std::string>& 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;

View File

@ -181,6 +181,37 @@ void ContainerBase::clear()
pimpl()->children_.clear();
}
void ContainerBase::exposePropertiesOfChild(int child, const std::initializer_list<std::string>& 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();

View File

@ -131,6 +131,18 @@ Property& PropertyMap::property(const std::string &name)
return it->second;
}
void PropertyMap::exposeTo(PropertyMap& other, const std::set<std::string> &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<std::string> &properties)
{
for (auto &pair : props_) {