1 /* $Id: filter_factory.cpp,v 1.2 2005-12-10 09:59:10 adam Exp $
2 Copyright (c) 2005, Index Data.
9 #include "filter_factory.hpp"
20 class FilterFactory::Rep {
21 typedef std::map<std::string, CreateFilterCallback> CallbackMap;
23 friend class FilterFactory;
30 yp2::FilterFactoryException::FilterFactoryException(const std::string message)
31 : std::runtime_error("FilterException: " + message)
35 yp2::FilterFactory::Rep::Rep()
39 yp2::FilterFactory::Rep::~Rep()
43 yp2::FilterFactory::FilterFactory() : m_p(new yp2::FilterFactory::Rep)
48 yp2::FilterFactory::~FilterFactory()
53 bool yp2::FilterFactory::add_creator(std::string fi,
54 CreateFilterCallback cfc)
56 return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
60 bool yp2::FilterFactory::drop_creator(std::string fi)
62 return m_p->m_fcm.erase(fi) == 1;
65 yp2::filter::Base* yp2::FilterFactory::create(std::string fi)
67 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
69 if (it == m_p->m_fcm.end()){
70 std::string msg = "filter type '" + fi + "' not found";
71 throw yp2::FilterFactoryException(msg);
73 // call create function
74 return (it->second());
78 bool yp2::FilterFactory::add_creator_dyn(const std::string &fi,
79 const std::string &path)
81 std::string full_path = path + "/yp2_filter_" + fi + ".so";
82 void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
85 const char *dl = dlerror();
86 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
91 std::string full_name = "yp2_filter_" + fi;
93 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
96 std::cout << "dlsym " << full_name << " failed\n";
99 struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
100 return add_creator(fi, s->creator);
107 * indent-tabs-mode: nil
108 * c-file-style: "stroustrup"
110 * vim: shiftwidth=4 tabstop=8 expandtab