* copy and assignment operator for state

copy and assignment operator for transition

remove unused const_casts

address comments

check for null in copy constructor

up

use init and fini functions from rcl

remove unused include

* explicitly zero initialize state and transitions

* add todo comment for follow up
This commit is contained in:
Karsten Knese 2017-12-05 22:51:52 -08:00 committed by GitHub
parent c40f3c25c6
commit 6d13bcb0fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 286 additions and 6 deletions

View file

@ -44,9 +44,15 @@ public:
const rcl_lifecycle_state_t * rcl_lifecycle_state_handle, const rcl_lifecycle_state_t * rcl_lifecycle_state_handle,
rcutils_allocator_t allocator = rcutils_get_default_allocator()); rcutils_allocator_t allocator = rcutils_get_default_allocator());
RCLCPP_LIFECYCLE_PUBLIC
State(const State & rhs);
RCLCPP_LIFECYCLE_PUBLIC RCLCPP_LIFECYCLE_PUBLIC
virtual ~State(); virtual ~State();
RCLCPP_LIFECYCLE_PUBLIC
State & operator=(const State & rhs);
RCLCPP_LIFECYCLE_PUBLIC RCLCPP_LIFECYCLE_PUBLIC
uint8_t uint8_t
id() const; id() const;

View file

@ -51,9 +51,15 @@ public:
const rcl_lifecycle_transition_t * rcl_lifecycle_transition_handle, const rcl_lifecycle_transition_t * rcl_lifecycle_transition_handle,
rcutils_allocator_t allocator = rcutils_get_default_allocator()); rcutils_allocator_t allocator = rcutils_get_default_allocator());
RCLCPP_LIFECYCLE_PUBLIC
Transition(const Transition & rhs);
RCLCPP_LIFECYCLE_PUBLIC RCLCPP_LIFECYCLE_PUBLIC
virtual ~Transition(); virtual ~Transition();
RCLCPP_LIFECYCLE_PUBLIC
Transition & operator=(const Transition & rhs);
RCLCPP_LIFECYCLE_PUBLIC RCLCPP_LIFECYCLE_PUBLIC
uint8_t uint8_t
id() const; id() const;

View file

