-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathclass_loader.hpp
355 lines (310 loc) · 13.8 KB
/
class_loader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (c) 2009, Willow Garage, Inc.
* All rights reserved.
*
* 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.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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 OWNER 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.
*/
#ifndef PLUGINLIB__CLASS_LOADER_HPP_
#define PLUGINLIB__CLASS_LOADER_HPP_
#include <map>
#include <string>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "class_loader/multi_library_class_loader.hpp"
#include "pluginlib/class_desc.hpp"
#include "pluginlib/class_loader_base.hpp"
#include "pluginlib/exceptions.hpp"
#include "ros/console.h"
#include "ros/package.h"
#include "tinyxml2.h" // NOLINT
// Note: pluginlib has traditionally utilized a "lookup name" for classes that does not match its
// real C++ name.
// This was done due to limitations of how pluginlib was implemented.
// As of version 1.9, a lookup name is no longer necessary and an attempt to the merge two concepts
// is underway.
namespace pluginlib
{
#if __cplusplus >= 201103L
template<typename T>
using UniquePtr = class_loader::ClassLoader::UniquePtr<T>;
#endif
/// A class to help manage and load classes.
template<class T>
class ClassLoader : public ClassLoaderBase
{
public:
typedef typename std::map<std::string, ClassDesc>::iterator ClassMapIterator;
public:
/**
* \param package The package containing the base class
* \param base_class The type of the base class for classes to be loaded
* \param attrib_name The attribute to search for in manifext.xml files, defaults to "plugin"
* \param plugin_xml_paths The list of paths of plugin.xml files, defaults to be crawled via
* ros::package::getPlugins()
* \param enable_ondemand_loadunload Set this argument to true to enable automatic library
* loading/unloading
* \throws pluginlib::ClassLoaderException if package manifest cannot be found
*/
ClassLoader(
std::string package, std::string base_class,
std::string attrib_name = std::string("plugin"),
std::vector<std::string> plugin_xml_paths = std::vector<std::string>(),
bool enable_ondemand_loadunload = false);
~ClassLoader();
/// Create an instance of a desired class, optionally loading the associated library too.
/**
* \param lookup_name The name of the class to load
* \param auto_load Specifies whether or not to automatically load the
* library containing the class, set to true by default.
* \throws pluginlib::LibraryLoadException when the library associated
* with the class cannot be loaded
* \throws pluginlib::CreateClassException when the class cannot be instantiated
* \return An instance of the class
* \deprecated use either createInstance() or createUnmanagedInstance()
*/
[[deprecated]] T * createClassInstance(
const std::string & lookup_name,
bool auto_load = true);
/// Create an instance of a desired class.
/**
* Implicitly calls loadLibraryForClass() to increment the library counter.
*
* Deleting the instance and calling unloadLibraryForClass() is automatically
* handled by the shared pointer.
* \param lookup_name The name of the class to load
* \throws pluginlib::LibraryLoadException when the library associated with
* the class cannot be loaded
* \throws pluginlib::CreateClassException when the class cannot be instantiated
* \return An instance of the class
*/
boost::shared_ptr<T> createInstance(const std::string & lookup_name);
#if __cplusplus >= 201103L
/// Create an instance of a desired class.
/**
* Implicitly calls loadLibraryForClass() to increment the library counter.
*
* Deleting the instance and calling unloadLibraryForClass() is automatically
* handled by the unique pointer.
*
* If you release the wrapped pointer you must manually call the original
* deleter when you want to destroy the released pointer.
*
* \param lookup_name The name of the class to load.
* \throws pluginlib::LibraryLoadException when the library associated with
* the class cannot be loaded.
* \throws pluginlib::CreateClassException when the class cannot be instantiated
* \return An instance of the class
*/
UniquePtr<T> createUniqueInstance(const std::string & lookup_name);
#endif
/// Create an instance of a desired class.
/**
* Implicitly calls loadLibraryForClass() to increment the library counter.
*
* \attention The ownership is transfered to the caller, which is responsible
* for deleting the instance and calling unloadLibraryForClass()
* (in order to decrement the associated library counter and unloading it
* if it is no more used).
* \param lookup_name The name of the class to load
* \throws pluginlib::LibraryLoadException when the library associated with
* the class cannot be loaded
* \throws pluginlib::CreateClassException when the class cannot be instantiated
* \return An instance of the class
*/
T * createUnmanagedInstance(const std::string & lookup_name);
/// Return a list of all available plugin manifest paths for this ClassLoader's base class type.
/**
* \return A vector of strings corresponding to the paths of all available plugin manifests
*/
std::vector<std::string> getPluginXmlPaths();
/// Return a list of all available classes for this ClassLoader's base class type.
/**
* \return A vector of strings corresponding to the names of all available classes
*/
std::vector<std::string> getDeclaredClasses();
/// Strip the package name off of a lookup name.
/**
* \param lookup_name The name of the plugin
* \return The name of the plugin stripped of the package name
*/
virtual std::string getName(const std::string & lookup_name);
/// Given the lookup name of a class, return the type of the associated base class.
/**
* \return The type of the associated base class
*/
virtual std::string getBaseClassType() const;
/// Given the lookup name of a class, return the type of the derived class associated with it.
/**
* \param lookup_name The name of the class
* \return The name of the associated derived class
*/
virtual std::string getClassType(const std::string & lookup_name);
/// Given the lookup name of a class, return its description.
/**
* \param lookup_name The lookup name of the class
* \return The description of the class
*/
virtual std::string getClassDescription(const std::string & lookup_name);
/// Given the name of a class, return the path to its associated library.
/**
* \param lookup_name The name of the class
* \return The path to the associated library
*/
virtual std::string getClassLibraryPath(const std::string & lookup_name);
/// Given the name of a class, return name of the containing package.
/**
* \param lookup_name The name of the class
* \return The name of the containing package
*/
virtual std::string getClassPackage(const std::string & lookup_name);
/// Given the name of a class, return the path of the associated plugin manifest.
/**
* \param lookup_name The name of the class
* \return The path of the associated plugin manifest
*/
virtual std::string getPluginManifestPath(const std::string & lookup_name);
/// Return the libraries that are registered and can be loaded.
/**
* \return A vector of strings corresponding to the names of registered libraries
*/
virtual std::vector<std::string> getRegisteredLibraries();
/// Check if the library for a given class is currently loaded.
/**
* \param lookup_name The lookup name of the class to query
* \return True if the class is loaded, false otherwise
*/
bool isClassLoaded(const std::string & lookup_name);
/// Check if the class associated with a plugin name is available to be loaded.
/**
* \param lookup_name The name of the plugin
* \return true if the plugin is available, false otherwise
*/
virtual bool isClassAvailable(const std::string & lookup_name);
/// Attempt to load the library containing a class with a given name.
/**
* The counter for the library uses (refcount) is also incremented.
*
* \param lookup_name The lookup name of the class to load
* \throws pluginlib::LibraryLoadException if the library for the
* class cannot be loaded
*/
virtual void loadLibraryForClass(const std::string & lookup_name);
/// Refresh the list of all available classes for this ClassLoader's base class type.
/**
* \throws pluginlib::LibraryLoadException if package manifest cannot be found
*/
virtual void refreshDeclaredClasses();
/// Decrement the counter for the library containing a class with a given name.
/**
* Also try to unload the library, If the counter reaches zero.
*
* \param lookup_name The lookup name of the class to unload
* \throws pluginlib::LibraryUnloadException if the library for the
* class cannot be unloaded
* \return The number of pending unloads until the library is removed from memory
*/
virtual int unloadLibraryForClass(const std::string & lookup_name);
private:
/// Return the paths to plugin.xml files.
/**
* \throws pluginlib::LibraryLoadException if package manifest cannot be found
* \return A vector of paths
*/
std::vector<std::string> getPluginXmlPaths(
const std::string & package,
const std::string & attrib_name,
bool force_recrawl = false);
/// Return the available classes.
/**
* \param plugin_xml_paths The vector of paths of plugin.xml files
* \throws pluginlib::LibraryLoadException if package manifest cannot be found
* \return A map of class names and the corresponding descriptions
*/
std::map<std::string, ClassDesc> determineAvailableClasses(
const std::vector<std::string> & plugin_xml_paths);
/// Open a package.xml file and extract the package name (i.e. contents of <name> tag).
/**
* \param package_xml_path The path to the package.xml file
* \return The name of the package if successful, otherwise an empty string
*/
std::string extractPackageNameFromPackageXML(const std::string & package_xml_path);
/// Get a list of paths to try to find a library.
/**
* As we transition from rosbuild to Catkin build systems, plugins can be
* found in the old rosbuild place (pkg_name/lib usually) or somewhere in the
* Catkin build space.
*/
std::vector<std::string> getAllLibraryPathsToTry(
const std::string & library_name,
const std::string & exporting_package_name);
/// Return the paths where libraries are installed according to the Catkin build system.
std::vector<std::string> getCatkinLibraryPaths();
/// Return an error message for an unknown class.
/**
* \param lookup_name The name of the class
* \return The error message
*/
std::string getErrorStringForUnknownClass(const std::string & lookup_name);
/// Get the standard path separator for the native OS (e.g. "/" on *nix, "\" on Windows).
std::string getPathSeparator();
/// Given a package name, return the path where rosbuild thinks plugins are installed.
std::string getROSBuildLibraryPath(const std::string & exporting_package_name);
/// Get the package name from a path to a plugin XML file.
std::string getPackageFromPluginXMLFilePath(const std::string & path);
/// Join two filesystem paths together utilzing appropriate path separator.
std::string joinPaths(const std::string & path1, const std::string & path2);
/// Parse a plugin XML file.
/**
* Also insert the appropriate ClassDesc entries into the passes
* classes_available map.
*/
void processSingleXMLPluginFile(
const std::string & xml_file, std::map<std::string,
ClassDesc> & class_available);
/// Strip all but the filename from an explicit file path.
/**
* \param path The path to strip
* \return The basename of the path
*/
std::string stripAllButFileFromPath(const std::string & path);
/// Helper function for unloading a shared library.
/**
* \param library_path The exact path to the library to unload
* \return The number of pending unloads until the library is removed from memory
*/
int unloadClassLibraryInternal(const std::string & library_path);
private:
std::vector<std::string> plugin_xml_paths_;
// Map from library to class's descriptions described in XML.
std::map<std::string, ClassDesc> classes_available_;
std::string package_;
std::string base_class_;
std::string attrib_name_;
class_loader::MultiLibraryClassLoader lowlevel_class_loader_; // The underlying classloader
};
} // namespace pluginlib
// Note: The implementation of the methods is in a separate file for clarity.
#include "./class_loader_imp.hpp"
#endif // PLUGINLIB__CLASS_LOADER_HPP_