Skip to content

Commit

Permalink
Remove use of boost::filesystem in public APIs (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 authored Sep 20, 2019
1 parent e53b1fb commit 18bff8e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
OS_NAME: linux
COMPILER: gcc
BUILD_TYPE: Release
RUN_TESTS: OFF
RUN_TESTS: ON
run: '.ci/script.sh'
windows_2019:
name: Build on Windows 2019
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### [DART 6.10.0 (20XX-XX-XX)](https://github.com/dartsim/dart/milestone/58?closed=1)

* Common

* Removed use of boost::filesystem in public APIs: [#1417](https://github.com/dartsim/dart/pull/1417)

* Kinematics

* Added IkFast parameter accessors to IkFast class: [#1396](https://github.com/dartsim/dart/pull/1396)
Expand Down
27 changes: 23 additions & 4 deletions dart/common/SharedLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,30 @@ namespace common {
//==============================================================================
std::shared_ptr<SharedLibrary> SharedLibrary::create(
const boost::filesystem::path& path)
{
return create(path.string());
}

//==============================================================================
std::shared_ptr<SharedLibrary> SharedLibrary::create(const std::string& path)
{
return detail::SharedLibraryManager::getSingleton().load(path);
}

//==============================================================================
SharedLibrary::SharedLibrary(
ProtectedConstructionTag, const boost::filesystem::path& canonicalPath)
: mCanonicalPath(canonicalPath), mInstance(nullptr)
: SharedLibrary(ProtectedConstruction, canonicalPath.string())
{
mInstance
= static_cast<DYNLIB_HANDLE>(DYNLIB_LOAD(canonicalPath.string().c_str()));
// Do nothing
}

//==============================================================================
SharedLibrary::SharedLibrary(
ProtectedConstructionTag, const std::string& canonicalPath)
: mCanonicalPath(canonicalPath), mPath(canonicalPath), mInstance(nullptr)
{
mInstance = static_cast<DYNLIB_HANDLE>(DYNLIB_LOAD(canonicalPath.c_str()));

if (!mInstance)
{
Expand All @@ -86,7 +99,7 @@ SharedLibrary::~SharedLibrary()
if (DYNLIB_UNLOAD(mInstance))
{
dterr << "[SharedLibrary::~SharedLibrary] Failed to unload library '"
<< mCanonicalPath << "': " << getLastError() << "\n";
<< mPath << "': " << getLastError() << "\n";
}
}

Expand All @@ -96,6 +109,12 @@ const boost::filesystem::path& SharedLibrary::getCanonicalPath() const
return mCanonicalPath;
}

//==============================================================================
const std::string& SharedLibrary::path() const
{
return mPath;
}

//==============================================================================
bool SharedLibrary::isValid() const
{
Expand Down
44 changes: 44 additions & 0 deletions dart/common/SharedLibrary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

#include <memory>
#include <string>

#include <boost/filesystem.hpp>

#include "dart/common/Deprecated.hpp"
#include "dart/common/Platform.hpp"

#if DART_OS_LINUX
Expand Down Expand Up @@ -108,9 +111,25 @@ class SharedLibrary
/// "/path/../to/yourfile".
/// \return Pointer to the created SharedLibrary upon success in loading.
/// Otherwise, returns nullptr.
/// \deprecated Deprecated in 6.10. Please use create(const std::string&)
/// instead.
DART_DEPRECATED(6.10)
static std::shared_ptr<SharedLibrary> create(
const boost::filesystem::path& path);

/// Creates a SharedLibrary from a path to the shared library.
///
/// \note SharedLibrary should be always created from this create function.
/// \param[in] path The path to the shared library. The path can be a relative
/// path or an absolute path. If the path doens't exist this function returns
/// nullptr. If the path exist, the path will be stored as the canonical path
/// where a canonical path is an absolute path that has no elements which are
/// symbolic links, and no dot or dot dot elements such as
/// "/path/../to/yourfile".
/// \return Pointer to the created SharedLibrary upon success in loading.
/// Otherwise, returns nullptr.
static std::shared_ptr<SharedLibrary> create(const std::string& path);

/// Constructs from a path to the shared library.
///
/// This constructor is only called by detail::SharedLibraryManager.
Expand All @@ -122,15 +141,34 @@ class SharedLibrary
/// \param[in] path The canonical path to the shared library.
/// \return Pointer to the created SharedLibrary upon success in loading.
/// Otherwise, returns nullptr.
/// \deprecated Deprecated in 6.10. Please use
/// SharedLibrary(ProtectedConstructionTag, const std::string&) instead.
DART_DEPRECATED(6.10)
explicit SharedLibrary(
ProtectedConstructionTag, const boost::filesystem::path& path);

/// Constructs from a path to the shared library.
///
/// This constructor is only called by detail::SharedLibraryManager.
/// ProtectedConstructionTag is necessary to enforce creating SharedLibrary
/// using std::make_shared.
///
/// \note Please use create() to contruct SharedLibrary instead of this
/// constructor.
/// \param[in] path The canonical path to the shared library.
/// \return Pointer to the created SharedLibrary upon success in loading.
/// Otherwise, returns nullptr.
explicit SharedLibrary(ProtectedConstructionTag, const std::string& path);

/// Destructor.
virtual ~SharedLibrary();

/// Returns the path to the shared library file.
const boost::filesystem::path& getCanonicalPath() const;

/// Returns the path to the shared library file.
const std::string& path() const;

/// Returns true if the shared library loading was successful.
bool isValid() const;

Expand All @@ -148,6 +186,12 @@ class SharedLibrary
/// path that has no elements which are symbolic links, and no dot or dot dot
/// elements such as "/path/../to/yourfile".
boost::filesystem::path mCanonicalPath;
// TODO(JS): Remove in DART 7.

/// Canonical path to the shared library where a canonical path is an absolute
/// path that has no elements which are symbolic links, and no dot or dot dot
/// elements such as "/path/../to/yourfile".
std::string mPath;

/// Handle to the loaded library.
DYNLIB_HANDLE mInstance;
Expand Down
21 changes: 15 additions & 6 deletions dart/common/detail/SharedLibraryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "dart/common/detail/SharedLibraryManager.hpp"

#include <fstream>

#include "dart/common/Console.hpp"
#include "dart/common/SharedLibrary.hpp"

Expand All @@ -42,9 +44,16 @@ namespace detail {
//==============================================================================
std::shared_ptr<SharedLibrary> SharedLibraryManager::load(
const boost::filesystem::path& path)
{
return load(path.string());
}

//==============================================================================
std::shared_ptr<SharedLibrary> SharedLibraryManager::load(
const std::string& path)
{
// Check if the given path exits
const bool exists = boost::filesystem::exists(path);
const bool exists = std::ifstream(path).good();
if (!exists)
{
dtwarn << "[SharedLibraryManager::load] Failed to load the shared library '"
Expand All @@ -53,11 +62,11 @@ std::shared_ptr<SharedLibrary> SharedLibraryManager::load(
}

// Convert the given path to the canonical path
const auto canonicalPath = boost::filesystem::canonical(path);
const auto canonicalPath = boost::filesystem::canonical(path).string();

const auto iter = mLibraries.find(canonicalPath);
const auto iter = mSharedLibraries.find(canonicalPath);

const auto found = iter != mLibraries.end();
const auto found = iter != mSharedLibraries.end();
if (found)
{
auto lib = iter->second.lock();
Expand All @@ -68,7 +77,7 @@ std::shared_ptr<SharedLibrary> SharedLibraryManager::load(
if (lib)
return lib;
else
mLibraries.erase(iter);
mSharedLibraries.erase(iter);
}

const auto newLib = std::make_shared<SharedLibrary>(
Expand All @@ -77,7 +86,7 @@ std::shared_ptr<SharedLibrary> SharedLibraryManager::load(
if (!newLib->isValid())
return nullptr;

mLibraries[canonicalPath] = newLib;
mSharedLibraries[canonicalPath] = newLib;
assert(canonicalPath == newLib->getCanonicalPath());

return newLib;
Expand Down
25 changes: 23 additions & 2 deletions dart/common/detail/SharedLibraryManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
#define DART_COMMON_DETAIL_SHAREDLIBRARYMANAGER_HPP_

#include <memory>
#include <string>
#include <unordered_map>
#include <boost/filesystem.hpp>
#include "dart/common/Singleton.hpp"

#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>

#include "dart/common/Deprecated.hpp"
#include "dart/common/Singleton.hpp"

namespace std {

template <>
Expand Down Expand Up @@ -71,15 +74,33 @@ class SharedLibraryManager final : public Singleton<SharedLibraryManager>
/// Windows).
/// \return Pointer to the shared library upon success. Otherwise, returns
/// nullptr.
/// \deprecated Deprecated in 6.10. Please use load(const std::string&)
/// instead.
DART_DEPRECATED(6.10)
std::shared_ptr<SharedLibrary> load(const boost::filesystem::path& path);

/// Loads the shared library with the specified path.
///
/// \param[in] path The path to the shared library. If the path doesn't
/// include the extension, this function will use the best guess depending on
/// the OS (e.g., '.so' for Linux, '.dylib' for macOS, and '.dll' for
/// Windows).
/// \return Pointer to the shared library upon success. Otherwise, returns
/// nullptr.
std::shared_ptr<SharedLibrary> load(const std::string& path);

protected:
friend class Singleton<SharedLibraryManager>;

protected:
/// Map from library path to the library instances.
std::unordered_map<boost::filesystem::path, std::weak_ptr<SharedLibrary>>
mLibraries;
// TODO(JS): Remove this in DART 7.

/// Map from library path to the library instances.
std::unordered_map<std::string, std::weak_ptr<SharedLibrary>>
mSharedLibraries;
};

} // namespace detail
Expand Down

0 comments on commit 18bff8e

Please sign in to comment.