resource_locator.h
Go to the documentation of this file.
1 
26 #ifndef TESSERACT_COMMON_RESOURCE_LOCATOR_H
27 #define TESSERACT_COMMON_RESOURCE_LOCATOR_H
28 
31 #include <functional>
32 #include <memory>
33 #include <string>
34 #include <unordered_map>
35 #include <vector>
36 #include <boost/serialization/export.hpp>
38 
39 #include <filesystem>
40 
41 namespace boost::serialization
42 {
43 class access;
44 }
45 
46 namespace tesseract_common
47 {
48 // Forward declare
49 class Resource;
50 struct Serialization;
51 
54 {
55 public:
56  using Ptr = std::shared_ptr<ResourceLocator>;
57  using ConstPtr = std::shared_ptr<const ResourceLocator>;
58 
59  virtual ~ResourceLocator() = default;
60 
67  virtual std::shared_ptr<Resource> locateResource(const std::string& url) const = 0;
68 
69  bool operator==(const ResourceLocator& rhs) const;
70  bool operator!=(const ResourceLocator& rhs) const;
71 
72 private:
75  template <class Archive>
76  void serialize(Archive& ar, const unsigned int version); // NOLINT
77 };
78 
85 {
86 public:
87  using Ptr = std::shared_ptr<GeneralResourceLocator>;
88  using ConstPtr = std::shared_ptr<const GeneralResourceLocator>;
94  GeneralResourceLocator(const std::vector<std::string>& environment_variables = { "TESSERACT_RESOURCE_PATH",
95  "ROS_PACKAGE_PATH" });
103  GeneralResourceLocator(const std::vector<std::filesystem::path>& paths,
104  const std::vector<std::string>& environment_variables = { "TESSERACT_RESOURCE_PATH",
105  "ROS_PACKAGE_PATH" });
110  ~GeneralResourceLocator() override = default;
111 
112  std::shared_ptr<Resource> locateResource(const std::string& url) const override;
113 
114  bool operator==(const GeneralResourceLocator& rhs) const;
115  bool operator!=(const GeneralResourceLocator& rhs) const;
116 
124  bool addPath(const std::filesystem::path& path);
125 
133  bool loadEnvironmentVariable(const std::string& environment_variable);
134 
135 private:
138  template <class Archive>
139  void serialize(Archive& ar, const unsigned int version); // NOLINT
140 
141  std::unordered_map<std::string, std::string> package_paths_;
142 
143  void processToken(const std::string& token);
144 };
145 
147 class Resource : public ResourceLocator
148 {
149 public:
150  using Ptr = std::shared_ptr<Resource>;
151  using ConstPtr = std::shared_ptr<const Resource>;
152 
158  virtual bool isFile() const = 0;
159 
165  virtual std::string getUrl() const = 0;
166 
172  virtual std::string getFilePath() const = 0;
173 
179  virtual std::vector<uint8_t> getResourceContents() const = 0;
180 
186  virtual std::shared_ptr<std::istream> getResourceContentStream() const = 0;
187 
188  bool operator==(const Resource& rhs) const;
189  bool operator!=(const Resource& rhs) const;
190 
191 private:
194  template <class Archive>
195  void serialize(Archive& ar, const unsigned int version); // NOLINT
196 };
197 
198 using SimpleResourceLocatorFn = std::function<std::string(const std::string&)>;
199 
202 {
203 public:
204  using Ptr = std::shared_ptr<SimpleLocatedResource>;
205  using ConstPtr = std::shared_ptr<const SimpleLocatedResource>;
206 
208  SimpleLocatedResource() = default;
209 
216  SimpleLocatedResource(std::string url, std::string filename, ResourceLocator::ConstPtr parent = nullptr);
217  ~SimpleLocatedResource() override = default;
222 
223  bool isFile() const override final;
224 
225  std::string getUrl() const override final;
226 
227  std::string getFilePath() const override final;
228 
229  std::vector<uint8_t> getResourceContents() const override final;
230 
231  std::shared_ptr<std::istream> getResourceContentStream() const override final;
232 
233  Resource::Ptr locateResource(const std::string& url) const override final;
234 
235  bool operator==(const SimpleLocatedResource& rhs) const;
236  bool operator!=(const SimpleLocatedResource& rhs) const;
237 
238 private:
239  std::string url_;
240  std::string filename_;
242 
243  friend class boost::serialization::access;
245  template <class Archive>
246  void serialize(Archive& ar, const unsigned int version); // NOLINT
247 };
248 
250 {
251 public:
253  BytesResource() = default;
254 
255  BytesResource(std::string url, std::vector<uint8_t> bytes, ResourceLocator::ConstPtr parent = nullptr);
256  BytesResource(std::string url, const uint8_t* bytes, size_t bytes_len, ResourceLocator::ConstPtr parent = nullptr);
257  ~BytesResource() override = default;
258  BytesResource(const BytesResource&) = default;
259  BytesResource& operator=(const BytesResource&) = default;
260  BytesResource(BytesResource&&) = default;
261  BytesResource& operator=(BytesResource&&) = default;
262 
263  bool isFile() const override final;
264  std::string getUrl() const override final;
265  std::string getFilePath() const override final;
266  std::vector<uint8_t> getResourceContents() const override final;
267  std::shared_ptr<std::istream> getResourceContentStream() const override final;
268  Resource::Ptr locateResource(const std::string& url) const override final;
269 
270  bool operator==(const BytesResource& rhs) const;
271  bool operator!=(const BytesResource& rhs) const;
272 
273 private:
274  std::string url_;
275  std::vector<uint8_t> bytes_;
277 
278  friend class boost::serialization::access;
280  template <class Archive>
281  void serialize(Archive& ar, const unsigned int version); // NOLINT
282 };
283 
284 } // namespace tesseract_common
285 BOOST_SERIALIZATION_ASSUME_ABSTRACT(tesseract_common::ResourceLocator)
286 BOOST_SERIALIZATION_ASSUME_ABSTRACT(tesseract_common::Resource)
287 
288 BOOST_CLASS_EXPORT_KEY(tesseract_common::GeneralResourceLocator)
289 BOOST_CLASS_EXPORT_KEY(tesseract_common::SimpleLocatedResource)
290 BOOST_CLASS_EXPORT_KEY(tesseract_common::BytesResource)
291 
292 #endif // TESSERACT_COMMON_RESOURCE_LOCATOR_H
tesseract_common::SimpleLocatedResource::operator==
bool operator==(const SimpleLocatedResource &rhs) const
Definition: resource_locator.cpp:301
tesseract_common::Resource::access
friend class boost::serialization::access
Definition: resource_locator.h:192
tesseract_common::Resource::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:223
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::Resource::operator!=
bool operator!=(const Resource &rhs) const
Definition: resource_locator.cpp:220
tesseract_common::SimpleLocatedResource::url_
std::string url_
Definition: resource_locator.h:239
tesseract_common::Resource::getUrl
virtual std::string getUrl() const =0
Get the original URL used to locate the file.
tesseract_common::SimpleLocatedResource::SimpleLocatedResource
SimpleLocatedResource()=default
This is for boost serialization do not use directly.
tesseract_common::GeneralResourceLocator::operator=
GeneralResourceLocator & operator=(const GeneralResourceLocator &)=default
macros.h
Common Tesseract Macros.
tesseract_common::ResourceLocator::locateResource
virtual std::shared_ptr< Resource > locateResource(const std::string &url) const =0
Locate a resource based on a URL.
boost
Definition: allowed_collision_matrix.h:14
tesseract_common::SimpleResourceLocatorFn
std::function< std::string(const std::string &)> SimpleResourceLocatorFn
Definition: resource_locator.h:198
tesseract_common::GeneralResourceLocator::loadEnvironmentVariable
bool loadEnvironmentVariable(const std::string &environment_variable)
Load paths from an environment variable.
Definition: resource_locator.cpp:82
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::BytesResource::url_
std::string url_
Definition: resource_locator.h:274
tesseract_common::SimpleLocatedResource::operator=
SimpleLocatedResource & operator=(const SimpleLocatedResource &)=default
tesseract_common::SimpleLocatedResource
Resource implementation for a local file.
Definition: resource_locator.h:201
tesseract_common::Serialization
Definition: serialization.h:97
tesseract_common::GeneralResourceLocator::processToken
void processToken(const std::string &token)
Definition: resource_locator.cpp:113
tesseract_common::Resource::getResourceContentStream
virtual std::shared_ptr< std::istream > getResourceContentStream() const =0
Get the resource as a std::istream. This function and the returned stream may block.
tesseract_common::SimpleLocatedResource::~SimpleLocatedResource
~SimpleLocatedResource() override=default
tesseract_common::Resource::isFile
virtual bool isFile() const =0
Returns true if the located resource is a local file.
tesseract_common::ResourceLocator::access
friend class boost::serialization::access
Definition: resource_locator.h:73
tesseract_common::GeneralResourceLocator::access
friend class boost::serialization::access
Definition: resource_locator.h:136
tesseract_common::GeneralResourceLocator::GeneralResourceLocator
GeneralResourceLocator(const std::vector< std::string > &environment_variables={ "TESSERACT_RESOURCE_PATH", "ROS_PACKAGE_PATH" })
Construct a new General Resource Locator object using the TESSERACT_RESOURCE_PATH environment variabl...
Definition: resource_locator.cpp:60
tesseract_common::GeneralResourceLocator::operator!=
bool operator!=(const GeneralResourceLocator &rhs) const
Definition: resource_locator.cpp:211
tesseract_common::ResourceLocator::ConstPtr
std::shared_ptr< const ResourceLocator > ConstPtr
Definition: resource_locator.h:57
tesseract_common::ResourceLocator::operator!=
bool operator!=(const ResourceLocator &rhs) const
Definition: resource_locator.cpp:53
tesseract_common::Resource::getResourceContents
virtual std::vector< uint8_t > getResourceContents() const =0
Get the resource as bytes. This function may block.
tesseract_common::ResourceLocator
Abstract class for resource loaders.
Definition: resource_locator.h:53
tesseract_common::SimpleLocatedResource::getResourceContents
std::vector< uint8_t > getResourceContents() const override final
Get the resource as bytes. This function may block.
Definition: resource_locator.cpp:239
tesseract_common::GeneralResourceLocator::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:214
tesseract_common::Resource
Represents resource data available from a file or url.
Definition: resource_locator.h:147
boost::serialization
Definition: allowed_collision_matrix.h:14
tesseract_common::ResourceLocator::operator==
bool operator==(const ResourceLocator &rhs) const
Definition: resource_locator.cpp:52
tesseract_common::GeneralResourceLocator::package_paths_
std::unordered_map< std::string, std::string > package_paths_
Definition: resource_locator.h:141
tesseract_common::SimpleLocatedResource::operator!=
bool operator!=(const SimpleLocatedResource &rhs) const
Definition: resource_locator.cpp:311
tesseract_common::SimpleLocatedResource::getFilePath
std::string getFilePath() const override final
Get the file path of the resource. Only valid if isFile() is true.
Definition: resource_locator.cpp:237
tesseract_common::SimpleLocatedResource::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:314
tesseract_common::BytesResource::bytes_
std::vector< uint8_t > bytes_
Definition: resource_locator.h:275
tesseract_common::Resource::getFilePath
virtual std::string getFilePath() const =0
Get the file path of the resource. Only valid if isFile() is true.
tesseract_common::SimpleLocatedResource::getResourceContentStream
std::shared_ptr< std::istream > getResourceContentStream() const override final
Get the resource as a std::istream. This function and the returned stream may block.
Definition: resource_locator.cpp:259
tesseract_common::SimpleLocatedResource::parent_
ResourceLocator::ConstPtr parent_
Definition: resource_locator.h:241
tesseract_common::GeneralResourceLocator::locateResource
std::shared_ptr< Resource > locateResource(const std::string &url) const override
Locate a resource based on a URL.
Definition: resource_locator.cpp:169
tesseract_common::GeneralResourceLocator::~GeneralResourceLocator
~GeneralResourceLocator() override=default
tesseract_common::Resource::operator==
bool operator==(const Resource &rhs) const
Definition: resource_locator.cpp:219
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::BytesResource
Definition: resource_locator.h:249
tesseract_common::GeneralResourceLocator::operator==
bool operator==(const GeneralResourceLocator &rhs) const
Definition: resource_locator.cpp:210
tesseract_common::Resource::Ptr
std::shared_ptr< Resource > Ptr
Definition: resource_locator.h:150
tesseract_common::ResourceLocator::~ResourceLocator
virtual ~ResourceLocator()=default
tesseract_common::GeneralResourceLocator
A general resource loaders using environment variable.
Definition: resource_locator.h:84
tesseract_common::BytesResource::parent_
ResourceLocator::ConstPtr parent_
Definition: resource_locator.h:276
tesseract_common::ResourceLocator::Ptr
std::shared_ptr< ResourceLocator > Ptr
Definition: resource_locator.h:56
tesseract_common::ResourceLocator::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:56
tesseract_common::SimpleLocatedResource::isFile
bool isFile() const override final
Returns true if the located resource is a local file.
Definition: resource_locator.cpp:233
tesseract_common::SimpleLocatedResource::locateResource
Resource::Ptr locateResource(const std::string &url) const override final
Locate a resource based on a URL.
Definition: resource_locator.cpp:270
tesseract_common::SimpleLocatedResource::filename_
std::string filename_
Definition: resource_locator.h:240
tesseract_common::SimpleLocatedResource::getUrl
std::string getUrl() const override final
Get the original URL used to locate the file.
Definition: resource_locator.cpp:235
tesseract_common::GeneralResourceLocator::addPath
bool addPath(const std::filesystem::path &path)
Add path to the resource locator.
Definition: resource_locator.cpp:101


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40