-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add package resource related utility functions to cpp API (#27)
* add package resource related utility functions to cpp these already existed in Python, see: #24 * store package name in PackageNotFoundError
- Loading branch information
Showing
12 changed files
with
302 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ament_index_cpp/include/ament_index_cpp/get_package_prefix.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2017 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 AMENT_INDEX_CPP__GET_PACKAGE_PREFIX_HPP_ | ||
#define AMENT_INDEX_CPP__GET_PACKAGE_PREFIX_HPP_ | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "ament_index_cpp/visibility_control.h" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
/// Thrown when a package is not found. | ||
class PackageNotFoundError : public std::out_of_range | ||
{ | ||
public: | ||
AMENT_INDEX_CPP_PUBLIC | ||
explicit PackageNotFoundError(const std::string & package_name); | ||
|
||
AMENT_INDEX_CPP_PUBLIC | ||
virtual ~PackageNotFoundError(); | ||
|
||
const std::string package_name; | ||
}; | ||
|
||
/// Return the installation prefix of the given package if found. | ||
/** | ||
* \param package_name the name of the package to locate. | ||
* \return installation prefix path in which the package was found. | ||
* \throws PackageNotFoundError when the given package is not found. | ||
*/ | ||
AMENT_INDEX_CPP_PUBLIC | ||
std::string | ||
get_package_prefix(const std::string & package_name); | ||
|
||
} // namespace ament_index_cpp | ||
|
||
#endif // AMENT_INDEX_CPP__GET_PACKAGE_PREFIX_HPP_ |
37 changes: 37 additions & 0 deletions
37
ament_index_cpp/include/ament_index_cpp/get_package_share_directory.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2017 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 AMENT_INDEX_CPP__GET_PACKAGE_SHARE_DIRECTORY_HPP_ | ||
#define AMENT_INDEX_CPP__GET_PACKAGE_SHARE_DIRECTORY_HPP_ | ||
|
||
#include <string> | ||
|
||
#include "ament_index_cpp/visibility_control.h" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
/// Return the share directory of the given package if found. | ||
/** | ||
* \param package_name the name of the package to locate. | ||
* \return share path of the package. | ||
* \throws PackageNotFoundError when the given package is not found. | ||
*/ | ||
AMENT_INDEX_CPP_PUBLIC | ||
std::string | ||
get_package_share_directory(const std::string & package_name); | ||
|
||
} // namespace ament_index_cpp | ||
|
||
#endif // AMENT_INDEX_CPP__GET_PACKAGE_SHARE_DIRECTORY_HPP_ |
33 changes: 33 additions & 0 deletions
33
ament_index_cpp/include/ament_index_cpp/get_packages_with_prefixes.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2017 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 AMENT_INDEX_CPP__GET_PACKAGES_WITH_PREFIXES_HPP_ | ||
#define AMENT_INDEX_CPP__GET_PACKAGES_WITH_PREFIXES_HPP_ | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include "ament_index_cpp/visibility_control.h" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
/// Return a map of package names to their installation prefix. | ||
AMENT_INDEX_CPP_PUBLIC | ||
std::map<std::string, std::string> | ||
get_packages_with_prefixes(); | ||
|
||
} // namespace ament_index_cpp | ||
|
||
#endif // AMENT_INDEX_CPP__GET_PACKAGES_WITH_PREFIXES_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2017 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 "ament_index_cpp/get_package_prefix.hpp" | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "ament_index_cpp/get_resource.hpp" | ||
#include "ament_index_cpp/get_search_paths.hpp" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
static | ||
std::string | ||
format_package_not_found_error_message(const std::string & package_name) | ||
{ | ||
std::string message = "package '" + package_name + "' not found, searching: ["; | ||
auto search_paths = get_search_paths(); | ||
for (const auto & path : search_paths) { | ||
message += path + ", "; | ||
} | ||
if (search_paths.size() > 0) { | ||
message = message.substr(0, message.size() - 2); | ||
} | ||
return message + "]"; | ||
} | ||
|
||
PackageNotFoundError::PackageNotFoundError(const std::string & _package_name) | ||
: std::out_of_range(format_package_not_found_error_message(_package_name)), | ||
package_name(_package_name) | ||
{} | ||
|
||
PackageNotFoundError::~PackageNotFoundError() {} | ||
|
||
std::string | ||
get_package_prefix(const std::string & package_name) | ||
{ | ||
std::string content; | ||
std::string prefix_path; | ||
if (!get_resource("packages", package_name, content, &prefix_path)) { | ||
throw PackageNotFoundError(package_name); | ||
} | ||
return prefix_path; | ||
} | ||
|
||
} // namespace ament_index_cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2017 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 "ament_index_cpp/get_package_share_directory.hpp" | ||
|
||
#include <string> | ||
|
||
#include "ament_index_cpp/get_package_prefix.hpp" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
std::string | ||
get_package_share_directory(const std::string & package_name) | ||
{ | ||
return get_package_prefix(package_name) + "/share/" + package_name; | ||
} | ||
|
||
} // namespace ament_index_cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2017 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 "ament_index_cpp/get_packages_with_prefixes.hpp" | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include "ament_index_cpp/get_resources.hpp" | ||
|
||
namespace ament_index_cpp | ||
{ | ||
|
||
std::map<std::string, std::string> | ||
get_packages_with_prefixes() | ||
{ | ||
return get_resources("packages"); | ||
} | ||
|
||
} // namespace ament_index_cpp |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters