1 /* $Id: factory_filter.cpp,v 1.7 2007-01-25 14:05:54 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
9 #include "factory_filter.hpp"
19 namespace mp = metaproxy_1;
21 namespace metaproxy_1 {
22 class FactoryFilter::Rep {
23 typedef std::map<std::string, CreateFilterCallback> CallbackMap;
24 typedef std::map<std::string, CreateFilterCallback>::iterator
27 friend class FactoryFilter;
34 mp::FactoryFilter::NotFound::NotFound(const std::string message)
35 : std::runtime_error(message)
39 mp::FactoryFilter::Rep::Rep()
43 mp::FactoryFilter::Rep::~Rep()
47 mp::FactoryFilter::FactoryFilter() : m_p(new mp::FactoryFilter::Rep)
52 mp::FactoryFilter::~FactoryFilter()
57 bool mp::FactoryFilter::add_creator(std::string fi,
58 CreateFilterCallback cfc)
60 return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
64 bool mp::FactoryFilter::drop_creator(std::string fi)
66 return m_p->m_fcm.erase(fi) == 1;
69 bool mp::FactoryFilter::exist(std::string fi)
71 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
73 if (it == m_p->m_fcm.end())
80 mp::filter::Base* mp::FactoryFilter::create(std::string fi)
82 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
84 if (it == m_p->m_fcm.end()){
85 std::string msg = "filter type '" + fi + "' not found";
88 // call create function
89 return (it->second());
92 bool mp::FactoryFilter::have_dl_support()
101 bool mp::FactoryFilter::add_creator_dl(const std::string &fi,
102 const std::string &path)
105 if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
110 std::string full_path = path + "/metaproxy_filter_" + fi + ".so";
111 void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
114 const char *dl = dlerror();
115 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
120 std::string full_name = "metaproxy_1_filter_" + fi;
122 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
125 std::cout << "dlsym " << full_name << " failed\n";
128 struct metaproxy_1_filter_struct *s = (struct metaproxy_1_filter_struct *) dlsym_ptr;
129 return add_creator(fi, s->creator);
138 * indent-tabs-mode: nil
139 * c-file-style: "stroustrup"
141 * vim: shiftwidth=4 tabstop=8 expandtab