00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef plugin_factory_h
00036 #define plugin_factory_h
00037
00038 #include <string>
00039 #include <map>
00040 #include <algorithm>
00041
00042 #include "BESPlugin.h"
00043
00044 using std::string;
00045 using std::map;
00046 using std::pair;
00047 using std::unary_function;
00048
00049 #include "BESObj.h"
00050
00060 template<typename C>
00061 class BESPluginFactory : public BESObj
00062 {
00063 private:
00064 map<string, BESPlugin<C> *> d_children;
00065
00072 BESPluginFactory(const BESPluginFactory &pf)
00073 throw(BESInternalError)
00074 {
00075 throw BESInternalError( "Unimplemented method.");
00076 }
00077
00081 const BESPluginFactory &operator=(const BESPluginFactory &rhs)
00082 throw (BESInternalError)
00083 {
00084 throw BESInternalError( "Unimplemented method.");
00085 }
00086
00087 struct DeletePlugins
00088 : public unary_function<pair<string, BESPlugin<C> *>, void>
00089 {
00090
00091 void operator()(pair<string, BESPlugin<C> *> elem) {
00092 delete elem.second;
00093 }
00094 };
00095
00096 public:
00106 BESPluginFactory(const string &name, const string &library_name)
00107 {
00108 add_mapping(name, library_name);
00109 }
00110
00113 BESPluginFactory() { }
00114
00115 virtual ~BESPluginFactory()
00116 {
00117 for_each(d_children.begin(), d_children.end(), DeletePlugins());
00118 }
00119
00125 void add_mapping(const string &name, const string &library_name)
00126 {
00127 BESPlugin<C> *child_class = new BESPlugin<C>(library_name);
00128 d_children.insert(std::make_pair(name, child_class));
00129 }
00130
00146 C *get(const string &name) throw(NoSuchObject, NoSuchLibrary)
00147 {
00148 BESPlugin<C> *child_implementation = d_children[name];
00149 if (!child_implementation)
00150 throw NoSuchObject(string("No class is bound to ") + name, __FILE__, __LINE__ );
00151 return child_implementation->instantiate();
00152 }
00153
00154 virtual void dump( ostream &strm ) const
00155 {
00156 strm << "BESPluginFactory::dump - (" << (void *)this << ")" << endl ;
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 }
00167 };
00168
00169 #endif //plugin_h
00170