pluginlib_factory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef PLUGINLIB_FACTORY_H
30 #define PLUGINLIB_FACTORY_H
31 
32 #include <QHash>
33 #include <QString>
34 #include <QStringList>
35 
36 #include <string>
37 #include <vector>
38 
39 #ifndef Q_MOC_RUN
41 #endif
42 
44 #include "rviz/load_resource.h"
45 
46 namespace rviz
47 {
48 template <class Type>
50 {
51 private:
53  {
54  QString class_id_;
55  QString package_;
56  QString name_;
57  QString description_;
58  Type* (*factory_function_)();
59  };
60 
61 public:
62  PluginlibFactory(const QString& package, const QString& base_class_type)
63  {
65  new pluginlib::ClassLoader<Type>(package.toStdString(), base_class_type.toStdString());
66  }
67  ~PluginlibFactory() override
68  {
69  delete class_loader_;
70  }
71 
72  QStringList getDeclaredClassIds() override
73  {
74  QStringList ids;
75  std::vector<std::string> std_ids = class_loader_->getDeclaredClasses();
76  for (size_t i = 0; i < std_ids.size(); i++)
77  {
78  ids.push_back(QString::fromStdString(std_ids[i]));
79  }
80  typename QHash<QString, BuiltInClassRecord>::const_iterator iter;
81  for (iter = built_ins_.begin(); iter != built_ins_.end(); iter++)
82  {
83  ids.push_back(iter.key());
84  }
85  return ids;
86  }
87 
88  QString getClassDescription(const QString& class_id) const override
89  {
90  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
91  if (iter != built_ins_.end())
92  {
93  return iter->description_;
94  }
95  return QString::fromStdString(class_loader_->getClassDescription(class_id.toStdString()));
96  }
97 
98  QString getClassName(const QString& class_id) const override
99  {
100  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
101  if (iter != built_ins_.end())
102  {
103  return iter->name_;
104  }
105  return QString::fromStdString(class_loader_->getName(class_id.toStdString()));
106  }
107 
108  QString getClassPackage(const QString& class_id) const override
109  {
110  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
111  if (iter != built_ins_.end())
112  {
113  return iter->package_;
114  }
115  return QString::fromStdString(class_loader_->getClassPackage(class_id.toStdString()));
116  }
117 
118  virtual QString getPluginManifestPath(const QString& class_id) const
119  {
120  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
121  if (iter != built_ins_.end())
122  {
123  return "";
124  }
125  return QString::fromStdString(class_loader_->getPluginManifestPath(class_id.toStdString()));
126  }
127 
128  QIcon getIcon(const QString& class_id) const override
129  {
130  QString package = getClassPackage(class_id);
131  QString class_name = getClassName(class_id);
132  QIcon icon = loadPixmap("package://" + package + "/icons/classes/" + class_name + ".svg");
133  if (icon.isNull())
134  {
135  icon = loadPixmap("package://" + package + "/icons/classes/" + class_name + ".png");
136  if (icon.isNull())
137  {
138  icon = loadPixmap("package://rviz/icons/default_class_icon.png");
139  }
140  }
141  return icon;
142  }
143 
144  virtual void addBuiltInClass(const QString& package,
145  const QString& name,
146  const QString& description,
147  Type* (*factory_function)())
148  {
149  BuiltInClassRecord record;
150  record.class_id_ = package + "/" + name;
151  record.package_ = package;
152  record.name_ = name;
153  record.description_ = description;
154  record.factory_function_ = factory_function;
155  built_ins_[record.class_id_] = record;
156  }
157 
158 protected:
171  Type* makeRaw(const QString& class_id, QString* error_return = nullptr) override
172  {
173  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
174  if (iter != built_ins_.end())
175  {
176  Type* instance = iter->factory_function_();
177  if (instance == nullptr && error_return != nullptr)
178  {
179  *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
180  }
181  return instance;
182  }
183  try
184  {
185  return class_loader_->createUnmanagedInstance(class_id.toStdString());
186  }
188  {
189  ROS_ERROR("PluginlibFactory: The plugin for class '%s' failed to load. Error: %s",
190  qPrintable(class_id), ex.what());
191  if (error_return)
192  {
193  *error_return = QString::fromStdString(ex.what());
194  }
195  return nullptr;
196  }
197  }
198 
199 private:
201  QHash<QString, BuiltInClassRecord> built_ins_;
202 };
203 
204 } // end namespace rviz
205 
206 #endif // PLUGINLIB_FACTORY_H
virtual std::string getPluginManifestPath(const std::string &lookup_name)
pluginlib::ClassLoader< Type > * class_loader_
string package
QStringList getDeclaredClassIds() override
QString getClassDescription(const QString &class_id) const override
virtual std::string getClassDescription(const std::string &lookup_name)
QString getClassPackage(const QString &class_id) const override
virtual void addBuiltInClass(const QString &package, const QString &name, const QString &description, Type *(*factory_function)())
Templated factory which informs objects created by it what their class identifier string was...
T * createUnmanagedInstance(const std::string &lookup_name)
virtual std::string getClassPackage(const std::string &lookup_name)
virtual QString getPluginManifestPath(const QString &class_id) const
std::vector< std::string > getDeclaredClasses()
QString getClassName(const QString &class_id) const override
QHash< QString, BuiltInClassRecord > built_ins_
virtual std::string getName(const std::string &lookup_name)
Type * makeRaw(const QString &class_id, QString *error_return=nullptr) override
Instantiate and return a instance of a subclass of Type using our pluginlib::ClassLoader.
#define ROS_ERROR(...)
QPixmap loadPixmap(QString url, bool fill_cache)
QIcon getIcon(const QString &class_id) const override
PluginlibFactory(const QString &package, const QString &base_class_type)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sun Mar 27 2022 02:05:04