Skip to content

Commit

Permalink
Use std::optional instead of drake::optional in common and examples (R…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesnape authored Oct 30, 2019
1 parent 2cf9355 commit c8f0970
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 90 deletions.
4 changes: 2 additions & 2 deletions common/drake_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace drake {

optional<std::string> MaybeGetDrakePath() {
std::optional<std::string> MaybeGetDrakePath() {
// Find something that represents where Drake resources live. This will be
// "/path/to/drake/.drake-find_resource-sentinel" where "/path/to/" names the
// root of Drake's git source tree (or perhaps an installed version of same).
const auto& find_result = FindResource("drake/.drake-find_resource-sentinel");
if (find_result.get_error_message()) {
return nullopt;
return std::nullopt;
}
filesystem::path sentinel_path(find_result.get_absolute_path_or_throw());

Expand Down
4 changes: 2 additions & 2 deletions common/drake_path.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <optional>
#include <string>

#include "drake/common/drake_deprecated.h"
#include "drake/common/drake_optional.h"

namespace drake {

Expand All @@ -15,6 +15,6 @@ namespace drake {
/// Most users should prefer FindResource() or FindResourceOrThrow() to locate
/// Drake resources for a specific resource filename. This method only exists
/// for legacy compatibility reasons, and might eventually be removed.
optional<std::string> MaybeGetDrakePath();
std::optional<std::string> MaybeGetDrakePath();

} // namespace drake
12 changes: 6 additions & 6 deletions common/find_loaded_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ unsigned char * ReadProcessMemory(mach_vm_address_t addr,
// Gets the list of all the dynamic libraries that have been loaded. Finds
// `library_name` in the list, and returns its absolute directory path.
// This function is specific to MacOS
optional<string> LoadedLibraryPath(const string& library_name) {
std::optional<string> LoadedLibraryPath(const string& library_name) {
task_dyld_info dyld_info;
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
// Getinformation from current process.
Expand All @@ -54,7 +54,7 @@ optional<string> LoadedLibraryPath(const string& library_name) {
unsigned char* data =
ReadProcessMemory(dyld_info.all_image_info_addr, &size);
if (!data) {
return nullopt;
return std::nullopt;
}
dyld_all_image_infos* infos =
reinterpret_cast<dyld_all_image_infos *>(data);
Expand All @@ -65,7 +65,7 @@ optional<string> LoadedLibraryPath(const string& library_name) {
unsigned char* info_addr = ReadProcessMemory(
reinterpret_cast<mach_vm_address_t>(infos->infoArray), &size2);
if (!info_addr) {
return nullopt;
return std::nullopt;
}
dyld_image_info* info =
reinterpret_cast<dyld_image_info*>(info_addr);
Expand All @@ -80,14 +80,14 @@ optional<string> LoadedLibraryPath(const string& library_name) {
}
}
}
return nullopt;
return std::nullopt;
}
#else // Not __APPLE__

// Gets the list of all the shared objects that have been loaded. Finds
// `library_name` in the list, and returns its absolute directory path.
// This function is specific to Linux.
optional<string> LoadedLibraryPath(const std::string& library_name) {
std::optional<string> LoadedLibraryPath(const std::string& library_name) {
void* handle = dlopen(NULL, RTLD_NOW);
link_map *map;
dlinfo(handle, RTLD_DI_LINKMAP, &map);
Expand All @@ -112,7 +112,7 @@ optional<string> LoadedLibraryPath(const std::string& library_name) {
}
map = map->l_next;
}
return nullopt;
return std::nullopt;
}
#endif
} // namespace drake
5 changes: 2 additions & 3 deletions common/find_loaded_library.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include <optional>
#include <string>

#include "drake/common/drake_optional.h"

namespace drake {

/// This function returns the absolute path of the library with the name
/// `library_name` if that library was loaded in the current running
/// process. Otherwise it returns an empty optional.
optional<std::string> LoadedLibraryPath(const std::string& library_name);
std::optional<std::string> LoadedLibraryPath(const std::string& library_name);


} // namespace drake
45 changes: 23 additions & 22 deletions common/find_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace drake {

using Result = FindResourceResult;

optional<string>
std::optional<string>
Result::get_absolute_path() const {
return absolute_path_;
}
Expand All @@ -34,21 +34,21 @@ Result::get_absolute_path_or_throw() const {

// Otherwise, throw the error message.
const auto& optional_error = get_error_message();
DRAKE_ASSERT(optional_error != nullopt);
DRAKE_ASSERT(optional_error != std::nullopt);
throw std::runtime_error(*optional_error);
}

optional<string>
std::optional<string>
Result::get_error_message() const {
// If an error has been set, return it.
if (error_message_ != nullopt) {
DRAKE_ASSERT(absolute_path_ == nullopt);
if (error_message_ != std::nullopt) {
DRAKE_ASSERT(absolute_path_ == std::nullopt);
return error_message_;
}

// If successful, return no-error.
if (absolute_path_ != nullopt) {
return nullopt;
if (absolute_path_ != std::nullopt) {
return std::nullopt;
}

// Both optionals are null; we are empty; return a default error message.
Expand Down Expand Up @@ -91,15 +91,16 @@ Result Result::make_empty() {
void Result::CheckInvariants() {
if (resource_path_.empty()) {
// For our "empty" state, both success and error must be empty.
DRAKE_DEMAND(absolute_path_ == nullopt);
DRAKE_DEMAND(error_message_ == nullopt);
DRAKE_DEMAND(absolute_path_ == std::nullopt);
DRAKE_DEMAND(error_message_ == std::nullopt);
} else {
// For our "non-empty" state, we must have exactly one of success or error.
DRAKE_DEMAND((absolute_path_ == nullopt) != (error_message_ == nullopt));
DRAKE_DEMAND(
(absolute_path_ == std::nullopt) != (error_message_ == std::nullopt));
}
// When non-nullopt, the path and error cannot be the empty string.
DRAKE_DEMAND((absolute_path_ == nullopt) || !absolute_path_->empty());
DRAKE_DEMAND((error_message_ == nullopt) || !error_message_->empty());
DRAKE_DEMAND((absolute_path_ == std::nullopt) || !absolute_path_->empty());
DRAKE_DEMAND((error_message_ == std::nullopt) || !error_message_->empty());
}

namespace {
Expand Down Expand Up @@ -153,7 +154,7 @@ Result CheckAndMakeResult(
// Opportunistically searches inside the attic for multibody resource paths.
// This function is not unit tested -- only acceptance-tested by the fact that
// none of the tests in the attic fail.
optional<string> MaybeFindResourceInAttic(const string& resource_path) {
std::optional<string> MaybeFindResourceInAttic(const string& resource_path) {
const string prefix("drake/");
DRAKE_DEMAND(StartsWith(resource_path, prefix));
const string substr = resource_path.substr(prefix.size());
Expand All @@ -175,49 +176,49 @@ optional<string> MaybeFindResourceInAttic(const string& resource_path) {
}
}
}
return nullopt;
return std::nullopt;
}

// If the DRAKE_RESOURCE_ROOT environment variable is usable as a resource
// root, returns its value, else returns nullopt.
optional<string> MaybeGetEnvironmentResourceRoot() {
std::optional<string> MaybeGetEnvironmentResourceRoot() {
const char* const env_name = kDrakeResourceRootEnvironmentVariableName;
const char* const env_value = getenv(env_name);
if (!env_value) {
log()->debug(
"FindResource ignoring {} because it is not set.",
env_name);
return nullopt;
return std::nullopt;
}
const std::string root{env_value};
if (!filesystem::is_directory({root})) {
static const logging::Warn log_once(
"FindResource ignoring {}='{}' because it does not exist.",
env_name, env_value);
return nullopt;
return std::nullopt;
}
if (!filesystem::is_directory({root + "/drake"})) {
static const logging::Warn log_once(
"FindResource ignoring {}='{}' because it does not contain a 'drake' "
"subdirectory.", env_name, env_value);
return nullopt;
return std::nullopt;
}
if (!filesystem::is_regular_file({root + "/" + kSentinelRelpath})) {
static const logging::Warn log_once(
"FindResource ignoring {}='{}' because it does not contain the "
"expected sentinel file '{}'.", env_name, env_value, kSentinelRelpath);
return nullopt;
return std::nullopt;
}
return root;
}

// If we are linked against libdrake_marker.so, and the install-tree-relative
// path resolves correctly, returns the install tree resource root, else
// returns nullopt.
optional<string> MaybeGetInstallResourceRoot() {
std::optional<string> MaybeGetInstallResourceRoot() {
// Ensure that we have the library loaded.
DRAKE_DEMAND(drake::internal::drake_marker_lib_check() == 1234);
optional<string> libdrake_dir = LoadedLibraryPath("libdrake_marker.so");
std::optional<string> libdrake_dir = LoadedLibraryPath("libdrake_marker.so");
if (libdrake_dir) {
const string root = *libdrake_dir + "/../share";
if (filesystem::is_directory({root})) {
Expand All @@ -229,7 +230,7 @@ optional<string> MaybeGetInstallResourceRoot() {
} else {
log()->debug("FindResource has no CMake install candidate");
}
return nullopt;
return std::nullopt;
}

} // namespace
Expand Down
10 changes: 5 additions & 5 deletions common/find_resource.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include <optional>
#include <string>
#include <vector>

#include "drake/common/drake_assert.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/drake_optional.h"

namespace drake {

Expand All @@ -23,15 +23,15 @@ class FindResourceResult {
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(FindResourceResult);

/// Returns the absolute path to the resource, iff the resource was found.
optional<std::string> get_absolute_path() const;
std::optional<std::string> get_absolute_path() const;

/// Either returns the get_absolute_path() iff the resource was found,
/// or else throws std::runtime_error.
std::string get_absolute_path_or_throw() const;

/// Returns the error message, iff the resource was not found.
/// The string will never be empty; only the optional can be empty.
optional<std::string> get_error_message() const;
std::optional<std::string> get_error_message() const;

/// Returns the resource_path asked of FindResource.
/// (This may be empty only in the make_empty() case.)
Expand Down Expand Up @@ -61,15 +61,15 @@ class FindResourceResult {
std::string resource_path_;

// The absolute path where resource_path was found, if success.
optional<std::string> absolute_path_;
std::optional<std::string> absolute_path_;

// An error message, permitted to be present only when base_path is empty.
//
// All three of resource_path, base_path, and error_message can be empty
// (e.g., a default-constructed and/or moved-from object), which represents
// resource-not-found along with an unspecified non-empty default error
// message from get_error_message().
optional<std::string> error_message_;
std::optional<std::string> error_message_;
};

/// Attempts to locate a Drake resource named by the given @p resource_path.
Expand Down
14 changes: 3 additions & 11 deletions common/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <functional>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "drake/common/drake_assert.h"
#include "drake/common/drake_optional.h"
#include "drake/common/drake_throw.h"

/// @defgroup hash_append hash_append generic hashing
Expand Down Expand Up @@ -127,23 +127,15 @@ void hash_append(
hash_append(hasher, item.second);
}

/// Provides @ref hash_append for drake::optional.
/// Provides @ref hash_append for std::optional.
///
/// Note that `std::hash<std::optional<T>>` provides the peculiar invariant
/// that the hash of an `optional` bearing a value `v` shall evaluate to the
/// same hash as that of the value `v` itself. Hash operations implemented
/// with this `hash_append` do *not* provide that invariant.
//
// NB: In general, implementations of `hash_append` for drake types belong
// with the definitions of their respective drake types, not here.
//
// However, since `drake::optional` is more or less an alias for an
// STL type, and in particular since `drake_optional.h` is included
// in the ":essential" library, and `hash.h` is not, this is the
// better location for this definition.
template <class HashAlgorithm, class T>
void hash_append(
HashAlgorithm& hasher, const drake::optional<T>& item) noexcept {
HashAlgorithm& hasher, const std::optional<T>& item) noexcept {
if (item) {
hash_append(hasher, *item);
}
Expand Down
11 changes: 6 additions & 5 deletions common/symbolic_simplification.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "drake/common/symbolic_simplification.h"

#include <optional>
#include <stdexcept>
#include <utility>

#include "drake/common/drake_assert.h"
#include "drake/common/drake_optional.h"

namespace drake {
namespace symbolic {
Expand Down Expand Up @@ -45,12 +45,13 @@ class UnificationVisitor {
//
// In this case, there is no way to match `e` with `p` because `a + b` is not
// matched with `x * y`. Therefore, `Unify(p, e)` returns `nullopt`.
optional<Substitution> Unify(const Pattern& p, const Expression& e) const {
std::optional<Substitution> Unify(const Pattern& p,
const Expression& e) const {
Substitution subst;
if (Unify(p, e, &subst)) {
return subst;
} else {
return nullopt;
return std::nullopt;
}
}

Expand Down Expand Up @@ -767,14 +768,14 @@ class UnificationVisitor {
};

// Unifies the expression `e` with the pattern `p`.
optional<Substitution> Unify(const Pattern& p, const Expression& e) {
std::optional<Substitution> Unify(const Pattern& p, const Expression& e) {
return UnificationVisitor{}.Unify(p, e);
}
} // namespace

Rewriter MakeRuleRewriter(const RewritingRule& rule) {
return [rule](const Expression& e) {
const optional<Substitution> subst{Unify(rule.lhs(), e)};
const std::optional<Substitution> subst{Unify(rule.lhs(), e)};
if (subst) {
return rule.rhs().Substitute(*subst);
} else {
Expand Down
2 changes: 1 addition & 1 deletion common/test/find_loaded_library_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GTEST_TEST(FindLibraryTest, Library) {
lib_is_real_dummy_function();
// Test whether or not `LoadedLibraryPath()` can find the path to a library
// loaded by the process.
optional<string> library_path = LoadedLibraryPath("lib_is_real.so");
std::optional<string> library_path = LoadedLibraryPath("lib_is_real.so");
ASSERT_TRUE(library_path);
EXPECT_EQ(library_path.value()[0], '/');
library_path = LoadedLibraryPath("lib_not_real.so");
Expand Down
Loading

0 comments on commit c8f0970

Please sign in to comment.