-
Notifications
You must be signed in to change notification settings - Fork 27
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 package resource related utility functions to cpp API #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should the member be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the package name is baked into the error message, and if you change this then it would be inconsistent with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't think that the class should prescribe the member to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I don't agree. I've given a reason why it would be undesirable to change the package name after creation, but I haven't seen a reason why you would want to change it. I'm willing to change it if a convincing reason was given and that outweighs the downside I've already stated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The package for which the exception was thrown does not seem like it should be modifiable after the fact or in the traceback/error handling thus keeping it const would make sense. And relaxing the const constraint in the future will always be easier than adding it if there is a use case that needs it than the reverse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, I looked at making a setter and getter for the http://en.cppreference.com/w/cpp/error/exception/what So, the creating of the new |
||
}; | ||
|
||
/// 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_ |
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_ |
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_ |
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 |
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the package name be stored as a member in the custom exception class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 sure: 0d43992