@ -23,7 +23,6 @@
#include "rclcpp/exceptions.hpp" #include "rclcpp/exceptions.hpp"
#include "rcutils/allocator.h" #include "rcutils/allocator.h"
#include "rcutils/strdup.h"
namespace rclcpp_lifecycle namespace rclcpp_lifecycle
{ {
@ -49,6 +48,9 @@ State::State(
if (!state_handle_) { if (!state_handle_) {
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t"); 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_); auto ret = rcl_lifecycle_state_init(state_handle_, id, label.c_str(), &allocator_);
if (ret != RCL_RET_OK) { if (ret != RCL_RET_OK) {
@ -70,11 +72,58 @@ State::State(
state_handle_ = const_cast<rcl_lifecycle_state_t *>(rcl_lifecycle_state_handle); 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)
{
*this = rhs;
}
State::~State() State::~State()
{ {
reset(); reset();
} }
State &
State::operator=(const State & rhs)
{
if (this == &rhs) {
return *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 uint8_t
State::id() const State::id() const
{ {
@ -104,6 +153,10 @@ State::reset()
return; return;
} }
// TODO(karsten1987): Fini currently deallocate the state_handle_ instance as well
// this should be changed to only deallocate members of the pointer so that stack
// variables can be correctly used as well.
// see https://github.com/ros2/rclcpp/pull/419#discussion_r155157098
auto ret = rcl_lifecycle_state_fini(state_handle_, &allocator_); auto ret = rcl_lifecycle_state_fini(state_handle_, &allocator_);
if (ret != RCL_RET_OK) { if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(ret); rclcpp::exceptions::throw_from_rcl_error(ret);

View file

@ -23,7 +23,6 @@
#include "rclcpp/exceptions.hpp" #include "rclcpp/exceptions.hpp"
#include "rcutils/allocator.h" #include "rcutils/allocator.h"
#include "rcutils/strdup.h"
namespace rclcpp_lifecycle namespace rclcpp_lifecycle
{ {
@ -41,6 +40,11 @@ Transition::Transition(
if (!transition_handle_) { if (!transition_handle_) {
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_transition_t"); throw std::runtime_error("failed to allocate memory for rcl_lifecycle_transition_t");
} }
// zero initialize
transition_handle_->id = 0;
transition_handle_->label = nullptr;
transition_handle_->start = nullptr;
transition_handle_->goal = nullptr;
auto ret = rcl_lifecycle_transition_init( auto ret = rcl_lifecycle_transition_init(
transition_handle_, id, label.c_str(), nullptr, nullptr, &allocator_); transition_handle_, id, label.c_str(), nullptr, nullptr, &allocator_);
@ -62,6 +66,10 @@ Transition::Transition(
reset(); reset();
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t"); throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
} }
// zero initialize
transition_handle_->start->id = 0;
transition_handle_->start->label = nullptr;
auto ret = rcl_lifecycle_state_init( auto ret = rcl_lifecycle_state_init(
transition_handle_->start, start.id(), start.label().c_str(), &allocator_); transition_handle_->start, start.id(), start.label().c_str(), &allocator_);
if (ret != RCL_RET_OK) { if (ret != RCL_RET_OK) {
@ -75,6 +83,10 @@ Transition::Transition(
reset(); reset();
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t"); throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
} }
// zero initialize
transition_handle_->goal->id = 0;
transition_handle_->goal->label = nullptr;
ret = rcl_lifecycle_state_init( ret = rcl_lifecycle_state_init(
transition_handle_->goal, goal.id(), goal.label().c_str(), &allocator_); transition_handle_->goal, goal.id(), goal.label().c_str(), &allocator_);
if (ret != RCL_RET_OK) { if (ret != RCL_RET_OK) {
@ -96,16 +108,110 @@ Transition::Transition(
transition_handle_ = const_cast<rcl_lifecycle_transition_t *>(rcl_lifecycle_transition_handle); transition_handle_ = const_cast<rcl_lifecycle_transition_t *>(rcl_lifecycle_transition_handle);
} }
Transition::Transition(const Transition & rhs)
: allocator_(rhs.allocator_),
owns_rcl_transition_handle_(false),
transition_handle_(nullptr)
{
*this = rhs;
}
Transition::~Transition() Transition::~Transition()
{ {
reset(); reset();
} }
Transition &
Transition::operator=(const Transition & rhs)
{
if (this == &rhs) {
return *this;
}
// reset all currently used resources
reset();
allocator_ = rhs.allocator_;
owns_rcl_transition_handle_ = rhs.owns_rcl_transition_handle_;
// we don't own the handle, so we can return straight ahead
if (!owns_rcl_transition_handle_) {
transition_handle_ = rhs.transition_handle_;
return *this;
}
// we own the handle, so we have to deep-copy the rhs object
transition_handle_ = static_cast<rcl_lifecycle_transition_t *>(
allocator_.allocate(sizeof(rcl_lifecycle_transition_t), allocator_.state));
if (!transition_handle_) {
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_transition_t");
}
// zero initialize
transition_handle_->id = 0;
transition_handle_->label = nullptr;
transition_handle_->start = nullptr;
transition_handle_->goal = nullptr;
auto ret = rcl_lifecycle_transition_init(
transition_handle_, rhs.id(), rhs.label().c_str(), nullptr, nullptr, &allocator_);
if (ret != RCL_RET_OK) {
reset();
rclcpp::exceptions::throw_from_rcl_error(ret);
}
// only copy start state when available
if (rhs.transition_handle_->start) {
transition_handle_->start = static_cast<rcl_lifecycle_state_t *>(
allocator_.allocate(sizeof(rcl_lifecycle_state_t), allocator_.state));
if (!transition_handle_->start) {
reset();
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
}
// zero initialize
transition_handle_->start->id = 0;
transition_handle_->start->label = nullptr;
ret = rcl_lifecycle_state_init(
transition_handle_->start,
rhs.start_state().id(),
rhs.start_state().label().c_str(),
&allocator_);
if (ret != RCL_RET_OK) {
reset();
rclcpp::exceptions::throw_from_rcl_error(ret);
}
}
// only copy goal state when available
if (rhs.transition_handle_->goal) {
transition_handle_->goal = static_cast<rcl_lifecycle_state_t *>(
allocator_.allocate(sizeof(rcl_lifecycle_state_t), allocator_.state));
if (!transition_handle_->goal) {
reset();
throw std::runtime_error("failed to allocate memory for rcl_lifecycle_state_t");
}
// zero initialize
transition_handle_->goal->id = 0;
transition_handle_->goal->label = nullptr;
ret = rcl_lifecycle_state_init(
transition_handle_->goal,
rhs.goal_state().id(),
rhs.goal_state().label().c_str(),
&allocator_);
if (ret != RCL_RET_OK) {
reset();
rclcpp::exceptions::throw_from_rcl_error(ret);
}
}
return *this;
}
uint8_t uint8_t
Transition::id() const Transition::id() const
{ {
if (!transition_handle_) { if (!transition_handle_) {
throw std::runtime_error("Error in state! Internal transition_handle_ is NULL."); throw std::runtime_error("internal transition_handle is null");
} }
return transition_handle_->id; return transition_handle_->id;
} }
@ -114,7 +220,7 @@ std::string
Transition::label() const Transition::label() const
{ {
if (!transition_handle_) { if (!transition_handle_) {
throw std::runtime_error("Error in state! Internal transition_handle_ is NULL."); throw std::runtime_error("internal transition_handle is null");
} }
return transition_handle_->label; return transition_handle_->label;
} }
@ -123,7 +229,7 @@ State
Transition::start_state() const Transition::start_state() const
{ {
if (!transition_handle_) { if (!transition_handle_) {
throw std::runtime_error("Error in state! Internal transition_handle_ is NULL."); throw std::runtime_error("internal transition_handle is null");
} }
// State constructor throws if start pointer is null // State constructor throws if start pointer is null
return State(transition_handle_->start, allocator_); return State(transition_handle_->start, allocator_);
@ -133,7 +239,7 @@ State
Transition::goal_state() const Transition::goal_state() const
{ {
if (!transition_handle_) { if (!transition_handle_) {
throw std::runtime_error("Error in state! Internal transition_handle_ is NULL."); throw std::runtime_error("internal transition_handle is null");
} }
// State constructor throws if goal pointer is null // State constructor throws if goal pointer is null
return State(transition_handle_->goal, allocator_); return State(transition_handle_->goal, allocator_);
@ -151,6 +257,10 @@ Transition::reset()
return; return;
} }
// TODO(karsten1987): Fini currently deallocate the transition_handle_ instance as well
// this should be changed to only deallocate members of the pointer so that stack
// variables can be correctly used as well.
// see https://github.com/ros2/rclcpp/pull/419#discussion_r155157098
auto ret = rcl_lifecycle_transition_fini(transition_handle_, &allocator_); auto ret = rcl_lifecycle_transition_fini(transition_handle_, &allocator_);
if (ret != RCL_RET_OK) { if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(ret); rclcpp::exceptions::throw_from_rcl_error(ret);

View file

@ -82,3 +82,87 @@ TEST_F(TestStateWrapper, wrapper) {
// EXPECT_STREQ("my_c_state", c_state.label().c_str()); // EXPECT_STREQ("my_c_state", c_state.label().c_str());
// } // }
} }
TEST_F(TestStateWrapper, copy_constructor) {
auto a = std::make_shared<rclcpp_lifecycle::State>(1, "my_c_state");
rclcpp_lifecycle::State b(*a);
a.reset();
EXPECT_EQ(1, b.id());
EXPECT_STREQ("my_c_state", b.label().c_str());
}
TEST_F(TestStateWrapper, assignment_operator) {
auto a = std::make_shared<rclcpp_lifecycle::State>(1, "one");
auto b = std::make_shared<rclcpp_lifecycle::State>(2, "two");
*b = *a;
a.reset();
EXPECT_EQ(1, b->id());
EXPECT_STREQ("one", b->label().c_str());
}
TEST_F(TestStateWrapper, assignment_operator2) {
// Non-owning State
rcl_lifecycle_state_t * lc_state1 =
new rcl_lifecycle_state_t{"my_c_state1", 1, NULL, NULL, 0};
auto non_owning_state1 = std::make_shared<rclcpp_lifecycle::State>(lc_state1);
// Non-owning State
rcl_lifecycle_state_t * lc_state2 =
new rcl_lifecycle_state_t{"my_c_state2", 2, NULL, NULL, 0};
auto non_owning_state2 = std::make_shared<rclcpp_lifecycle::State>(lc_state2);
*non_owning_state2 = *non_owning_state1;
EXPECT_EQ(1, non_owning_state2->id());
EXPECT_STREQ("my_c_state1", non_owning_state2->label().c_str());
non_owning_state1.reset();
non_owning_state2.reset();
delete lc_state1;
delete lc_state2;
}
TEST_F(TestStateWrapper, assignment_operator3) {
// Non-owning State
rcl_lifecycle_state_t * lc_state1 =
new rcl_lifecycle_state_t{"my_c_state1", 1, NULL, NULL, 0};
auto non_owning_state1 = std::make_shared<rclcpp_lifecycle::State>(lc_state1);
// owning State
auto owning_state2 = std::make_shared<rclcpp_lifecycle::State>(2, "my_c_state2");
*owning_state2 = *non_owning_state1;
EXPECT_EQ(1, owning_state2->id());
EXPECT_STREQ("my_c_state1", owning_state2->label().c_str());
non_owning_state1.reset();
owning_state2.reset();
delete lc_state1;
}
TEST_F(TestStateWrapper, assignment_operator4) {
// Non-owning State
rcl_lifecycle_state_t * lc_state1 =
new rcl_lifecycle_state_t{"my_c_state1", 1, NULL, NULL, 0};
auto non_owning_state1 = std::make_shared<rclcpp_lifecycle::State>(lc_state1);
// owning State
auto owning_state2 = std::make_shared<rclcpp_lifecycle::State>(2, "my_c_state2");
*non_owning_state1 = *owning_state2;
EXPECT_EQ(2, non_owning_state1->id());
EXPECT_STREQ("my_c_state2", non_owning_state1->label().c_str());
non_owning_state1.reset();
owning_state2.reset();
delete lc_state1;
}

View file

@ -63,3 +63,24 @@ TEST_F(TestTransitionWrapper, wrapper) {
EXPECT_STREQ("from_start_to_goal", t.label().c_str()); EXPECT_STREQ("from_start_to_goal", t.label().c_str());
} }
} }
TEST_F(TestTransitionWrapper, copy_constructor) {
auto a = std::make_shared<rclcpp_lifecycle::Transition>(1, "my_transition");
rclcpp_lifecycle::Transition b(*a);
a.reset();
EXPECT_EQ(1, b.id());
EXPECT_STREQ("my_transition", b.label().c_str());
}
TEST_F(TestTransitionWrapper, assignment_operator) {
auto a = std::make_shared<rclcpp_lifecycle::Transition>(1, "one");
auto b = std::make_shared<rclcpp_lifecycle::Transition>(2, "two");
*b = *a;
a.reset();
EXPECT_EQ(1, b->id());
EXPECT_STREQ("one", b->label().c_str());
}