Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_executable_name() function #70

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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_STREQ("test_process", rcpputils::get_executable_name().c_str());
cottsay marked this conversation as resolved.
Show resolved Hide resolved
}