streamline add/insert interfaces for Task/Container

`add` falls back to `insert` for both structures,
but `add` throws exceptions and does not provide a return value.

`insert` provides standard STL container access.
This commit is contained in:
v4hn 2020-03-20 16:37:28 +01:00
parent df8a783239
commit c046ec605d
4 changed files with 18 additions and 5 deletions

View File

@ -62,6 +62,8 @@ public:
/// traverse all children of this container recursively
bool traverseRecursively(const StageCallback& processor) const;
void add(Stage::pointer&& stage);
virtual bool insert(Stage::pointer&& stage, int before = -1);
virtual bool remove(int pos);
virtual bool remove(Stage* child);

View File

@ -92,8 +92,8 @@ public:
/// load robot model from given parameter
void loadRobotModel(const std::string& robot_description = "robot_description");
// TODO: use Stage::insert as well?
void add(Stage::pointer&& stage);
bool insert(Stage::pointer&& stage, int before = -1);
void clear() final;
/// enable introspection publishing for use with rviz

View File

@ -194,7 +194,18 @@ bool ContainerBase::traverseRecursively(const ContainerBase::StageCallback& proc
return pimpl()->traverseStages(processor, 1, UINT_MAX);
}
void ContainerBase::add(Stage::pointer&& stage) {
if (!insert(std::move(stage))) {
throw std::runtime_error(name() + ": Could not insert stage");
}
}
bool ContainerBase::insert(Stage::pointer&& stage, int before) {
if (!stage) {
ROS_ERROR_STREAM(name() << ": reveived invalid stage pointer");
return false;
}
StagePrivate* impl = stage->pimpl();
if (!impl->setParent(this))
return false;

View File

@ -194,11 +194,11 @@ void Task::loadRobotModel(const std::string& robot_description) {
}
void Task::add(Stage::pointer&& stage) {
if (!stage)
throw std::runtime_error("stage insertion failed: invalid stage pointer");
stages()->add(std::move(stage));
}
if (!stages()->insert(std::move(stage)))
throw std::runtime_error(std::string("insertion failed for stage: ") + stage->name());
bool Task::insert(Stage::pointer&& stage, int before) {
return stages()->insert(std::move(stage), before);
}
void Task::clear() {