Skip to content

Commit

Permalink
Add get_executable_name() function (#70)
Browse files Browse the repository at this point in the history
This function wraps rcutils_get_executable_name and takes care of the
allocator and throws an exception when an error occurs.

The rcutils_get_executable_name implementation doesn't set any error
information when it fails, and from reading through the implementation
and the documentation for the system functions it calls, an error should
be extremely unlikely. A generic failure message is the best I can do.

Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Jun 12, 2020
1 parent 417f8be commit 16b579a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ if(BUILD_TESTING)

ament_add_gtest(test_pointer_traits test/test_pointer_traits.cpp)

ament_add_gtest(test_process test/test_process.cpp)
ament_target_dependencies(test_process rcutils)

set(append_library_dirs "$<TARGET_FILE_DIR:${PROJECT_NAME}>")

ament_add_gtest(test_shared_library test/test_shared_library.cpp
Expand Down
49 changes: 49 additions & 0 deletions include/rcpputils/process.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 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.

#ifndef RCPPUTILS__PROCESS_HPP_
#define RCPPUTILS__PROCESS_HPP_

#include <rcutils/process.h>

#include <string>

namespace rcpputils
{

/// Retrieve the current executable name.
/**
* This function portably retrieves the current program name and returns
* a copy of it.
*
* This function is thread-safe.
*
* \return The program name.
* \throws std::runtime_error on error
*/
std::string get_executable_name()
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * executable_name = rcutils_get_executable_name(allocator);
if (nullptr == executable_name) {
throw std::runtime_error("Failed to get executable name");
}
std::string ret(executable_name);
allocator.deallocate(executable_name, allocator.state);
return ret;
}

} // namespace rcpputils

#endif // RCPPUTILS__PROCESS_HPP_
23 changes: 23 additions & 0 deletions test/test_process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 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 <gtest/gtest.h>

#include <string>

#include "rcpputils/process.hpp"

TEST(TestProcess, test_get_executable_name) {
EXPECT_EQ("test_process", rcpputils::get_executable_name());
}

0 comments on commit 16b579a

Please sign in to comment.