X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffactory_filter.cpp;h=a0c3d6eb294e4c9f2619e10fd9b4238ef8417b74;hb=586d78659d671683f33ec55f4a7d32b28e345ccd;hp=de786ff61fd9d76f12022bdf86339495c1d41ad0;hpb=14d0e634d5061208301502d813d488d1e82f190b;p=metaproxy-moved-to-github.git diff --git a/src/factory_filter.cpp b/src/factory_filter.cpp index de786ff..a0c3d6e 100644 --- a/src/factory_filter.cpp +++ b/src/factory_filter.cpp @@ -1,8 +1,20 @@ -/* $Id: factory_filter.cpp,v 1.1 2006-01-04 14:30:51 adam Exp $ - Copyright (c) 2005, Index Data. +/* This file is part of Metaproxy. + Copyright (C) Index Data -%LICENSE% - */ +Metaproxy is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "config.hpp" @@ -16,10 +28,12 @@ #include #include -namespace yp2 { +namespace mp = metaproxy_1; + +namespace metaproxy_1 { class FactoryFilter::Rep { typedef std::map CallbackMap; - typedef std::map::iterator + typedef std::map::iterator CallbackMapIt; public: friend class FactoryFilter; @@ -29,90 +43,108 @@ namespace yp2 { }; } -yp2::FactoryFilterException::FactoryFilterException(const std::string message) - : std::runtime_error("FilterException: " + message) +mp::FactoryFilter::NotFound::NotFound(const std::string message) + : std::runtime_error(message) { } -yp2::FactoryFilter::Rep::Rep() +mp::FactoryFilter::Rep::Rep() { } -yp2::FactoryFilter::Rep::~Rep() +mp::FactoryFilter::Rep::~Rep() { } -yp2::FactoryFilter::FactoryFilter() : m_p(new yp2::FactoryFilter::Rep) +mp::FactoryFilter::FactoryFilter() : m_p(new mp::FactoryFilter::Rep) { } -yp2::FactoryFilter::~FactoryFilter() +mp::FactoryFilter::~FactoryFilter() { } -bool yp2::FactoryFilter::add_creator(std::string fi, - CreateFilterCallback cfc) +bool mp::FactoryFilter::add_creator(const std::string &fi, + CreateFilterCallback cfc) { return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second; } -bool yp2::FactoryFilter::drop_creator(std::string fi) +bool mp::FactoryFilter::drop_creator(std::string fi) { return m_p->m_fcm.erase(fi) == 1; } -yp2::filter::Base* yp2::FactoryFilter::create(std::string fi) +bool mp::FactoryFilter::exist(std::string fi) +{ + Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi); + + if (it == m_p->m_fcm.end()) + { + return false; + } + return true; +} + +mp::filter::Base* mp::FactoryFilter::create(std::string fi) { Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi); - + if (it == m_p->m_fcm.end()){ std::string msg = "filter type '" + fi + "' not found"; - throw yp2::FactoryFilterException(msg); + throw NotFound(msg); } // call create function return (it->second()); } -#if HAVE_DLFCN_H -bool yp2::FactoryFilter::add_creator_dyn(const std::string &fi, - const std::string &path) +bool mp::FactoryFilter::add_creator_dl(const std::string &fi, + const std::string &path) { +#if HAVE_DLFCN_H if (m_p->m_fcm.find(fi) != m_p->m_fcm.end()) { return true; } + std::string full_name = "metaproxy_1_filter_" + fi; - std::string full_path = path + "/yp2_filter_" + fi + ".so"; - void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW); - if (!dl_handle) + void *dl_handle = dlopen(0, RTLD_GLOBAL|RTLD_NOW); + void *dlsym_ptr = dlsym(dl_handle, full_name.c_str()); + + if (!dlsym_ptr) { - const char *dl = dlerror(); - std::cout << "dlopen " << full_path << " failed. dlerror=" << dl << - std::endl; - return false; + std::string full_path = path + "/metaproxy_filter_" + fi + ".so"; + dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW); + if (!dl_handle) + { + const char *dl = dlerror(); + std::cout << "dlopen " << full_path << " failed. dlerror=" << dl << + std::endl; + return false; + } + dlsym_ptr = dlsym(dl_handle, full_name.c_str()); } - - std::string full_name = "yp2_filter_" + fi; - - void *dlsym_ptr = dlsym(dl_handle, full_name.c_str()); if (!dlsym_ptr) { std::cout << "dlsym " << full_name << " failed\n"; return false; } - struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr; + struct metaproxy_1_filter_struct *s = (struct metaproxy_1_filter_struct *) dlsym_ptr; return add_creator(fi, s->creator); -} +#else + return false; #endif +} /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil - * c-file-style: "stroustrup" * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +