1 /* $Id: factory_filter.cpp,v 1.3 2006-01-19 09:41:01 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 bool yp2::FactoryFilter::exist(std::string fi)
69 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
71 if (it == m_p->m_fcm.end())
78 yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
80 Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
82 if (it == m_p->m_fcm.end()){
83 std::string msg = "filter type '" + fi + "' not found";
86 // call create function
87 return (it->second());
90 bool yp2::FactoryFilter::have_dl_support()
99 bool yp2::FactoryFilter::add_creator_dl(const std::string &fi,
100 const std::string &path)
103 if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
108 std::string full_path = path + "/yp2_filter_" + fi + ".so";
109 void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
112 const char *dl = dlerror();
113 std::cout << "dlopen " << full_path << " failed. dlerror=" << dl <<
118 std::string full_name = "yp2_filter_" + fi;
120 void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
123 std::cout << "dlsym " << full_name << " failed\n";
126 struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
127 return add_creator(fi, s->creator);
136 * indent-tabs-mode: nil
137 * c-file-style: "stroustrup"
139 * vim: shiftwidth=4 tabstop=8 expandtab