pluginlib_factory.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #ifndef PLUGINLIB_FACTORY_H
00030 #define PLUGINLIB_FACTORY_H
00031 
00032 #include <QHash>
00033 #include <QString>
00034 #include <QStringList>
00035 
00036 #include <string>
00037 #include <vector>
00038 
00039 #include <pluginlib/class_loader.h>
00040 
00041 #include "rviz/class_id_recording_factory.h"
00042 #include "rviz/load_resource.h"
00043 
00044 namespace rviz
00045 {
00046 
00047 template<class Type>
00048 class PluginlibFactory: public ClassIdRecordingFactory<Type>
00049 {
00050 private:
00051   struct BuiltInClassRecord
00052   {
00053     QString class_id_;
00054     QString package_;
00055     QString name_;
00056     QString description_;
00057     Type*(*factory_function_)();
00058   };
00059 
00060 public:
00061   PluginlibFactory( const QString& package, const QString& base_class_type )
00062     {
00063       class_loader_ = new pluginlib::ClassLoader<Type>( package.toStdString(), base_class_type.toStdString() );
00064     }
00065   virtual ~PluginlibFactory()
00066     {
00067       delete class_loader_;
00068     }
00069 
00070   virtual QStringList getDeclaredClassIds()
00071     {
00072       QStringList ids;
00073       std::vector<std::string> std_ids = class_loader_->getDeclaredClasses();
00074       for( size_t i = 0; i < std_ids.size(); i++ )
00075       {
00076         ids.push_back( QString::fromStdString( std_ids[ i ]));
00077       }
00078       typename QHash<QString, BuiltInClassRecord>::const_iterator iter;
00079       for( iter = built_ins_.begin(); iter != built_ins_.end(); iter++ )
00080       {
00081         ids.push_back( iter.key() );
00082       }
00083       return ids;
00084     }
00085 
00086   virtual QString getClassDescription( const QString& class_id ) const
00087     {
00088       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00089       if( iter != built_ins_.end() )
00090       {
00091         return iter->description_;
00092       }
00093       return QString::fromStdString( class_loader_->getClassDescription( class_id.toStdString() ));
00094     }
00095 
00096   virtual QString getClassName( const QString& class_id ) const
00097     {
00098       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00099       if( iter != built_ins_.end() )
00100       {
00101         return iter->name_;
00102       }
00103       return QString::fromStdString( class_loader_->getName( class_id.toStdString() ));
00104     }
00105 
00106   virtual QString getClassPackage( const QString& class_id ) const
00107     {
00108       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00109       if( iter != built_ins_.end() )
00110       {
00111         return iter->package_;
00112       }
00113       return QString::fromStdString( class_loader_->getClassPackage( class_id.toStdString() ));
00114     }
00115 
00116   virtual QString getPluginManifestPath( const QString& class_id ) const
00117     {
00118       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00119       if( iter != built_ins_.end() )
00120       {
00121         return "";
00122       }
00123       return QString::fromStdString( class_loader_->getPluginManifestPath( class_id.toStdString() ));
00124     }
00125 
00126   virtual QIcon getIcon( const QString& class_id ) const
00127   {
00128     QString package = getClassPackage( class_id );
00129     QString class_name = getClassName( class_id );
00130     QIcon icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".svg" );
00131     if ( icon.isNull() )
00132     {
00133       icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".png" );
00134       if ( icon.isNull() )
00135       {
00136         icon = loadPixmap( "package://rviz/icons/default_class_icon.png");
00137       }
00138     }
00139     return icon;
00140   }
00141 
00142   virtual void addBuiltInClass( const QString& package, const QString& name, const QString& description,
00143                                 Type* (*factory_function)() )
00144     {
00145       BuiltInClassRecord record;
00146       record.class_id_ = package + "/" + name;
00147       record.package_ = package;
00148       record.name_ = name;
00149       record.description_ = description;
00150       record.factory_function_ = factory_function;
00151       built_ins_[ record.class_id_ ] = record;
00152     }
00153 
00154 protected:
00166   virtual Type* makeRaw( const QString& class_id, QString* error_return = NULL )
00167     {
00168       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00169       if( iter != built_ins_.end() )
00170       {
00171         Type* instance = iter->factory_function_();
00172         if( instance == NULL && error_return != NULL )
00173         {
00174           *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
00175         }
00176         return instance;
00177       }
00178       try
00179       {
00180         return class_loader_->createUnmanagedInstance( class_id.toStdString() );
00181       }
00182       catch( pluginlib::PluginlibException& ex )
00183       {
00184         ROS_ERROR( "PluginlibFactory: The plugin for class '%s' failed to load.  Error: %s",
00185                    qPrintable( class_id ), ex.what() );
00186         if( error_return )
00187         {
00188           *error_return = QString::fromStdString( ex.what() );
00189         }
00190         return NULL;
00191       }
00192     }
00193 
00194 private:
00195   pluginlib::ClassLoader<Type>* class_loader_;
00196   QHash<QString, BuiltInClassRecord> built_ins_;
00197 };
00198 
00199 } // end namespace rviz
00200 
00201 #endif // PLUGINLIB_FACTORY_H


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27