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 QIcon getIcon( const QString& class_id ) const
00117   {
00118     QString package = getClassPackage( class_id );
00119     QString class_name = getClassName( class_id );
00120     QIcon icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".svg" );
00121     if ( icon.isNull() )
00122     {
00123       icon = loadPixmap( "package://"+package+"/icons/classes/"+class_name+".png" );
00124       if ( icon.isNull() )
00125       {
00126         icon = loadPixmap( "package://rviz/icons/default_class_icon.png");
00127       }
00128     }
00129     return icon;
00130   }
00131 
00132   virtual void addBuiltInClass( const QString& package, const QString& name, const QString& description,
00133                                 Type* (*factory_function)() )
00134     {
00135       BuiltInClassRecord record;
00136       record.class_id_ = package + "/" + name;
00137       record.package_ = package;
00138       record.name_ = name;
00139       record.description_ = description;
00140       record.factory_function_ = factory_function;
00141       built_ins_[ record.class_id_ ] = record;
00142     }
00143 
00144 protected:
00156   virtual Type* makeRaw( const QString& class_id, QString* error_return = NULL )
00157     {
00158       typename QHash<QString, BuiltInClassRecord>::const_iterator iter = built_ins_.find( class_id );
00159       if( iter != built_ins_.end() )
00160       {
00161         Type* instance = iter->factory_function_();
00162         if( instance == NULL && error_return != NULL )
00163         {
00164           *error_return = "Factory function for built-in class '" + class_id + "' returned NULL.";
00165         }
00166         return instance;
00167       }
00168       try
00169       {
00170         return class_loader_->createUnmanagedInstance( class_id.toStdString() );
00171       }
00172       catch( pluginlib::PluginlibException& ex )
00173       {
00174         ROS_ERROR( "PluginlibFactory: The plugin for class '%s' failed to load.  Error: %s",
00175                    qPrintable( class_id ), ex.what() );
00176         if( error_return )
00177         {
00178           *error_return = QString::fromStdString( ex.what() );
00179         }
00180         return NULL;
00181       }
00182     }
00183 
00184 private:
00185   pluginlib::ClassLoader<Type>* class_loader_;
00186   QHash<QString, BuiltInClassRecord> built_ins_;
00187 };
00188 
00189 } // end namespace rviz
00190 
00191 #endif // PLUGINLIB_FACTORY_H


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35