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 #ifndef Q_MOC_RUN
00040 #include <pluginlib/class_loader.h>
00041 #endif
00042 
00043 #include "rviz/class_id_recording_factory.h"
00044 #include "rviz/load_resource.h"
00045 
00046 namespace rviz
00047 {
00048 
00049 template<class Type>
00050 class PluginlibFactory: public ClassIdRecordingFactory<Type>
00051 {
00052 private:
00053   struct BuiltInClassRecord
00054   {
00055     QString class_id_;
00056     QString package_;
00057     QString name_;
00058     QString description_;
00059     Type*(*factory_function_)();
00060   };
00061 
00062 public:
00063   PluginlibFactory( const QString& package, const QString& base_class_type )
00064     {
00065       class_loader_ = new pluginlib::ClassLoader<Type>( package.toStdString(), base_class_type.toStdString() );
00066     }
00067   virtual ~PluginlibFactory()
00068     {
00069       delete class_loader_;
00070     }
00071 
00072   virtual QStringList getDeclaredClassIds()
00073     {
00074       QStringList ids;
00075       std::vector<std::string> std_ids = class_loader_->getDeclaredClasses();
00076       for( size_t i = 0; i < std_ids.size(); i++ )
00077       {
00078         ids.push_back( QString::fromStdString( std_ids[ i ]));
00079       }
00080       typename QHash<QString, BuiltInClassRecord>::const_iterator iter;
00081       for( iter = built_ins_.begin(); iter != built_ins_.end(); iter++ )
00082       {
00083         ids.push_back( iter.key() );
00084       }
00085       return ids;
00086     }
00087 
00088   virtual QString getClassDescription( const QString& class_id ) const
00089     {
00090       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00091       if( iter != built_ins_.end() )
00092       {
00093         return iter->description_;
00094       }
00095       return QString::fromStdString( class_loader_->getClassDescription( class_id.toStdString() ));
00096     }
00097 
00098   virtual QString getClassName( const QString& class_id ) const
00099     {
00100       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00101       if( iter != built_ins_.end() )
00102       {
00103         return iter->name_;
00104       }
00105       return QString::fromStdString( class_loader_->getName( class_id.toStdString() ));
00106     }
00107 
00108   virtual QString getClassPackage( const QString& class_id ) const
00109     {
00110       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00111       if( iter != built_ins_.end() )
00112       {
00113         return iter->package_;
00114       }
00115       return QString::fromStdString( class_loader_->getClassPackage( class_id.toStdString() ));
00116     }
00117 
00118   virtual QString getPluginManifestPath( const QString& class_id ) const
00119     {
00120       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00121       if( iter != built_ins_.end() )
00122       {
00123         return "";
00124       }
00125       return QString::fromStdString( class_loader_->getPluginManifestPath( class_id.toStdString() ));
00126     }
00127 
00128   virtual QIcon getIcon( const QString& class_id ) const
00129   {
00130     QString package = getClassPackage( class_id );
00131     QString class_name = getClassName( class_id );
00132     QIcon icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".svg" );
00133     if ( icon.isNull() )
00134     {
00135       icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".png" );
00136       if ( icon.isNull() )
00137       {
00138         icon = loadPixmap( "package://rviz/icons/default_class_icon.png");
00139       }
00140     }
00141     return icon;
00142   }
00143 
00144   virtual void addBuiltInClass( const QString& package, const QString& name, const QString& description,
00145                                 Type* (*factory_function)() )
00146     {
00147       BuiltInClassRecord record;
00148       record.class_id_ = package + "/" + name;
00149       record.package_ = package;
00150       record.name_ = name;
00151       record.description_ = description;
00152       record.factory_function_ = factory_function;
00153       built_ins_[ record.class_id_ ] = record;
00154     }
00155 
00156 protected:
00168   virtual Type* makeRaw( const QString& class_id, QString* error_return = NULL )
00169     {
00170       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00171       if( iter != built_ins_.end() )
00172       {
00173         Type* instance = iter->factory_function_();
00174         if( instance == NULL && error_return != NULL )
00175         {
00176           *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
00177         }
00178         return instance;
00179       }
00180       try
00181       {
00182         return class_loader_->createUnmanagedInstance( class_id.toStdString() );
00183       }
00184       catch( pluginlib::PluginlibException& ex )
00185       {
00186         ROS_ERROR( "PluginlibFactory: The plugin for class '%s' failed to load.  Error: %s",
00187                    qPrintable( class_id ), ex.what() );
00188         if( error_return )
00189         {
00190           *error_return = QString::fromStdString( ex.what() );
00191         }
00192         return NULL;
00193       }
00194     }
00195 
00196 private:
00197   pluginlib::ClassLoader<Type>* class_loader_;
00198   QHash<QString, BuiltInClassRecord> built_ins_;
00199 };
00200 
00201 } // end namespace rviz
00202 
00203 #endif // PLUGINLIB_FACTORY_H


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15