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 
49 template<class Type>
51 {
52 private:
54  {
55  QString class_id_;
56  QString package_;
57  QString name_;
58  QString description_;
59  Type*(*factory_function_)();
60  };
61 
62 public:
63  PluginlibFactory( const QString& package, const QString& base_class_type )
64  {
65  class_loader_ = new pluginlib::ClassLoader<Type>( package.toStdString(), base_class_type.toStdString() );
66  }
68  {
69  delete class_loader_;
70  }
71 
72  virtual QStringList getDeclaredClassIds()
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  virtual QString getClassDescription( const QString& class_id ) const
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  virtual QString getClassName( const QString& class_id ) const
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  virtual QString getClassPackage( const QString& class_id ) const
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  virtual QIcon getIcon( const QString& class_id ) const
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, const QString& name, const QString& description,
145  Type* (*factory_function)() )
146  {
147  BuiltInClassRecord record;
148  record.class_id_ = package + "/" + name;
149  record.package_ = package;
150  record.name_ = name;
151  record.description_ = description;
152  record.factory_function_ = factory_function;
153  built_ins_[ record.class_id_ ] = record;
154  }
155 
156 protected:
168  virtual Type* makeRaw( const QString& class_id, QString* error_return = NULL )
169  {
170  typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
171  if( iter != built_ins_.end() )
172  {
173  Type* instance = iter->factory_function_();
174  if( instance == NULL && error_return != NULL )
175  {
176  *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
177  }
178  return instance;
179  }
180  try
181  {
182  return class_loader_->createUnmanagedInstance( class_id.toStdString() );
183  }
184  catch( pluginlib::PluginlibException& ex )
185  {
186  ROS_ERROR( "PluginlibFactory: The plugin for class '%s' failed to load. Error: %s",
187  qPrintable( class_id ), ex.what() );
188  if( error_return )
189  {
190  *error_return = QString::fromStdString( ex.what() );
191  }
192  return NULL;
193  }
194  }
195 
196 private:
198  QHash<QString, BuiltInClassRecord> built_ins_;
199 };
200 
201 } // end namespace rviz
202 
203 #endif // PLUGINLIB_FACTORY_H
virtual std::string getPluginManifestPath(const std::string &lookup_name)
#define NULL
Definition: global.h:37
pluginlib::ClassLoader< Type > * class_loader_
string package
virtual Type * makeRaw(const QString &class_id, QString *error_return=NULL)
Instantiate and return a instance of a subclass of Type using our pluginlib::ClassLoader.
virtual std::string getClassDescription(const std::string &lookup_name)
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 QString getClassName(const QString &class_id) const
virtual QIcon getIcon(const QString &class_id) const
virtual std::string getClassPackage(const std::string &lookup_name)
virtual QStringList getDeclaredClassIds()
virtual QString getClassPackage(const QString &class_id) const
std::vector< std::string > getDeclaredClasses()
QHash< QString, BuiltInClassRecord > built_ins_
virtual std::string getName(const std::string &lookup_name)
virtual QString getClassDescription(const QString &class_id) const
#define ROS_ERROR(...)
QPixmap loadPixmap(QString url, bool fill_cache)
PluginlibFactory(const QString &package, const QString &base_class_type)
virtual QString getPluginManifestPath(const QString &class_id) const


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51