Gnash  0.8.11dev
GnashFactory.h
Go to the documentation of this file.
1 // GnashFactory.h A generic class template
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 
21 #ifndef GNASH_FACTORY_H
22 #define GNASH_FACTORY_H
23 
24 #ifdef HAVE_CONFIG_H
25 # include "gnashconfig.h"
26 #endif
27 
28 #include <map>
29 #include <string>
30 #include <algorithm>
31 #include <iterator>
32 #include <boost/type_traits.hpp>
33 #include <boost/utility/enable_if.hpp>
34 
35 #include "dsodefs.h"
36 #include "GnashAlgorithm.h"
37 
38 namespace gnash {
39 
40 
42 //
46 //
49 //
55 template<typename T, typename Init = void, typename Key = std::string>
57 {
58 public:
59 
60  typedef T value_type;
61  typedef Key key_type;
62 
63  template<typename Derived>
65  {
66  static T* createHandler() {
67  return new Derived();
68  }
69 
70  RegisterHandler(const Key& name) {
71  GnashFactory::instance().registerHandler(name, createHandler);
72  }
73  };
74 
75  typedef T*(*CreateHandler)();
76  typedef std::map<std::string, CreateHandler> Handlers;
77 
79  static GnashFactory& instance() {
80  static GnashFactory m;
81  return m;
82  }
83 
85  //
87  template<typename Iterator>
88  void listKeys(Iterator i, typename boost::enable_if<boost::is_same<
89  typename std::iterator_traits<Iterator>::iterator_category,
90  std::output_iterator_tag> >::type* dummy = 0) {
91  Init();
92  static_cast<void>(dummy);
93  std::transform(_handlers.begin(), _handlers.end(), i,
94  boost::bind(&Handlers::value_type::first, _1));
95  }
96 
98  //
103  T* get(const Key& name) {
104  Init();
105  if (name.empty()) {
106  return _handlers.empty() ? 0 : _handlers.begin()->second();
107  }
108 
109  typename Handlers::const_iterator it = _handlers.find(name);
110  if (it == _handlers.end()) return 0;
111  return it->second();
112  }
113 
115  //
120  void registerHandler(const Key& name, CreateHandler r) {
121  _handlers[name] = r;
122  }
123 
124 private:
125 
126  GnashFactory() {}
127 
128  Handlers _handlers;
129 
130 };
131 
132 } // namespace gnash
133 
134 #endif
T value_type
Definition: GnashFactory.h:60
void registerHandler(const Key &name, CreateHandler r)
Register a Handler with a particular name.
Definition: GnashFactory.h:120
static GnashFactory & instance()
Get the GnashFactory singleton.
Definition: GnashFactory.h:79
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
type
Definition: GnashKey.h:329
RegisterHandler(const Key &name)
Definition: GnashFactory.h:70
Definition: GnashKey.h:164
void listKeys(Iterator i, typename boost::enable_if< boost::is_same< typename std::iterator_traits< Iterator >::iterator_category, std::output_iterator_tag > >::type *dummy=0)
Dump the registered keys to the iterator.
Definition: GnashFactory.h:88
Definition: GnashFactory.h:64
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: GnashKey.h:132
Key key_type
Definition: GnashFactory.h:61
Definition: GnashKey.h:155
static T * createHandler()
Definition: GnashFactory.h:66
std::map< std::string, CreateHandler > Handlers
Definition: GnashFactory.h:76
Definition: GnashKey.h:159
A generic factory class for registering and retrieving objects by key.
Definition: GnashFactory.h:56
Definition: GnashKey.h:95
std::string name
Definition: LocalConnection_as.cpp:151