Skip to content

Commit

Permalink
Deprecated path class (#196)
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Hernández Cordero <[email protected]>
  • Loading branch information
ahcorde authored Jul 24, 2024
1 parent acd78ce commit 6743223
Show file tree
Hide file tree
Showing 6 changed files with 561 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ if(BUILD_TESTING)
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(test_filesystem_helper ${PROJECT_NAME})

ament_add_gtest(test_filesystem_helper_std test/test_filesystem_helper_std.cpp
ENV
EXPECTED_WORKING_DIRECTORY=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(test_filesystem_helper_std ${PROJECT_NAME})

ament_add_gtest(test_find_and_replace test/test_find_and_replace.cpp)
target_link_libraries(test_find_and_replace ${PROJECT_NAME})

Expand Down
28 changes: 26 additions & 2 deletions include/rcpputils/filesystem_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ static constexpr const char kPreferredSeparator = RCPPUTILS_IMPL_OS_DIRSEP;

#undef RCPPUTILS_IMPL_OS_DIRSEP


// TODO(ahcorde): Remove deprecated class on the next release.
#if !defined(_WIN32)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#else // !defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4996)
#endif
/**
* \brief Drop-in replacement for [std::filesystem::path](https://en.cppreference.com/w/cpp/filesystem/path).
*
* It must conform to the same standard described and cannot include methods that are not
* incorporated there.
*/
class path
class [[deprecated("use std::filesystem instead of rcpputils::path")]] path
{
public:
/**
Expand Down Expand Up @@ -232,6 +239,7 @@ class path
* \param[in] p The path to check
* \return True if the path exists, false otherwise.
*/
[[deprecated("Please use std::filesystem::is_regular_file(..) instead")]]
RCPPUTILS_PUBLIC bool is_regular_file(const path & p) noexcept;

/**
Expand All @@ -240,6 +248,7 @@ RCPPUTILS_PUBLIC bool is_regular_file(const path & p) noexcept;
* \param[in] p The path to check
* \return True if the path is an existing directory, false otherwise.
*/
[[deprecated("Please use std::filesystem::is_directory(..) instead")]]
RCPPUTILS_PUBLIC bool is_directory(const path & p) noexcept;

/**
Expand All @@ -250,6 +259,7 @@ RCPPUTILS_PUBLIC bool is_directory(const path & p) noexcept;
*
* \throws std::sytem_error
*/
[[deprecated("Please use std::filesystem::file_size(..) instead")]]
RCPPUTILS_PUBLIC uint64_t file_size(const path & p);

/**
Expand All @@ -258,6 +268,7 @@ RCPPUTILS_PUBLIC uint64_t file_size(const path & p);
* \param[in] path_to_check The path to check.
* \return True if the path exists, false otherwise.
*/
[[deprecated("Please use std::filesystem::exists(..) instead")]]
RCPPUTILS_PUBLIC bool exists(const path & path_to_check);


Expand Down Expand Up @@ -317,6 +328,7 @@ RCPPUTILS_PUBLIC std::filesystem::path create_temporary_directory(
*
* \throws std::system_error
*/
[[deprecated("Please use std::filesystem::current_path(..) instead")]]
RCPPUTILS_PUBLIC path current_path();

/**
Expand All @@ -326,6 +338,7 @@ RCPPUTILS_PUBLIC path current_path();
* \param[in] p The path at which to create the directory.
* \return Return true if the directory already exists or is created, false otherwise.
*/
[[deprecated("Please use std::filesystem::create_directories(..) instead")]]
RCPPUTILS_PUBLIC bool create_directories(const path & p);

/**
Expand All @@ -334,6 +347,7 @@ RCPPUTILS_PUBLIC bool create_directories(const path & p);
* \param[in] p The path of the object to remove.
* \return true if the file exists and it was successfully removed, false otherwise.
*/
[[deprecated("Please use std::filesystem::remove(..) instead")]]
RCPPUTILS_PUBLIC bool remove(const path & p);

/**
Expand All @@ -344,6 +358,7 @@ RCPPUTILS_PUBLIC bool remove(const path & p);
* \param[in] p The path of the directory to remove.
* \return true if the directory exists and it was successfully removed, false otherwise.
*/
[[deprecated("Please use std::filesystem::remove_all(..) instead")]]
RCPPUTILS_PUBLIC bool remove_all(const path & p);

/**
Expand All @@ -362,7 +377,9 @@ RCPPUTILS_PUBLIC path remove_extension(const path & file_path, int n_times = 1);
*
* \return True if both paths are equal as strings.
*/
[[deprecated("This operator will be remove with the deprecated path class")]]
RCPPUTILS_PUBLIC bool operator==(const path & a, const path & b);
[[deprecated("This operator will be remove with the deprecated path class")]]
RCPPUTILS_PUBLIC bool operator!=(const path & a, const path & b);

/**
Expand All @@ -372,8 +389,15 @@ RCPPUTILS_PUBLIC bool operator!=(const path & a, const path & b);
* \param[in] p The path to stringify
* \return The ostream, for chaining
*/
[[deprecated("This operator will be remove with the deprecated path class")]]
RCPPUTILS_PUBLIC std::ostream & operator<<(std::ostream & os, const path & p);

// remove warning suppression
#if !defined(_WIN32)
# pragma GCC diagnostic pop
#else // !defined(_WIN32)
# pragma warning(pop)
#endif
} // namespace fs
} // namespace rcpputils

Expand Down
14 changes: 14 additions & 0 deletions src/filesystem_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ namespace rcpputils
namespace fs
{

#if !defined(_WIN32)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#else // !defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4996)
#endif

/// \internal Returns true if the path is an absolute path with a drive letter on Windows.
static bool is_absolute_with_drive_letter(const std::string & path);

Expand Down Expand Up @@ -517,5 +525,11 @@ std::ostream & operator<<(std::ostream & os, const path & p)
return os;
}

// remove warning suppression
#if !defined(_WIN32)
# pragma GCC diagnostic pop
#else // !defined(_WIN32)
# pragma warning(pop)
#endif
} // namespace fs
} // namespace rcpputils
5 changes: 3 additions & 2 deletions src/find_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cassert>
#include <cstddef>

#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -71,8 +72,8 @@ std::string find_library_path(const std::string & library_name)

std::string path_for_library(const std::string & directory, const std::string & library_name)
{
auto path = rcpputils::fs::path(directory) / filename_for_library(library_name);
if (path.is_regular_file()) {
auto path = std::filesystem::path(directory) / filename_for_library(library_name);
if (std::filesystem::is_regular_file(path)) {
return path.string();
}
return "";
Expand Down
13 changes: 13 additions & 0 deletions test/test_filesystem_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ static constexpr const bool is_win32 = true;
#else
static constexpr const bool is_win32 = false;
#endif
#if !defined(_WIN32)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#else // !defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4996)
#endif

using path = rcpputils::fs::path;

Expand Down Expand Up @@ -531,3 +538,9 @@ TEST(TestFilesystemHelper, equal_operators)
path d = path("foo") / "bar";
EXPECT_EQ(c, d);
}

#if !defined(_WIN32)
# pragma GCC diagnostic pop
#else // !defined(_WIN32)
# pragma warning(pop)
#endif
Loading

0 comments on commit 6743223

Please sign in to comment.