Drake
find_resource.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
8 
9 namespace drake {
10 
21  public:
23 
26 
29  std::string get_absolute_path_or_throw() const;
30 
34 
37  std::string get_resource_path() const;
38 
44  std::string resource_path, std::string base_path);
45 
50  std::string resource_path, std::string error_message);
51 
54 
55  private:
56  FindResourceResult() = default;
57  void CheckInvariants();
58 
59  // The path as requested by the user.
60  std::string resource_path_;
61 
62  // The base path (directory where resource_path was found), if found.
63  optional<std::string> base_path_;
64 
65  // An error message, permitted to be present only when base_path is empty.
66  //
67  // All three of resource_path, base_path, and error_message can be empty
68  // (e.g., a default-constructed and/or moved-from object), which represents
69  // resource-not-found along with an unspecified non-empty default error
70  // message from get_error_message().
71  optional<std::string> error_message_;
72 };
73 
83 FindResourceResult FindResource(std::string resource_path);
84 
87 std::string FindResourceOrThrow(std::string resource_path);
88 
96 extern const char* const kDrakeResourceRootEnvironmentVariableName;
97 
98 } // namespace drake
const char *const kDrakeResourceRootEnvironmentVariableName
The name of the environment variable that provides the first place where FindResource attempts to loo...
Definition: find_resource.cc:163
static FindResourceResult make_empty()
Returns an empty error result (no requested resource).
Definition: find_resource.cc:83
Definition: automotive_demo.cc:88
std::string FindResourceOrThrow(std::string resource_path)
Convenient wrapper for querying FindResource(resource_path) followed by FindResourceResult::get_absol...
Definition: find_resource.cc:202
static FindResourceResult make_error(std::string resource_path, std::string error_message)
Returns an error result (the requested resource was NOT found).
Definition: find_resource.cc:72
Result FindResource(string resource_path)
Attempts to locate a Drake resource named by the given resource_path.
Definition: find_resource.cc:166
std::string get_resource_path() const
Returns the resource_path asked of FindResource.
Definition: find_resource.cc:57
std::string get_absolute_path_or_throw() const
Either returns the get_absolute_path() iff the resource was found, or else throws runtime_error...
Definition: find_resource.cc:28
stx::optional< T > optional
Definition: drake_optional.h:15
optional< std::string > get_absolute_path() const
Returns the absolute path to the resource, iff the resource was found.
Definition: find_resource.cc:18
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:59
Provides Drake&#39;s assertion implementation.
Provides drake::optional as an alias for the appropriate implementation of std::optional or std::expe...
optional< std::string > get_error_message() const
Returns the error message, iff the resource was not found.
Definition: find_resource.cc:40
Models the outcome of drake::FindResource.
Definition: find_resource.h:20
static FindResourceResult make_success(std::string resource_path, std::string base_path)
Returns a success result (the requested resource was found).
Definition: find_resource.cc:61
Provides careful macros to selectively enable or disable the special member functions for copy-constr...