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 #include <ros/console.h>
47 
48 namespace rviz
49 {
50 template <class Type>
52 {
53 private:
55  {
56  QString class_id_;
57  QString package_;
58  QString name_;
59  QString description_;
60  Type* (*factory_function_)();
61  };
62 
63 public:
64  PluginlibFactory(const QString& package, const QString& base_class_type)
65  {
67  new pluginlib::ClassLoader<Type>(package.toStdString(), base_class_type.toStdString());
68  }
69  ~PluginlibFactory() override
70  {
71  delete class_loader_;
72  }
73 
74  QStringList getDeclaredClassIds() override
75  {
76  QStringList ids;
77  std::vector<std::string> std_ids = class_loader_->getDeclaredClasses();
78  for (size_t i = 0; i < std_ids.size(); i++)
79  {
80  ids.push_back(QString::fromStdString(std_ids[i]));
81  }
82  typename QHash<QString, BuiltInClassRecord>::const_iterator iter;
83  for (iter = built_ins_.begin(); iter != built_ins_.end(); iter++)
84  {
85  ids.push_back(iter.key());
86  }
87  return ids;
88  }
89 
90  QString getClassDescription(const QString& class_id) const override
91  {
92  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
93  if (iter != built_ins_.end())
94  {
95  return iter->description_;
96  }
97  return QString::fromStdString(class_loader_->getClassDescription(class_id.toStdString()));
98  }
99 
100  QString getClassName(const QString& class_id) const override
101  {
102  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
103  if (iter != built_ins_.end())
104  {
105  return iter->name_;
106  }
107  return QString::fromStdString(class_loader_->getName(class_id.toStdString()));
108  }
109 
110  QString getClassPackage(const QString& class_id) const override
111  {
112  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
113  if (iter != built_ins_.end())
114  {
115  return iter->package_;
116  }
117  return QString::fromStdString(class_loader_->getClassPackage(class_id.toStdString()));
118  }
119 
120  virtual QString getPluginManifestPath(const QString& class_id) const
121  {
122  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
123  if (iter != built_ins_.end())
124  {
125  return "";
126  }
127  return QString::fromStdString(class_loader_->getPluginManifestPath(class_id.toStdString()));
128  }
129 
130  QIcon getIcon(const QString& class_id) const override
131  {
132  QString package = getClassPackage(class_id);
133  QString class_name = getClassName(class_id);
134  QIcon icon = loadPixmap("package://" + package + "/icons/classes/" + class_name + ".svg");
135  if (icon.isNull())
136  {
137  icon = loadPixmap("package://" + package + "/icons/classes/" + class_name + ".png");
138  if (icon.isNull())
139  {
140  icon = loadPixmap("package://rviz/icons/default_class_icon.png");
141  }
142  }
143  return icon;
144  }
145 
146  virtual void addBuiltInClass(const QString& package,
147  const QString& name,
148  const QString& description,
149  Type* (*factory_function)())
150  {
151  BuiltInClassRecord record;
152  record.class_id_ = package + "/" + name;
153  record.package_ = package;
154  record.name_ = name;
155  record.description_ = description;
156  record.factory_function_ = factory_function;
157  built_ins_[record.class_id_] = record;
158  }
159 
160 protected:
173  Type* makeRaw(const QString& class_id, QString* error_return = nullptr) override
174  {
175  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find(class_id);
176  if (iter != built_ins_.end())
177  {
178  Type* instance = iter->factory_function_();
179  if (instance == nullptr && error_return != nullptr)
180  {
181  *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
182  }
183  return instance;
184  }
185  try
186  {
187  return class_loader_->createUnmanagedInstance(class_id.toStdString());
188  }
190  {
191  ROS_ERROR("PluginlibFactory: The plugin for class '%s' failed to load. Error: %s",
192  qPrintable(class_id), ex.what());
193  if (error_return)
194  {
195  *error_return = QString::fromStdString(ex.what());
196  }
197  return nullptr;
198  }
199  }
200 
201 private:
203  QHash<QString, BuiltInClassRecord> built_ins_;
204 };
205 
206 } // end namespace rviz
207 
208 #endif // PLUGINLIB_FACTORY_H
rviz::PluginlibFactory::BuiltInClassRecord::name_
QString name_
Definition: pluginlib_factory.h:58
rviz::PluginlibFactory::BuiltInClassRecord::class_id_
QString class_id_
Definition: pluginlib_factory.h:56
rviz::PluginlibFactory
Definition: pluginlib_factory.h:51
pluginlib::ClassLoader::getPluginManifestPath
virtual std::string getPluginManifestPath(const std::string &lookup_name)
rviz::PluginlibFactory::PluginlibFactory
PluginlibFactory(const QString &package, const QString &base_class_type)
Definition: pluginlib_factory.h:64
description
description
rviz::PluginlibFactory::getClassName
QString getClassName(const QString &class_id) const override
Definition: pluginlib_factory.h:100
rviz::loadPixmap
QPixmap loadPixmap(const QString &url, bool fill_cache)
Definition: load_resource.cpp:65
class_id_recording_factory.h
rviz::PluginlibFactory::getIcon
QIcon getIcon(const QString &class_id) const override
Definition: pluginlib_factory.h:130
pluginlib::PluginlibException
package
string package
console.h
rviz::PluginlibFactory::BuiltInClassRecord::description_
QString description_
Definition: pluginlib_factory.h:59
rviz::PluginlibFactory::~PluginlibFactory
~PluginlibFactory() override
Definition: pluginlib_factory.h:69
rviz
Definition: add_display_dialog.cpp:54
rviz::PluginlibFactory::getDeclaredClassIds
QStringList getDeclaredClassIds() override
Definition: pluginlib_factory.h:74
rviz::PluginlibFactory::getClassDescription
QString getClassDescription(const QString &class_id) const override
Definition: pluginlib_factory.h:90
pluginlib::ClassLoader< Type >
class_loader.hpp
rviz::PluginlibFactory::class_loader_
pluginlib::ClassLoader< Type > * class_loader_
Definition: pluginlib_factory.h:202
pluginlib::ClassLoader::getClassDescription
virtual std::string getClassDescription(const std::string &lookup_name)
pluginlib::ClassLoader::getClassPackage
virtual std::string getClassPackage(const std::string &lookup_name)
rviz::PluginlibFactory::makeRaw
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.
Definition: pluginlib_factory.h:173
rviz::PluginlibFactory::built_ins_
QHash< QString, BuiltInClassRecord > built_ins_
Definition: pluginlib_factory.h:203
pluginlib::ClassLoader::getName
virtual std::string getName(const std::string &lookup_name)
ROS_ERROR
#define ROS_ERROR(...)
pluginlib::ClassLoader::createUnmanagedInstance
T * createUnmanagedInstance(const std::string &lookup_name)
rviz::PluginlibFactory::BuiltInClassRecord::package_
QString package_
Definition: pluginlib_factory.h:57
rviz::PluginlibFactory::getPluginManifestPath
virtual QString getPluginManifestPath(const QString &class_id) const
Definition: pluginlib_factory.h:120
load_resource.h
rviz::PluginlibFactory::addBuiltInClass
virtual void addBuiltInClass(const QString &package, const QString &name, const QString &description, Type *(*factory_function)())
Definition: pluginlib_factory.h:146
rviz::PluginlibFactory::BuiltInClassRecord
Definition: pluginlib_factory.h:54
rviz::ClassIdRecordingFactory
Templated factory which informs objects created by it what their class identifier string was.
Definition: class_id_recording_factory.h:41
rviz::PluginlibFactory::getClassPackage
QString getClassPackage(const QString &class_id) const override
Definition: pluginlib_factory.h:110
pluginlib::ClassLoader::getDeclaredClasses
std::vector< std::string > getDeclaredClasses()


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10