1 /* $Id: factory_filter.cpp,v 1.2 2006-01-05 16:39:37 adam Exp $
2 Copyright (c) 2005, Index Data.
9 #include "factory_filter.hpp"
20 class FactoryFilter::Rep {
21 typedef std::map<std::string, CreateFilterCallback> CallbackMap;
22 typedef std::map<std::string, CreateFilterCallback>::iterator
25 friend class FactoryFilter;
32 yp2::FactoryFilter::NotFound::NotFound(const std::string message)
33 : std::runtime_error(message)
37 yp2::FactoryFilter::Rep::Rep()
41 yp2::FactoryFilter::Rep::~Rep()
45 yp2::FactoryFilter::FactoryFilter() : m_p(new yp2::FactoryFilter::Rep)
50 yp2::FactoryFilter::~FactoryFilter()
55 bool yp2::FactoryFilter::add_creator(std::string fi,
56 CreateFilterCallback cfc)
58 return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
62 bool yp2::FactoryFilter::drop_creator(std::string fi)
64 return m_p->m_fcm.erase(fi) == 1;
67 yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
69 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
71 if (it == m_p->m_fcm.end()){
72 std::string msg = "filter type '" + fi + "' not found";
75 // call create function
76 return (it->second());
80 bool yp2::FactoryFilter::add_creator_dyn(const std::string &fi,
81 const std::string &path)
83 if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
88 std::string full_path = path + "/yp2_filter_" + fi + ".so";
89 void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
92 const char *dl = dlerror();
93 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
98 std::string full_name = "yp2_filter_" + fi;
100 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
103 std::cout << "dlsym " << full_name << " failed\n";
106 struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
107 return add_creator(fi, s->creator);
114 * indent-tabs-mode: nil
115 * c-file-style: "stroustrup"
117 * vim: shiftwidth=4 tabstop=8 expandtab