Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
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 <boost/serialization/access.hpp>
32#include <functional>
33#include <memory>
35
36namespace tesseract_common
37{
38// Forward declare
39class Resource;
40
43{
44public:
45 using Ptr = std::shared_ptr<ResourceLocator>;
46 using ConstPtr = std::shared_ptr<const ResourceLocator>;
47
48 virtual ~ResourceLocator() = default;
49
56 virtual std::shared_ptr<Resource> locateResource(const std::string& url) const = 0;
57
58 bool operator==(const ResourceLocator& rhs) const;
59 bool operator!=(const ResourceLocator& rhs) const;
60
61private:
63 template <class Archive>
64 void serialize(Archive& ar, const unsigned int version); // NOLINT
65};
66
73{
74public:
75 using Ptr = std::shared_ptr<GeneralResourceLocator>;
76 using ConstPtr = std::shared_ptr<const GeneralResourceLocator>;
82 ~GeneralResourceLocator() override = default;
83
84 std::shared_ptr<Resource> locateResource(const std::string& url) const override;
85
86 bool operator==(const GeneralResourceLocator& rhs) const;
87 bool operator!=(const GeneralResourceLocator& rhs) const;
88
89private:
91 template <class Archive>
92 void serialize(Archive& ar, const unsigned int version); // NOLINT
93
94 std::unordered_map<std::string, std::string> package_paths_;
95};
96
99{
100public:
101 using Ptr = std::shared_ptr<Resource>;
102 using ConstPtr = std::shared_ptr<const Resource>;
103
109 virtual bool isFile() const = 0;
110
116 virtual std::string getUrl() const = 0;
117
123 virtual std::string getFilePath() const = 0;
124
130 virtual std::vector<uint8_t> getResourceContents() const = 0;
131
137 virtual std::shared_ptr<std::istream> getResourceContentStream() const = 0;
138
139 bool operator==(const Resource& rhs) const;
140 bool operator!=(const Resource& rhs) const;
141
142private:
144 template <class Archive>
145 void serialize(Archive& ar, const unsigned int version); // NOLINT
146};
147
148using SimpleResourceLocatorFn = std::function<std::string(const std::string&)>;
149
152{
153public:
154 using Ptr = std::shared_ptr<SimpleLocatedResource>;
155 using ConstPtr = std::shared_ptr<const SimpleLocatedResource>;
156
159
166 SimpleLocatedResource(std::string url, std::string filename, ResourceLocator::ConstPtr parent = nullptr);
167 ~SimpleLocatedResource() override = default;
172
173 bool isFile() const override final;
174
175 std::string getUrl() const override final;
176
177 std::string getFilePath() const override final;
178
179 std::vector<uint8_t> getResourceContents() const override final;
180
181 std::shared_ptr<std::istream> getResourceContentStream() const override final;
182
183 Resource::Ptr locateResource(const std::string& url) const override final;
184
185 bool operator==(const SimpleLocatedResource& rhs) const;
186 bool operator!=(const SimpleLocatedResource& rhs) const;
187
188private:
189 std::string url_;
190 std::string filename_;
192
193 friend class boost::serialization::access;
194 template <class Archive>
195 void serialize(Archive& ar, const unsigned int version); // NOLINT
196};
197
199{
200public:
202 BytesResource() = default;
203
204 BytesResource(std::string url, std::vector<uint8_t> bytes, ResourceLocator::ConstPtr parent = nullptr);
205 BytesResource(std::string url, const uint8_t* bytes, size_t bytes_len, ResourceLocator::ConstPtr parent = nullptr);
206 ~BytesResource() override = default;
207 BytesResource(const BytesResource&) = default;
211
212 bool isFile() const override final;
213 std::string getUrl() const override final;
214 std::string getFilePath() const override final;
215 std::vector<uint8_t> getResourceContents() const override final;
216 std::shared_ptr<std::istream> getResourceContentStream() const override final;
217 Resource::Ptr locateResource(const std::string& url) const override final;
218
219 bool operator==(const BytesResource& rhs) const;
220 bool operator!=(const BytesResource& rhs) const;
221
222private:
223 std::string url_;
224 std::vector<uint8_t> bytes_;
226
227 friend class boost::serialization::access;
228 template <class Archive>
229 void serialize(Archive& ar, const unsigned int version); // NOLINT
230};
231
232} // namespace tesseract_common
233
234#include <boost/serialization/export.hpp>
235#include <boost/serialization/tracking.hpp>
236BOOST_CLASS_EXPORT_KEY2(tesseract_common::GeneralResourceLocator, "GeneralResourceLocator")
238BOOST_CLASS_EXPORT_KEY2(tesseract_common::BytesResource, "BytesResource")
239
240#endif // TESSERACT_COMMON_RESOURCE_LOCATOR_H
#define vector(a, b, c)
Definition: FloatMath.inl:3227
Definition: resource_locator.h:199
BytesResource & operator=(const BytesResource &)=default
BytesResource & operator=(BytesResource &&)=default
BytesResource(const BytesResource &)=default
~BytesResource() override=default
BytesResource()=default
This is for boost serialization do not use directly.
BytesResource(BytesResource &&)=default
A general resource loaders using environment variable.
Definition: resource_locator.h:73
GeneralResourceLocator()
Definition: resource_locator.cpp:55
std::shared_ptr< Resource > locateResource(const std::string &url) const override
Locate a resource based on a URL.
Definition: resource_locator.cpp:110
bool operator==(const GeneralResourceLocator &rhs) const
Definition: resource_locator.cpp:151
GeneralResourceLocator & operator=(GeneralResourceLocator &&)=default
std::shared_ptr< GeneralResourceLocator > Ptr
Definition: resource_locator.h:75
std::unordered_map< std::string, std::string > package_paths_
Definition: resource_locator.h:94
GeneralResourceLocator(const GeneralResourceLocator &)=default
std::shared_ptr< const GeneralResourceLocator > ConstPtr
Definition: resource_locator.h:76
GeneralResourceLocator & operator=(const GeneralResourceLocator &)=default
friend class boost::serialization::access
Definition: resource_locator.h:90
bool operator!=(const GeneralResourceLocator &rhs) const
Definition: resource_locator.cpp:152
GeneralResourceLocator(GeneralResourceLocator &&)=default
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:155
Abstract class for resource loaders.
Definition: resource_locator.h:43
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:51
bool operator!=(const ResourceLocator &rhs) const
Definition: resource_locator.cpp:48
std::shared_ptr< const ResourceLocator > ConstPtr
Definition: resource_locator.h:46
virtual ~ResourceLocator()=default
std::shared_ptr< ResourceLocator > Ptr
Definition: resource_locator.h:45
virtual std::shared_ptr< Resource > locateResource(const std::string &url) const =0
Locate a resource based on a URL.
bool operator==(const ResourceLocator &rhs) const
Definition: resource_locator.cpp:47
friend class boost::serialization::access
Definition: resource_locator.h:62
Represents resource data available from a file or url.
Definition: resource_locator.h:99
virtual bool isFile() const =0
Returns true if the located resource is a local file.
virtual std::shared_ptr< std::istream > getResourceContentStream() const =0
Get the resource as a std::istream. This function and the returned stream may block.
bool operator==(const Resource &rhs) const
Definition: resource_locator.cpp:160
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:164
virtual std::string getUrl() const =0
Get the original URL used to locate the file.
bool operator!=(const Resource &rhs) const
Definition: resource_locator.cpp:161
std::shared_ptr< const Resource > ConstPtr
Definition: resource_locator.h:102
friend class boost::serialization::access
Definition: resource_locator.h:143
std::shared_ptr< Resource > Ptr
Definition: resource_locator.h:101
virtual std::vector< uint8_t > getResourceContents() const =0
Get the resource as bytes. This function may block.
virtual std::string getFilePath() const =0
Get the file path of the resource. Only valid if isFile() is true.
Resource implementation for a local file.
Definition: resource_locator.h:152
std::vector< uint8_t > getResourceContents() const override final
Get the resource as bytes. This function may block.
Definition: resource_locator.cpp:179
bool isFile() const override final
Returns true if the located resource is a local file.
Definition: resource_locator.cpp:173
void serialize(Archive &ar, const unsigned int version)
Definition: resource_locator.cpp:245
SimpleLocatedResource & operator=(const SimpleLocatedResource &)=default
std::string filename_
Definition: resource_locator.h:190
SimpleLocatedResource()=default
This is for boost serialization do not use directly.
std::string getFilePath() const override final
Get the file path of the resource. Only valid if isFile() is true.
Definition: resource_locator.cpp:177
SimpleLocatedResource(SimpleLocatedResource &&)=default
Resource::Ptr locateResource(const std::string &url) const override final
Locate a resource based on a URL.
Definition: resource_locator.cpp:210
std::shared_ptr< const SimpleLocatedResource > ConstPtr
Definition: resource_locator.h:155
std::string getUrl() const override final
Get the original URL used to locate the file.
Definition: resource_locator.cpp:175
SimpleLocatedResource & operator=(SimpleLocatedResource &&)=default
std::shared_ptr< SimpleLocatedResource > Ptr
Definition: resource_locator.h:154
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:199
friend class boost::serialization::access
Definition: resource_locator.h:193
std::string url_
Definition: resource_locator.h:189
SimpleLocatedResource(const SimpleLocatedResource &)=default
ResourceLocator::ConstPtr parent_
Definition: resource_locator.h:191
Common Tesseract Macros.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
Definition: serialization.h:45
Definition: allowed_collision_matrix.h:16
std::function< std::string(const std::string &)> SimpleResourceLocatorFn
Definition: resource_locator.h:148