Skip to content

Commit 2d32d03

Browse files
authored
Make lifecycle impl get_current_state() const. (#2031)
We should only need to update the current state when it changes, so we do that in the change_state method (which is not const). Then we can just return the current_state_ object in get_current_state, and then mark it as const. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
1 parent 28890bf commit 2d32d03

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
839839
*/
840840
RCLCPP_LIFECYCLE_PUBLIC
841841
const State &
842-
get_current_state();
842+
get_current_state() const;
843843

844844
/// Return a list with the available states.
845845
/**

rclcpp_lifecycle/src/lifecycle_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ LifecycleNode::register_on_error(
535535
}
536536

537537
const State &
538-
LifecycleNode::get_current_state()
538+
LifecycleNode::get_current_state() const
539539
{
540540
return impl_->get_current_state();
541541
}

rclcpp_lifecycle/src/lifecycle_node_interface_impl.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ LifecycleNode::LifecycleNodeInterfaceImpl::init(bool enable_communication_interf
182182
nullptr);
183183
}
184184
}
185+
186+
current_state_ = State(state_machine_.current_state);
185187
}
186188

187189
bool
@@ -327,9 +329,8 @@ LifecycleNode::LifecycleNodeInterfaceImpl::on_get_transition_graph(
327329
}
328330

329331
const State &
330-
LifecycleNode::LifecycleNodeInterfaceImpl::get_current_state()
332+
LifecycleNode::LifecycleNodeInterfaceImpl::get_current_state() const
331333
{
332-
current_state_ = State(state_machine_.current_state);
333334
return current_state_;
334335
}
335336

@@ -396,6 +397,9 @@ LifecycleNode::LifecycleNodeInterfaceImpl::change_state(
396397
return RCL_RET_ERROR;
397398
}
398399

400+
// Update the internal current_state_
401+
current_state_ = State(state_machine_.current_state);
402+
399403
auto get_label_for_return_code =
400404
[](node_interfaces::LifecycleNodeInterface::CallbackReturn cb_return_code) -> const char *{
401405
auto cb_id = static_cast<uint8_t>(cb_return_code);
@@ -421,6 +425,9 @@ LifecycleNode::LifecycleNodeInterfaceImpl::change_state(
421425
return RCL_RET_ERROR;
422426
}
423427

428+
// Update the internal current_state_
429+
current_state_ = State(state_machine_.current_state);
430+
424431
// error handling ?!
425432
// TODO(karsten1987): iterate over possible ret value
426433
if (cb_return_code == node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR) {
@@ -437,6 +444,10 @@ LifecycleNode::LifecycleNodeInterfaceImpl::change_state(
437444
return RCL_RET_ERROR;
438445
}
439446
}
447+
448+
// Update the internal current_state_
449+
current_state_ = State(state_machine_.current_state);
450+
440451
// This true holds in both cases where the actual callback
441452
// was successful or not, since at this point we have a valid transistion
442453
// to either a new primary state or error state

rclcpp_lifecycle/src/lifecycle_node_interface_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class LifecycleNode::LifecycleNodeInterfaceImpl final
6565
std::function<node_interfaces::LifecycleNodeInterface::CallbackReturn(const State &)> & cb);
6666

6767
const State &
68-
get_current_state();
68+
get_current_state() const;
6969

7070
std::vector<State>
7171
get_available_states() const;

0 commit comments

Comments
 (0)