1 /* $Id: factory_filter.cpp,v 1.8 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "factory_filter.hpp"
34 namespace mp = metaproxy_1;
36 namespace metaproxy_1 {
37 class FactoryFilter::Rep {
38 typedef std::map<std::string, CreateFilterCallback> CallbackMap;
39 typedef std::map<std::string, CreateFilterCallback>::iterator
42 friend class FactoryFilter;
49 mp::FactoryFilter::NotFound::NotFound(const std::string message)
50 : std::runtime_error(message)
54 mp::FactoryFilter::Rep::Rep()
58 mp::FactoryFilter::Rep::~Rep()
62 mp::FactoryFilter::FactoryFilter() : m_p(new mp::FactoryFilter::Rep)
67 mp::FactoryFilter::~FactoryFilter()
72 bool mp::FactoryFilter::add_creator(std::string fi,
73 CreateFilterCallback cfc)
75 return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
79 bool mp::FactoryFilter::drop_creator(std::string fi)
81 return m_p->m_fcm.erase(fi) == 1;
84 bool mp::FactoryFilter::exist(std::string fi)
86 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
88 if (it == m_p->m_fcm.end())
95 mp::filter::Base* mp::FactoryFilter::create(std::string fi)
97 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
99 if (it == m_p->m_fcm.end()){
100 std::string msg = "filter type '" + fi + "' not found";
103 // call create function
104 return (it->second());
107 bool mp::FactoryFilter::have_dl_support()
116 bool mp::FactoryFilter::add_creator_dl(const std::string &fi,
117 const std::string &path)
120 if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
125 std::string full_path = path + "/metaproxy_filter_" + fi + ".so";
126 void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
129 const char *dl = dlerror();
130 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
135 std::string full_name = "metaproxy_1_filter_" + fi;
137 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
140 std::cout << "dlsym " << full_name << " failed\n";
143 struct metaproxy_1_filter_struct *s = (struct metaproxy_1_filter_struct *) dlsym_ptr;
144 return add_creator(fi, s->creator);
153 * indent-tabs-mode: nil
154 * c-file-style: "stroustrup"
156 * vim: shiftwidth=4 tabstop=8 expandtab