instance_factory.cpp

00001  
00002 /***************************************************************************
00003  *  instance_factory.cpp - BlackBoard interface instance factory
00004  *
00005  *  Created: Mon Mar 03 18:01:53 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <blackboard/internal/instance_factory.h>
00025 #include <blackboard/exceptions.h>
00026 
00027 #include <interface/interface.h>
00028 
00029 #include <utils/system/dynamic_module/module_manager_factory.h>
00030 #include <utils/system/dynamic_module/module.h>
00031 #include <utils/logging/liblogger.h>
00032 
00033 #include <cstdlib>
00034 #include <cstring>
00035 
00036 namespace fawkes {
00037 
00038 /** @class BlackBoardInstanceFactory <blackboard/internal/instance_factory.h>
00039  * BlackBoard instance factory.
00040  * This class is used to interact with the interface shared object to create
00041  * and delete interface instances.
00042  *
00043  * @author Tim Niemueller
00044  */
00045 
00046 
00047 /** Constructor.*/
00048 BlackBoardInstanceFactory::BlackBoardInstanceFactory()
00049 {
00050   __mm = ModuleManagerFactory::getInstance(ModuleManagerFactory::MMT_DL, IFACEDIR);
00051 }
00052 
00053 
00054 /** Destructor */
00055 BlackBoardInstanceFactory::~BlackBoardInstanceFactory()
00056 {
00057   delete __mm;
00058 }
00059 
00060 
00061 /** Creates a new interface instance.
00062  * This method will look in the for the appropriate library in LIBDIR/interfaces
00063  * and then use the factory function for the interface of the given type. If
00064  * this was found a new instance of the interface is returned.
00065  * @param type type of the interface
00066  * @param identifier identifier of the interface
00067  * @return a new instance of the requested interface type
00068  * @exception BlackBoardInterfaceNotFoundException thrown if the factory function
00069  * for the given interface type could not be found
00070  */
00071 Interface *
00072 BlackBoardInstanceFactory::new_interface_instance(const char *type, const char *identifier)
00073 {
00074   Module *mod = NULL;
00075   std::string filename = std::string("lib") + type + "." + __mm->get_module_file_extension();
00076   try {
00077       mod = __mm->open_module(filename.c_str());
00078   } catch (Exception &e) {
00079     throw BlackBoardInterfaceNotFoundException(type, " Module file not found.");
00080   }
00081 
00082   if ( ! mod->has_symbol("interface_factory") ) {
00083     throw BlackBoardInterfaceNotFoundException(type, " Generator function not found.");
00084   }
00085 
00086   InterfaceFactoryFunc iff = (InterfaceFactoryFunc)mod->get_symbol("interface_factory");
00087 
00088   Interface *iface = iff();
00089   iface->set_type_id(type, identifier);
00090 
00091   return iface;
00092 }
00093 
00094 
00095 /** Destroy an interface instance.
00096  * The destroyer function for the given interface is called to destroy the given
00097  * interface instance.
00098  * @param interface to destroy
00099  * @exception BlackBoardInterfaceNotFoundException thrown if the destroyer function
00100  * for the given interface could not be found. The interface will not be freed.
00101  */
00102 void
00103 BlackBoardInstanceFactory::delete_interface_instance(Interface *interface)
00104 {
00105   std::string filename = std::string("lib") + interface->__type + "." + __mm->get_module_file_extension();
00106   Module *mod = __mm->get_module(filename.c_str());
00107 
00108   if ( ! mod) {
00109     throw BlackBoardInterfaceNotFoundException(interface->__type, " Interface module not opened.");
00110   }
00111 
00112   if ( ! mod->has_symbol("interface_destroy") ) {
00113     throw BlackBoardInterfaceNotFoundException(interface->__type, " Destroyer function not found.");
00114   }
00115 
00116   InterfaceDestroyFunc idf = (InterfaceDestroyFunc)mod->get_symbol("interface_destroy");
00117   idf(interface);
00118 
00119   mod->unref();
00120   __mm->close_module(mod);
00121 }
00122 
00123 } // end namespace fawkes

Generated on Tue Feb 22 13:31:25 2011 for Fawkes API by  doxygen 1.4.7