Skip to content

Commit

Permalink
[v7] Move ResourceLoader to v7
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Mar 23, 2024
1 parent a208d79 commit 319dfbf
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 113 deletions.
31 changes: 5 additions & 26 deletions dart/common/LocalResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,13 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef DART_COMMON_LOCALRESOURCERETRIEVER_HPP_
#define DART_COMMON_LOCALRESOURCERETRIEVER_HPP_
#pragma once

#include <dart/common/ResourceRetriever.hpp>
#include <dart/v7/local_resource_loader.hpp>

namespace dart {
namespace common {

/// LocalResourceRetriever provides access to local resources specified by
/// file:// URIs by wrapping the standard C and C++ file manipulation routines.
class LocalResourceRetriever : public virtual ResourceRetriever
{
public:
virtual ~LocalResourceRetriever() = default;

// Documentation inherited.
bool exists(const Uri& _uri) override;

// Documentation inherited.
ResourcePtr retrieve(const Uri& _uri) override;

// Documentation inherited.
std::string getFilePath(const Uri& uri) override;
};
namespace dart::common {

using LocalResourceRetriever = v7::LocalResourceLoader;
using LocalResourceRetrieverPtr = std::shared_ptr<LocalResourceRetriever>;

} // namespace common
} // namespace dart

#endif // ifndef DART_COMMON_LOCALRESOURCERETRIEVER_HPP_
} // namespace dart::common
50 changes: 5 additions & 45 deletions dart/common/ResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,13 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef DART_COMMON_RESOURCERETRIEVER_HPP_
#define DART_COMMON_RESOURCERETRIEVER_HPP_
#pragma once

#include <dart/common/Resource.hpp>
#include <dart/common/Uri.hpp>
#include <dart/v7/resource_loader.hpp>

#include <memory>
#include <string>

namespace dart {
namespace common {

/// ResourceRetriever provides methods for testing for the existance of and
/// accessing the content of a resource specified by URI.
class ResourceRetriever
{
public:
virtual ~ResourceRetriever() = default;

/// Returns whether the resource specified by a URI exists.
virtual bool exists(const Uri& uri) = 0;

/// Returns the resource specified by a URI or nullptr on failure.
virtual ResourcePtr retrieve(const Uri& uri) = 0;

/// Reads all data from the resource of uri, and returns it as a string.
///
/// \param[in] uri URI to the resource to be retrieved.
/// \return The string retrieved from the resource.
/// \throw std::runtime_error when failed to read sucessfully.
virtual std::string readAll(const Uri& uri);

/// Returns absolute file path to \c uri; an empty string if unavailable.
///
/// This base class returns an empty string by default.
virtual std::string getFilePath(const Uri& uri);

// We don't const-qualify for exists, retrieve, readAll, and getFilePath here.
// Derived classes of ResourceRetriever will be interacting with external
// resources that you don't necessarily have control over so we cannot
// guarantee that you get the same result every time with the same input Uri.
// Indeed, const-qualification for those functions is pointless.
};
namespace dart::common {

using ResourceRetriever = v7::ResourceLoader;
using ResourceRetrieverPtr = std::shared_ptr<ResourceRetriever>;

} // namespace common
} // namespace dart

#endif // ifndef DART_COMMON_RESOURCERETRIEVER_HPP_
} // namespace dart::common
1 change: 1 addition & 0 deletions dart/dynamics/MeshShape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <dart/dynamics/Shape.hpp>

#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <assimp/scene.h>

Expand Down
2 changes: 2 additions & 0 deletions dart/utils/CompositeResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#define DART_UTILS_COMPOSITERESOURCERETRIEVER_HPP_

#include <dart/common/ClassWithVirtualBase.hpp>
#include <dart/common/Resource.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <unordered_map>
#include <vector>
Expand Down
3 changes: 2 additions & 1 deletion dart/utils/DartResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
#ifndef DART_UTILS_DARTRESOURCERETRIEVER_HPP_
#define DART_UTILS_DARTRESOURCERETRIEVER_HPP_

#include <dart/common/Resource.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <unordered_map>
#include <vector>

