Gnash 0.8.9
|
00001 // GnashFactory.h A generic class template 00002 // 00003 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 00004 // 2011 Free Software Foundation, Inc 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 } // namespace gnash 00134 00135 #endif