PlugInResource.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef PLUGINRESOURCE_H
00011 #define PLUGINRESOURCE_H
00012 
00013 #include "ExecutableAgent.h"
00014 #include "ExportAgent.h"
00015 #include "ImportAgent.h"
00016 #include "ObjectResource.h"
00017 #include "PlugIn.h"
00018 #include "PlugInManagerServices.h"
00019 #include "Resource.h"
00020 
00021 #include <algorithm>
00022 #include <string>
00023 #include <vector>
00024 
00025 /**
00026  *  The %PlugInObject is a trait object for use with the %Resource template.
00027  *
00028  *  The %PlugInObject is a trait object for use with the %Resource template. It
00029  *  provides capability for getting and destroying plug-in instances.
00030  * 
00031  *  @see        PlugInResource
00032  */
00033 class PlugInObject
00034 {
00035 public:
00036    class Args 
00037    {
00038    public:
00039       Args() :
00040          mpPlugIn(NULL),
00041          mNeedToCreate(false) {}
00042 
00043       Args(std::string plugInName) :
00044          mPlugInName(plugInName),
00045          mpPlugIn(NULL),
00046          mNeedToCreate(true) {}
00047 
00048       Args(char* plugInName) :
00049          mPlugInName(plugInName),
00050          mpPlugIn(NULL),
00051          mNeedToCreate(true) {}
00052 
00053       Args(PlugIn* pPlugIn) :
00054          mpPlugIn(pPlugIn),
00055          mNeedToCreate(false) {}
00056 
00057       std::string mPlugInName;
00058       PlugIn* mpPlugIn;
00059       bool mNeedToCreate;
00060    };
00061 
00062    PlugIn* obtainResource(const Args& args) const
00063    {
00064       if (args.mpPlugIn != NULL)
00065       {
00066          return args.mpPlugIn;
00067       }
00068       if (!args.mNeedToCreate)
00069       {
00070          return NULL;
00071       }
00072       Service<PlugInManagerServices> pPlugInManager;
00073       PlugIn* pPlugIn = pPlugInManager->createPlugIn(args.mPlugInName);
00074       return pPlugIn;
00075    }
00076 
00077    void releaseResource(const Args& args, PlugIn* pPlugIn) const
00078    {
00079       Service<PlugInManagerServices> pPlugInManager;
00080       pPlugInManager->destroyPlugIn(pPlugIn);
00081    }
00082 };
00083 
00084 /**
00085  *  This is a %Resource class that wraps a plug-in.
00086  *
00087  *  This is a specialized %Resource class that wraps a plug-in to ensure proper
00088  *  creation and destruction of the plug-in.  When the %PlugInResource object
00089  *  goes out of scope, the plug-in will be destroyed.
00090  *
00091  *  @see        Resource, PlugInObject
00092  */
00093 class PlugInResource : public Resource<PlugIn, PlugInObject>
00094 {
00095 public:
00096    /**
00097     *  Creates a plug-in resource which wraps no plug-in.
00098     */
00099    explicit PlugInResource() :
00100       Resource<PlugIn, PlugInObject>(PlugInObject::Args())
00101    {}
00102 
00103    /**
00104     *  Creates a plug-in resource to create a plug-in with a given name.
00105     *
00106     *  @param   plugInName
00107     *           The name of the plug-in to create.
00108     */
00109    explicit PlugInResource(const std::string& plugInName) :
00110       Resource<PlugIn, PlugInObject>(PlugInObject::Args(plugInName))
00111    {}
00112 
00113    /**
00114     *  Creates a plug-in resource to manage an existing plug-in.
00115     *
00116     *  @param   pPlugIn
00117     *           The plug-in to manage. The resource assumes ownership of the
00118     *           given plug-in and will destroy it as necessary upon resource
00119     *           destruction.
00120     */
00121    explicit PlugInResource(PlugIn* pPlugIn) :
00122       Resource<PlugIn, PlugInObject>(pPlugIn, PlugInObject::Args(pPlugIn))
00123    {}
00124 };
00125 
00126 /**
00127  *  This is a helper class when using the Executable interface.
00128  *
00129  *  This class manages an instance of an ExecutableAgent that provides the
00130  *  actual helper methods when working with instances of Executable plug-ins.
00131  *
00132  *  @see        ExecutableAgent, Executable
00133  */
00134 class ExecutableResource : public FactoryResource<ExecutableAgent>
00135 {
00136 public:
00137    /**
00138     *  @copydoc ExecutableAgent::instantiate(Progress*, bool)
00139     *
00140     *  @par     ExecutableAgent
00141     *           This class simply allocates and controls the lifecycle of an
00142     *           ExecutableAgent object.  You can use -> to access any of the
00143     *           methods available on ExecutableAgent.
00144     */
00145    explicit ExecutableResource(Progress* pProgress = NULL, bool batch = true)
00146    {
00147       ExecutableAgent* pAgent = get();
00148       if (pAgent != NULL)
00149       {
00150          pAgent->instantiate(pProgress, batch);
00151       }
00152    }
00153 
00154    /**
00155     *  @copydoc ExecutableAgent::instantiate(const std::string&, const std::string&, Progress*, bool)
00156     *
00157     *  @par     ExecutableAgent
00158     *           This class simply allocates and controls the lifecycle of an
00159     *           ExecutableAgent object.  You can use -> to access any of the
00160     *           methods available on ExecutableAgent.
00161     */
00162    explicit ExecutableResource(const std::string& plugInName, const std::string& menuCommand = std::string(),
00163                                Progress* pProgress = NULL, bool batch = true)
00164    {
00165       ExecutableAgent* pAgent = get();
00166       if (pAgent != NULL)
00167       {
00168          pAgent->instantiate(plugInName, menuCommand, pProgress, batch);
00169       }
00170    }
00171 
00172    /**
00173     *  @copydoc ExecutableAgent::instantiate(PlugIn*, const std::string&, Progress*, bool)
00174     *
00175     *  @par     ExecutableAgent
00176     *           This class simply allocates and controls the lifecycle of an
00177     *           ExecutableAgent object.  You can use -> to access any of the
00178     *           methods available on ExecutableAgent.
00179     */
00180    explicit ExecutableResource(PlugIn* pPlugIn, const std::string& menuCommand = std::string(),
00181                                Progress* pProgress = NULL, bool batch = true)
00182    {
00183       ExecutableAgent* pAgent = get();
00184       if (pAgent != NULL)
00185       {
00186          pAgent->instantiate(pPlugIn, menuCommand, pProgress, batch);
00187       }
00188    }
00189 };
00190 
00191 /**
00192  *  This is a helper class when using the Importer interface.
00193  *
00194  *  This class manages an instance of an ImportAgent that provides the actual
00195  *  helper methods when working with instances of Importer plug-ins.
00196  *
00197  *  @see        ImportAgent, Importer, \ref usingimporterresource "Using ImporterResource"
00198  */
00199 class ImporterResource : public FactoryResource<ImportAgent>
00200 {
00201 public:
00202    /**
00203     *  @copydoc ImportAgent::instantiate(Progress*, bool)
00204     *
00205     *  @par     ImportAgent
00206     *           This class simply allocates and controls the lifecycle of an
00207     *           ImportAgent object.  You can use -> to access any of the methods
00208     *           available on ImportAgent.
00209     */
00210    ImporterResource(Progress* pProgress = NULL, bool batch = true)
00211    {
00212       ImportAgent* pAgent = get();
00213       if (pAgent != NULL)
00214       {
00215          pAgent->instantiate(pProgress, batch);
00216       }
00217    }
00218 
00219    /**
00220     *  @copydoc ImportAgent::instantiate(const std::string&, Progress*, bool)
00221     *
00222     *  @par     ImportAgent
00223     *           This class simply allocates and controls the lifecycle of an
00224     *           ImportAgent object.  You can use -> to access any of the methods
00225     *           available on ImportAgent.
00226     */
00227    ImporterResource(const std::string& importerName, Progress* pProgress = NULL, bool batch = true)
00228    {
00229       ImportAgent* pAgent = get();
00230       if (pAgent != NULL)
00231       {
00232          pAgent->instantiate(importerName, pProgress, batch);
00233       }
00234    }
00235 
00236    /**
00237     *  @copydoc ImportAgent::instantiate(const std::string&, const std::string&, Progress*, bool)
00238     *
00239     *  @par     ImportAgent
00240     *           This class simply allocates and controls the lifecycle of an
00241     *           ImportAgent object.  You can use -> to access any of the methods
00242     *           available on ImportAgent.
00243     */
00244    ImporterResource(const std::string& importerName, const std::string& filename, Progress* pProgress = NULL,
00245                     bool batch = true)
00246    {
00247       ImportAgent* pAgent = get();
00248       if (pAgent != NULL)
00249       {
00250          pAgent->instantiate(importerName, filename, pProgress, batch);
00251       }
00252    }
00253 
00254    /**
00255     *  @copydoc ImportAgent::instantiate(const std::string&, const std::vector<std::string>&, Progress*, bool)
00256     *
00257     *  @par     ImportAgent
00258     *           This class simply allocates and controls the lifecycle of an
00259     *           ImportAgent object.  You can use -> to access any of the methods
00260     *           available on ImportAgent.
00261     */
00262    ImporterResource(const std::string& importerName, const std::vector<std::string>& filenames,
00263                     Progress* pProgress = NULL, bool batch = true)
00264    {
00265       ImportAgent* pAgent = get();
00266       if (pAgent != NULL)
00267       {
00268          pAgent->instantiate(importerName, filenames, pProgress, batch);
00269       }
00270    }
00271 
00272    /**
00273     *  @copydoc ImportAgent::instantiate(const std::string&, const std::map<std::string, std::vector<ImportDescriptor*> >&, Progress*, bool)
00274     *
00275     *  @par     ImportAgent
00276     *           This class simply allocates and controls the lifecycle of an
00277     *           ImportAgent object.  You can use -> to access any of the methods
00278     *           available on ImportAgent.
00279     */
00280    ImporterResource(const std::string& importerName,
00281                     const std::map<std::string, std::vector<ImportDescriptor*> >& datasets,
00282                     Progress* pProgress = NULL, bool batch = true)
00283    {
00284       ImportAgent* pAgent = get();
00285       if (pAgent != NULL)
00286       {
00287          pAgent->instantiate(importerName, datasets, pProgress, batch);
00288       }
00289    }
00290 
00291    /**
00292     *  @copydoc ImportAgent::instantiate(PlugIn*, const std::map<std::string, std::vector<ImportDescriptor*> >&, Progress*, bool)
00293     *
00294     *  @par     ImportAgent
00295     *           This class simply allocates and controls the lifecycle of an
00296     *           ImportAgent object.  You can use -> to access any of the methods
00297     *           available on ImportAgent.
00298     */
00299    ImporterResource(PlugIn* pPlugIn, const std::map<std::string, std::vector<ImportDescriptor*> >& descriptors,
00300                     Progress* pProgress = NULL, bool batch = true)
00301    {
00302       ImportAgent* pAgent = get();
00303       if (pAgent != NULL)
00304       {
00305          pAgent->instantiate(pPlugIn, descriptors, pProgress, batch);
00306       }
00307    }
00308 };
00309 
00310 /**
00311  *  This is a helper class when using the Exporter interface.
00312  *
00313  *  This class manages an instance of an ExportAgent that provides the actual
00314  *  helper methods when working with instances of Exporter plug-ins.
00315  *
00316  *  @see        ExportAgent, Exporter
00317  */
00318 class ExporterResource : public FactoryResource<ExportAgent>
00319 {
00320 public:
00321    /**
00322     *  @copydoc ExportAgent::instantiate(Progress*, bool)
00323     *
00324     *  @par     ExportAgent
00325     *           This class simply allocates and controls the lifecycle of an
00326     *           ExportAgent object.  You can use -> to access any of the methods
00327     *           available on ExportAgent.
00328     */
00329    ExporterResource(Progress* pProgress = NULL, bool batch = true)
00330    {
00331       ExportAgent* pAgent = get();
00332       if (pAgent != NULL)
00333       {
00334          pAgent->instantiate(pProgress, batch);
00335       }
00336    }
00337 
00338    /**
00339     *  @copydoc ExportAgent::instantiate(std::string, Progress*, bool)
00340     *
00341     *  @par     ExportAgent
00342     *           This class simply allocates and controls the lifecycle of an
00343     *           ExportAgent object.  You can use -> to access any of the methods
00344     *           available on ExportAgent.
00345     */
00346    ExporterResource(std::string exporterName, Progress* pProgress = NULL, bool batch = true)
00347    {
00348       ExportAgent* pAgent = get();
00349       if (pAgent != NULL)
00350       {
00351          pAgent->instantiate(exporterName, pProgress, batch);
00352       }
00353    }
00354 
00355    /**
00356     *  @copydoc ExportAgent::instantiate(PlugIn*, Progress*, bool)
00357     *
00358     *  @par     ExportAgent
00359     *           This class simply allocates and controls the lifecycle of an
00360     *           ExportAgent object.  You can use -> to access any of the methods
00361     *           available on ExportAgent.
00362     */
00363    ExporterResource(PlugIn* pPlugIn, Progress* pProgress = NULL, bool batch = true)
00364    {
00365       ExportAgent* pAgent = get();
00366       if (pAgent != NULL)
00367       {
00368          pAgent->instantiate(pPlugIn, pProgress, batch);
00369       }
00370    }
00371 
00372    /**
00373     *  @copydoc ExportAgent::instantiate(std::string, SessionItem*, FileDescriptor*, Progress*, bool)
00374     *
00375     *  @par     ExportAgent
00376     *           This class simply allocates and controls the lifecycle of an
00377     *           ExportAgent object.  You can use -> to access any of the methods
00378     *           available on ExportAgent.
00379     */
00380    ExporterResource(std::string exporterName, SessionItem* pItem, FileDescriptor* pFileDescriptor,
00381                     Progress* pProgress = NULL, bool batch = true)
00382    {
00383       ExportAgent* pAgent = get();
00384       if (pAgent != NULL)
00385       {
00386          pAgent->instantiate(exporterName, pItem, pFileDescriptor, pProgress, batch);
00387       }
00388    }
00389 
00390    /**
00391     *  @copydoc ExportAgent::instantiate(PlugIn*, SessionItem*, FileDescriptor*, Progress*, bool)
00392     *
00393     *  @par     ExportAgent
00394     *           This class simply allocates and controls the lifecycle of an
00395     *           ExportAgent object.  You can use -> to access any of the methods
00396     *           available on ExportAgent.
00397     */
00398    ExporterResource(PlugIn* pPlugIn, SessionItem* pItem, FileDescriptor* pFileDescriptor,
00399                     Progress* pProgress = NULL, bool batch = true)
00400    {
00401       ExportAgent* pAgent = get();
00402       if (pAgent != NULL)
00403       {
00404          pAgent->instantiate(pPlugIn, pItem, pFileDescriptor, pProgress, batch);
00405       }
00406    }
00407 };
00408 
00409 #endif

Software Development Kit - Opticks 4.9.0 Build 16218