00001
00002
00003
00004
00005
00006
00007
00008 #ifndef plugin_factory_h
00009 #define plugin_factory_h
00010
00011 #include <string>
00012 #include <map>
00013 #include <algorithm>
00014
00015 #include "BESPlugin.h"
00016
00017 using std::string;
00018 using std::map;
00019 using std::pair;
00020 using std::unary_function;
00021
00022 #include "BESObj.h"
00023
00033 template<typename C>
00034 class BESPluginFactory : public BESObj
00035 {
00036 private:
00037 map<string, BESPlugin<C> *> d_children;
00038
00045 BESPluginFactory(const BESPluginFactory &pf)
00046 throw(BESPluginException)
00047 {
00048 throw BESPluginException( "Unimplemented method.");
00049 }
00050
00054 const BESPluginFactory &operator=(const BESPluginFactory &rhs)
00055 throw (BESPluginException)
00056 {
00057 throw BESPluginException( "Unimplemented method.");
00058 }
00059
00060 struct DeletePlugins
00061 : public unary_function<pair<string, BESPlugin<C> *>, void>
00062 {
00063
00064 void operator()(pair<string, BESPlugin<C> *> elem) {
00065 delete elem.second;
00066 }
00067 };
00068
00069 public:
00079 BESPluginFactory(const string &name, const string &library_name)
00080 {
00081 add_mapping(name, library_name);
00082 }
00083
00086 BESPluginFactory() { }
00087
00088 virtual ~BESPluginFactory()
00089 {
00090 for_each(d_children.begin(), d_children.end(), DeletePlugins());
00091 }
00092
00098 void add_mapping(const string &name, const string &library_name)
00099 {
00100 BESPlugin<C> *child_class = new BESPlugin<C>(library_name);
00101 d_children.insert(std::make_pair(name, child_class));
00102 }
00103
00119 C *get(const string &name) throw(NoSuchObject, NoSuchLibrary)
00120 {
00121 BESPlugin<C> *child_implementation = d_children[name];
00122 if (!child_implementation)
00123 throw NoSuchObject(string("No class is bound to ") + name, __FILE__, __LINE__ );
00124 return child_implementation->instantiate();
00125 }
00126
00127 virtual void dump( ostream &strm ) const
00128 {
00129 strm << "BESPluginFactory::dump - (" << (void *)this << ")" << endl ;
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 }
00140 };
00141
00142 #endif //plugin_h
00143