Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GNASH_FACTORY_H
00022 #define GNASH_FACTORY_H
00023
00024 #ifdef HAVE_CONFIG_H
00025 # include "gnashconfig.h"
00026 #endif
00027
00028 #include <map>
00029 #include <string>
00030 #include <algorithm>
00031 #include <iterator>
00032 #include <boost/type_traits.hpp>
00033 #include <boost/utility/enable_if.hpp>
00034
00035 #include "dsodefs.h"
00036 #include "GnashAlgorithm.h"
00037
00038 namespace gnash {
00039
00040
00042
00046
00049
00055 template<typename T, typename Init = void, typename Key = std::string>
00056 class DSOEXPORT GnashFactory
00057 {
00058 public:
00059
00060 typedef T value_type;
00061 typedef Key key_type;
00062
00063 template<typename Derived>
00064 struct RegisterHandler
00065 {
00066 static T* createHandler() {
00067 return new Derived();
00068 }
00069
00070 RegisterHandler(const Key& name) {
00071 GnashFactory::instance().registerHandler(name, createHandler);
00072 }
00073 };
00074
00075 typedef T*(*CreateHandler)();
00076 typedef std::map<std::string, CreateHandler> Handlers;
00077
00079 static GnashFactory& instance() {
00080 static GnashFactory m;
00081 return m;
00082 }
00083
00085
00087 template<typename Iterator>
00088 void listKeys(Iterator i, typename boost::enable_if<boost::is_same<
00089 typename std::iterator_traits<Iterator>::iterator_category,
00090 std::output_iterator_tag> >::type* dummy = 0) {
00091 Init();
00092 static_cast<void>(dummy);
00093 std::transform(_handlers.begin(), _handlers.end(), i,
00094 FirstElement<typename Handlers::value_type>());
00095
00096 }
00097
00099
00104 T* get(const Key& name) {
00105 Init();
00106 if (name.empty()) {
00107 return _handlers.empty() ? 0 : _handlers.begin()->second();
00108 }
00109
00110 typename Handlers::const_iterator it = _handlers.find(name);
00111 if (it == _handlers.end()) return 0;
00112 return it->second();
00113 }
00114
00116
00121 void registerHandler(const Key& name, CreateHandler r) {
00122 _handlers[name] = r;
00123 }
00124
00125 private:
00126
00127 GnashFactory() {}
00128
00129 Handlers _handlers;
00130
00131 };
00132
00133 }
00134
00135 #endif