1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "factory_filter.hpp"
31 namespace mp = metaproxy_1;
33 namespace metaproxy_1 {
34 class FactoryFilter::Rep {
35 typedef std::map<std::string, CreateFilterCallback> CallbackMap;
36 typedef std::map<std::string, CreateFilterCallback>::iterator
39 friend class FactoryFilter;
46 mp::FactoryFilter::NotFound::NotFound(const std::string message)
47 : std::runtime_error(message)
51 mp::FactoryFilter::Rep::Rep()
55 mp::FactoryFilter::Rep::~Rep()
59 mp::FactoryFilter::FactoryFilter() : m_p(new mp::FactoryFilter::Rep)
64 mp::FactoryFilter::~FactoryFilter()
69 bool mp::FactoryFilter::add_creator(const std::string &fi,
70 CreateFilterCallback cfc)
72 return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
76 bool mp::FactoryFilter::drop_creator(std::string fi)
78 return m_p->m_fcm.erase(fi) == 1;
81 bool mp::FactoryFilter::exist(std::string fi)
83 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
85 if (it == m_p->m_fcm.end())
92 mp::filter::Base* mp::FactoryFilter::create(std::string fi)
94 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
96 if (it == m_p->m_fcm.end()){
97 std::string msg = "filter type '" + fi + "' not found";
100 // call create function
101 return (it->second());
104 bool mp::FactoryFilter::add_creator_dl(const std::string &fi,
105 const std::string &path)
108 if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
112 std::string full_name = "metaproxy_1_filter_" + fi;
114 void *dl_handle = dlopen(0, RTLD_GLOBAL|RTLD_NOW);
115 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
119 std::string full_path = path + "/metaproxy_filter_" + fi + ".so";
120 dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
123 const char *dl = dlerror();
124 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
128 dlsym_ptr = dlsym(dl_handle, full_name.c_str());
132 std::cout << "dlsym " << full_name << " failed\n";
135 struct metaproxy_1_filter_struct *s = (struct metaproxy_1_filter_struct *) dlsym_ptr;
136 return add_creator(fi, s->creator);
145 * c-file-style: "Stroustrup"
146 * indent-tabs-mode: nil
148 * vim: shiftwidth=4 tabstop=8 expandtab