namespace dart {
Expand Down
2 changes: 2 additions & 0 deletions dart/utils/PackageResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#define DART_UTILS_PACKAGERESOURCERETRIEVER_HPP_

#include <dart/common/ClassWithVirtualBase.hpp>
#include <dart/common/Resource.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <unordered_map>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions dart/utils/SkelParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "dart/collision/dart/DARTCollisionDetector.hpp"
#include "dart/collision/fcl/FCLCollisionDetector.hpp"
#include "dart/common/Console.hpp"
#include "dart/common/LocalResourceRetriever.hpp"
#include "dart/config.hpp"
#include "dart/constraint/ConstraintSolver.hpp"
#include "dart/dynamics/BallJoint.hpp"
Expand Down
2 changes: 1 addition & 1 deletion dart/utils/SkelParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include <dart/simulation/World.hpp>

#include <dart/common/LocalResourceRetriever.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <string>
Expand Down
1 change: 1 addition & 0 deletions dart/utils/XmlHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <dart/common/Deprecated.hpp>
#include <dart/common/Logging.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <Eigen/Dense>
#include <Eigen/Geometry>
Expand Down
2 changes: 1 addition & 1 deletion dart/utils/mjcf/MjcfParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include <dart/simulation/World.hpp>

#include <dart/common/LocalResourceRetriever.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

#include <string>
Expand Down
1 change: 1 addition & 0 deletions dart/utils/sdf/SdfParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <dart/common/Deprecated.hpp>
#include <dart/common/ResourceRetriever.hpp>
#include <dart/common/Uri.hpp>

namespace dart {
namespace utils {
Expand Down
17 changes: 17 additions & 0 deletions dart/v7/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,25 @@

#include <dart/config.hpp>

#include <memory>

// Define a typedef for const and non-const version of shared_ptr and weak_ptr
// for the class X
#define DART_DECL_CLASS_PTRS(X) \
class X; \
using X##Ptr = std::shared_ptr<X>; \
using Const##X##Ptr = std::shared_ptr<const X>; \
using X##WeakPtr = std::weak_ptr<X>; \
using Const##X##WeakPtr = std::weak_ptr<const X>; \
using X##UniquePtr = std::unique_ptr<X>; \
using Const##X##UniquePtr = std::unique_ptr<const X>;

namespace dart::v7 {

DART_DECL_CLASS_PTRS(Resource)
DART_DECL_CLASS_PTRS(LocalResource)
DART_DECL_CLASS_PTRS(ResourceLoader)

struct StepInfo;

class Context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2024, The DART development contributors
* Copyright (c) The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
Expand Down Expand Up @@ -30,58 +30,57 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "dart/common/LocalResourceRetriever.hpp"
#include "dart/v7/local_resource_loader.hpp"

#include "dart/common/Console.hpp"
#include "dart/common/LocalResource.hpp"
#include "dart/common/Uri.hpp"
#include "dart/v7/local_resource.hpp"

#include <fstream>
#include <iostream>

namespace dart {
namespace common {
namespace dart::v7 {

//==============================================================================
bool LocalResourceRetriever::exists(const Uri& _uri)
bool LocalResourceLoader::exists(const Uri& uri)
{
return !getFilePath(_uri).empty();
return !getFilePath(uri).empty();
}

//==============================================================================
common::ResourcePtr LocalResourceRetriever::retrieve(const Uri& _uri)
ResourcePtr LocalResourceLoader::retrieve(const Uri& uri)
{
if (_uri.mScheme.get_value_or("file") != "file")
if (uri.mScheme.get_value_or("file") != "file") {
return nullptr;
else if (!_uri.mPath)
} else if (!uri.mPath) {
return nullptr;
}

const auto resource
= std::make_shared<LocalResource>(_uri.getFilesystemPath());
= std::make_shared<LocalResource>(uri.getFilesystemPath());

if (resource->isGood())
if (resource->isGood()) {
return resource;
else
} else {
return nullptr;
}
}

//==============================================================================
std::string LocalResourceRetriever::getFilePath(const Uri& uri)
std::string LocalResourceLoader::getFilePath(const Uri& uri)
{
// Open and close the file to check if it exists. It would be more efficient
// to stat() it, but that is not portable.
if (uri.mScheme.get_value_or("file") != "file")
if (uri.mScheme.get_value_or("file") != "file") {
return "";
else if (!uri.mPath)
} else if (!uri.mPath) {
return "";
}

const auto path = uri.getFilesystemPath();

if (std::ifstream(path, std::ios::binary).good())
if (std::ifstream(path, std::ios::binary).good()) {
return path;
else
} else {
return "";
}
}

} // namespace common
} // namespace dart
} // namespace dart::v7
56 changes: 56 additions & 0 deletions dart/v7/local_resource_loader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
* https://github.com/dartsim/dart/blob/main/LICENSE
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <dart/v7/resource_loader.hpp>

namespace dart::v7 {

/// LocalResourceLoaderprovides access to local resources specified by
/// file:// URIs by wrapping the standard C and C++ file manipulation routines.
class LocalResourceLoader : public ResourceLoader
{
public:
~LocalResourceLoader() override = default;

// Documentation inherited.
bool exists(const Uri& uri) override;

// Documentation inherited.
ResourcePtr retrieve(const Uri& uri) override;

// Documentation inherited.
std::string getFilePath(const Uri& uri) override;
};

} // namespace dart::v7
2 changes: 0 additions & 2 deletions dart/v7/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,4 @@ class Resource
virtual std::string readAll();
};

using ResourcePtr = std::shared_ptr<Resource>;

} // namespace dart::v7
20 changes: 6 additions & 14 deletions dart/common/ResourceRetriever.cpp → dart/v7/resource_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2024, The DART development contributors
* Copyright (c) The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
Expand Down Expand Up @@ -30,17 +30,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "dart/common/ResourceRetriever.hpp"
#include "dart/v7/resource_loader.hpp"

#include "dart/common/Console.hpp"
namespace dart::v7 {

#include <sstream>

namespace dart {
namespace common {

//==============================================================================
std::string ResourceRetriever::readAll(const Uri& uri)
std::string ResourceLoader::readAll(const Uri& uri)
{
auto resource = retrieve(uri);

Expand All @@ -53,11 +47,9 @@ std::string ResourceRetriever::readAll(const Uri& uri)
return resource->readAll();
}

//==============================================================================
std::string ResourceRetriever::getFilePath(const Uri& /*uri*/)
std::string ResourceLoader::getFilePath(const Uri& /*uri*/)
{
return "";
}

} // namespace common
} // namespace dart
} // namespace dart::v7
Loading

0 comments on commit 319dfbf

Please sign in to comment.