From fba891c0dff58b04eace926277cd9e49d96ca630 Mon Sep 17 00:00:00 2001 From: chapulina Date: Thu, 26 Jul 2018 13:17:33 -0700 Subject: [PATCH] Implement rclcpp::is_initialized() (#522) * Implement rclcpp::is_initialized() * linter --- rclcpp/CMakeLists.txt | 8 ++++++++ rclcpp/include/rclcpp/utilities.hpp | 6 ++++++ rclcpp/src/rclcpp/utilities.cpp | 6 ++++++ rclcpp/test/test_executor.cpp | 4 ++-- rclcpp/test/test_init.cpp | 29 +++++++++++++++++++++++++++++ rclcpp/test/test_utilities.cpp | 2 ++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 rclcpp/test/test_init.cpp diff --git a/rclcpp/CMakeLists.txt b/rclcpp/CMakeLists.txt index 10fe1da..5f549ba 100644 --- a/rclcpp/CMakeLists.txt +++ b/rclcpp/CMakeLists.txt @@ -386,6 +386,14 @@ if(BUILD_TESTING) target_link_libraries(test_utilities ${PROJECT_NAME}) endif() + ament_add_gtest(test_init test/test_init.cpp + APPEND_LIBRARY_DIRS "${append_library_dirs}") + if(TARGET test_init) + ament_target_dependencies(test_init + "rcl") + target_link_libraries(test_init ${PROJECT_NAME}) + endif() + ament_add_gtest(test_multi_threaded_executor test/executors/test_multi_threaded_executor.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_multi_threaded_executor) diff --git a/rclcpp/include/rclcpp/utilities.hpp b/rclcpp/include/rclcpp/utilities.hpp index 3bf8cc1..1118417 100644 --- a/rclcpp/include/rclcpp/utilities.hpp +++ b/rclcpp/include/rclcpp/utilities.hpp @@ -87,6 +87,12 @@ RCLCPP_PUBLIC bool ok(); +/// Returns true if init() has already been called. +/** \return True if init() has been called, false otherwise. */ +RCLCPP_PUBLIC +bool +is_initialized(); + /// Notify the signal handler and rmw that rclcpp is shutting down. RCLCPP_PUBLIC void diff --git a/rclcpp/src/rclcpp/utilities.cpp b/rclcpp/src/rclcpp/utilities.cpp index b85b857..af417fc 100644 --- a/rclcpp/src/rclcpp/utilities.cpp +++ b/rclcpp/src/rclcpp/utilities.cpp @@ -264,6 +264,12 @@ rclcpp::ok() return ::g_signal_status == 0; } +bool +rclcpp::is_initialized() +{ + return rcl_ok(); +} + static std::mutex on_shutdown_mutex_; static std::vector> on_shutdown_callbacks_; diff --git a/rclcpp/test/test_executor.cpp b/rclcpp/test/test_executor.cpp index 4b51cf1..40e378b 100644 --- a/rclcpp/test/test_executor.cpp +++ b/rclcpp/test/test_executor.cpp @@ -28,7 +28,7 @@ using namespace std::chrono_literals; -class TestExcutors : public ::testing::Test +class TestExecutors : public ::testing::Test { protected: static void SetUpTestCase() @@ -50,7 +50,7 @@ protected: }; // Make sure that executors detach from nodes when destructing -TEST_F(TestExcutors, detachOnDestruction) { +TEST_F(TestExecutors, detachOnDestruction) { { rclcpp::executors::SingleThreadedExecutor executor; executor.add_node(node); diff --git a/rclcpp/test/test_init.cpp b/rclcpp/test/test_init.cpp new file mode 100644 index 0000000..05cc943 --- /dev/null +++ b/rclcpp/test/test_init.cpp @@ -0,0 +1,29 @@ +// Copyright 2018 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 + +#include "rclcpp/utilities.hpp" + +TEST(TestInit, is_initialized) { + EXPECT_FALSE(rclcpp::is_initialized()); + + rclcpp::init(0, nullptr); + + EXPECT_TRUE(rclcpp::is_initialized()); + + rclcpp::shutdown(); + + EXPECT_TRUE(rclcpp::is_initialized()); +} diff --git a/rclcpp/test/test_utilities.cpp b/rclcpp/test/test_utilities.cpp index a0feb72..da63cef 100644 --- a/rclcpp/test/test_utilities.cpp +++ b/rclcpp/test/test_utilities.cpp @@ -48,4 +48,6 @@ TEST(TestUtilities, init_with_args) { ASSERT_EQ(1u, other_args.size()); ASSERT_EQ(std::string{"process_name"}, other_args[0]); + + EXPECT_TRUE(rclcpp::is_initialized()); }