Skip to content

Commit 86e0df3

Browse files
Thomas Groecheltgroechel
authored andcommitted
Sq: Lifecycle Separation of Concerns
Signed-off-by: Tom Groechel <[email protected]>
1 parent 86c7714 commit 86e0df3

13 files changed

+1132
-500
lines changed

rclcpp_lifecycle/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ find_package(rosidl_typesupport_cpp REQUIRED)
2323
### CPP High level library
2424
add_library(rclcpp_lifecycle
2525
src/lifecycle_node.cpp
26+
src/lifecycle_node_entities_manager.cpp
2627
src/lifecycle_node_interface_impl.cpp
28+
src/lifecycle_node_state_manager.cpp
29+
src/lifecycle_node_state_services_manager.cpp
2730
src/managed_entity.cpp
2831
src/node_interfaces/lifecycle_node_interface.cpp
2932
src/state.cpp

rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10041004
*/
10051005
RCLCPP_LIFECYCLE_PUBLIC
10061006
bool
1007-
register_on_configure(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1007+
register_on_configure(
1008+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10081009

10091010
/// Register the cleanup callback
10101011
/**
@@ -1014,7 +1015,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10141015
*/
10151016
RCLCPP_LIFECYCLE_PUBLIC
10161017
bool
1017-
register_on_cleanup(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1018+
register_on_cleanup(
1019+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10181020

10191021
/// Register the shutdown callback
10201022
/**
@@ -1024,7 +1026,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10241026
*/
10251027
RCLCPP_LIFECYCLE_PUBLIC
10261028
bool
1027-
register_on_shutdown(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1029+
register_on_shutdown(
1030+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10281031

10291032
/// Register the activate callback
10301033
/**
@@ -1034,7 +1037,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10341037
*/
10351038
RCLCPP_LIFECYCLE_PUBLIC
10361039
bool
1037-
register_on_activate(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1040+
register_on_activate(
1041+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10381042

10391043
/// Register the deactivate callback
10401044
/**
@@ -1044,7 +1048,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10441048
*/
10451049
RCLCPP_LIFECYCLE_PUBLIC
10461050
bool
1047-
register_on_deactivate(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1051+
register_on_deactivate(
1052+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10481053

10491054
/// Register the error callback
10501055
/**
@@ -1054,7 +1059,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
10541059
*/
10551060
RCLCPP_LIFECYCLE_PUBLIC
10561061
bool
1057-
register_on_error(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
1062+
register_on_error(
1063+
std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);
10581064

10591065
RCLCPP_LIFECYCLE_PUBLIC
10601066
CallbackReturn

rclcpp_lifecycle/src/lifecycle_node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ LifecycleNode::register_on_error(
541541
lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING, fcn);
542542
}
543543

544+
544545
const State &
545546
LifecycleNode::get_current_state() const
546547
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include "lifecycle_node_entities_manager.hpp"
15+
16+
namespace rclcpp_lifecycle
17+
{
18+
19+
void
20+
LifecycleNodeEntitiesManager::on_activate() const
21+
{
22+
for (const auto & weak_entity : weak_managed_entities_) {
23+
auto entity = weak_entity.lock();
24+
if (entity) {
25+
entity->on_activate();
26+
}
27+
}
28+
}
29+
30+
void
31+
LifecycleNodeEntitiesManager::on_deactivate() const
32+
{
33+
for (const auto & weak_entity : weak_managed_entities_) {
34+
auto entity = weak_entity.lock();
35+
if (entity) {
36+
entity->on_deactivate();
37+
}
38+
}
39+
}
40+
41+
void
42+
LifecycleNodeEntitiesManager::add_managed_entity(
43+
std::weak_ptr<rclcpp_lifecycle::ManagedEntityInterface> managed_entity)
44+
{
45+
weak_managed_entities_.push_back(managed_entity);
46+
}
47+
48+
void
49+
LifecycleNodeEntitiesManager::add_timer_handle(
50+
std::shared_ptr<rclcpp::TimerBase> timer)
51+
{
52+
weak_timers_.push_back(timer);
53+
}
54+
55+
} // namespace rclcpp_lifecycle
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef LIFECYCLE_NODE_ENTITIES_MANAGER_HPP_
16+
#define LIFECYCLE_NODE_ENTITIES_MANAGER_HPP_
17+
18+
#include <vector>
19+
#include <memory>
20+
#include "rclcpp_lifecycle/managed_entity.hpp"
21+
#include <rclcpp/timer.hpp>
22+
23+
namespace rclcpp_lifecycle
24+
{
25+
26+
class LifecycleNodeEntitiesManager
27+
{
28+
public:
29+
void
30+
on_activate() const;
31+
32+
void
33+
on_deactivate() const;
34+
35+
void
36+
add_managed_entity(std::weak_ptr<rclcpp_lifecycle::ManagedEntityInterface> managed_entity);
37+
38+
void
39+
add_timer_handle(std::shared_ptr<rclcpp::TimerBase> timer);
40+
41+
private:
42+
std::vector<std::weak_ptr<rclcpp_lifecycle::ManagedEntityInterface>> weak_managed_entities_;
43+
std::vector<std::weak_ptr<rclcpp::TimerBase>> weak_timers_;
44+
};
45+
46+
} // namespace rclcpp_lifecycle
47+
#endif // LIFECYCLE_NODE_ENTITIES_MANAGER_HPP_

0 commit comments

Comments
 (0)