-
Notifications
You must be signed in to change notification settings - Fork 427
/
state.cpp
185 lines (152 loc) · 4.8 KB
/
state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "rclcpp_lifecycle/state.hpp"
#include <string>
#include "lifecycle_msgs/msg/state.hpp"
#include "rcl_lifecycle/rcl_lifecycle.h"
#include "rclcpp/exceptions.hpp"
#include "rclcpp/logging.hpp"
#include "rcutils/allocator.h"
#include "mutex_map.hpp"
namespace rclcpp_lifecycle
{
MutexMap State::state_handle_mutex_map_;
State::State(rcutils_allocator_t allocator)
: State(lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN, "unknown", allocator)
{
state_handle_mutex_map_.add(this);
}
State::State(
uint8_t id,
const std::string & label,
rcutils_allocator_t allocator)
: allocator_(allocator),
owns_rcl_state_handle_(true),
state_handle_(nullptr)
{
state_handle_mutex_map_.add(this);
if (label.empty()) {
throw std::runtime_error("Lifecycle State cannot have an empty label.");
}
state_handle_ = static_cast<rcl_lifecycle_state_t *>(
allocator_.allocate(sizeof(rcl_lifecycle_state_t), allocator_.state));
if (!state_handle_) {
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
}
// zero initialize
state_handle_->id = 0;
state_handle_->label = nullptr;
auto ret = rcl_lifecycle_state_init(state_handle_, id, label.c_str(), &allocator_);
if (ret != RCL_RET_OK) {
reset();
rclcpp::exceptions::throw_from_rcl_error(ret);
}
}
State::State(
const rcl_lifecycle_state_t * rcl_lifecycle_state_handle,
rcutils_allocator_t allocator)
: allocator_(allocator),
owns_rcl_state_handle_(false),
state_handle_(nullptr)
{
state_handle_mutex_map_.add(this);
if (!rcl_lifecycle_state_handle) {
throw std::runtime_error("rcl_lifecycle_state_handle is null");
}
state_handle_ = const_cast<rcl_lifecycle_state_t *>(rcl_lifecycle_state_handle);
}
State::State(const State & rhs)
: allocator_(rhs.allocator_),
owns_rcl_state_handle_(false),
state_handle_(nullptr)
{
state_handle_mutex_map_.add(this);
*this = rhs;
}
State::~State()
{
reset();
state_handle_mutex_map_.remove(this);
}
State &
State::operator=(const State & rhs)
{
if (this == &rhs) {
return *this;
}
const auto lock = std::lock_guard<std::recursive_mutex>(state_handle_mutex_map_.getMutex(this));
// reset all currently used resources
reset();
allocator_ = rhs.allocator_;
owns_rcl_state_handle_ = rhs.owns_rcl_state_handle_;
// we don't own the handle, so we can return straight ahead
if (!owns_rcl_state_handle_) {
state_handle_ = rhs.state_handle_;
return *this;
}
// we own the handle, so we have to deep-copy the rhs object
state_handle_ = static_cast<rcl_lifecycle_state_t *>(
allocator_.allocate(sizeof(rcl_lifecycle_state_t), allocator_.state));
if (!state_handle_) {
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
}
// zero initialize
state_handle_->id = 0;
state_handle_->label = nullptr;
auto ret = rcl_lifecycle_state_init(
state_handle_, rhs.id(), rhs.label().c_str(), &allocator_);
if (ret != RCL_RET_OK) {
reset();
throw std::runtime_error("failed to duplicate label for rcl_lifecycle_state_t");
}
return *this;
}
uint8_t
State::id() const
{
const auto lock = std::lock_guard<std::recursive_mutex>(state_handle_mutex_map_.getMutex(this));
if (!state_handle_) {
throw std::runtime_error("Error in state! Internal state_handle is NULL.");
}
return static_cast<uint8_t>(state_handle_->id);
}
std::string
State::label() const
{
const auto lock = std::lock_guard<std::recursive_mutex>(state_handle_mutex_map_.getMutex(this));
if (!state_handle_) {
throw std::runtime_error("Error in state! Internal state_handle is NULL.");
}
return state_handle_->label;
}
void
State::reset() noexcept
{
const auto lock = std::lock_guard<std::recursive_mutex>(state_handle_mutex_map_.getMutex(this));
if (!owns_rcl_state_handle_) {
state_handle_ = nullptr;
}
if (!state_handle_) {
return;
}
auto ret = rcl_lifecycle_state_fini(state_handle_, &allocator_);
allocator_.deallocate(state_handle_, allocator_.state);
state_handle_ = nullptr;
if (ret != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp_lifecycle"),
"rcl_lifecycle_transition_fini did not complete successfully, leaking memory");
}
}
} // namespace rclcpp_lifecycle