Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

PluginFactory.cc

Go to the documentation of this file.
00001 /*
00002  * PluginFactory.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Helper class to access plug-ins in dynamic linkable libraries.
00006  *
00007  * Copyright (c) 2003 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stlib/Iterator.h>
00025 #include <stlib/Set.h>
00026 #include <stlib/String.h>
00027 #include <stlib/KeyNotFoundError.h>
00028 
00029 #include <stlib/external/Plugin.h>
00030 #include <stlib/external/PluginFactory.h>
00031 
00032 using namespace External;
00033 
00034 Set *PluginFactory::_plugins = nil;
00035 String PluginFactory::defaultNewInstanceSelector("newPluginInstance");
00036 String PluginFactory::defaultPluginNameSelector("pluginName");
00037 
00038 PluginFactory::PluginFactory(String *pluginLibName)
00039   : ExternalInterface(pluginLibName)
00040 {
00041     plugins()->add(this);
00042     name = nil;
00043     new_instance_selector = &defaultNewInstanceSelector;
00044     plugin_name_selector = &defaultPluginNameSelector;
00045 }
00046 
00047 PluginFactory::~PluginFactory(void)
00048 {
00049     plugins()->remove(this);
00050     name = nil;
00051 }
00052 
00053 /* Class-accessing protocol */
00054 Set *PluginFactory::plugins(void)
00055 {
00056     if (_plugins == nil) _plugins = new Set;
00057     return _plugins;
00058 }
00059 
00060 /* Accessing protocol */
00061 void PluginFactory::newInstanceSelector(String *selector)
00062 {
00063     new_instance_selector = selector;
00064 }
00065 
00066 void PluginFactory::pluginNameSelector(String *selector)
00067 {
00068     plugin_name_selector = selector;
00069 }
00070 
00071 /* Searching protocol */
00072 PluginFactory *PluginFactory::pluginWithName(String *pluginName)
00073 {
00074     for (Iterator *i = plugins()->iterator(); !i->finished(); i->next()) {
00075         PluginFactory *plug = (PluginFactory *) i->value();
00076         if (plug->pluginName()->isEqual(pluginName)) {
00077             delete i;
00078             return plug;
00079         }
00080     }
00081     (new KeyNotFoundError(__PRETTY_FUNCTION__, pluginName))->raise();
00082     return nil; // Unreachable, but keeps compiler happy
00083 }
00084 
00085 /* Procedure protocol */
00086 Plugin *PluginFactory::newPluginInstance(void)
00087 {
00088     Plugin *(*creator)(void);
00089 
00090     creator = (Plugin *(*)(void)) symbol(new_instance_selector);
00091     return(creator());
00092 }
00093 
00094 String *PluginFactory::pluginName(void)
00095 {
00096     String *(*function)(void);
00097 
00098     if (name == nil) {
00099         function = (String *(*)(void)) symbol(plugin_name_selector);
00100         name = function();
00101     }
00102     return name;
00103 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2