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 <type_traits>
33 
34 #include "dsodefs.h"
35 #include "GnashAlgorithm.h"
36 
37 namespace gnash {
38 
39 
41 //
45 //
48 //
54 template<typename T, typename Init, typename Key>
56 {
57 public:
58 
59  typedef T value_type;
60  typedef Key key_type;
61 
62  template<typename Derived>
64  {
65  static T* createHandler() {
66  return new Derived();
67  }
68 
69  RegisterHandler(const Key& name) {
70  GnashFactory::instance().registerHandler(name, createHandler);
71  }
72  };
73 
74  typedef T*(*CreateHandler)();
75  typedef std::map<std::string, CreateHandler> Handlers;
76 
78  static GnashFactory& instance() {
79  static GnashFactory m;
80  return m;
81  }
82 
84  //
86  template<typename Iterator>
87  void
88  listKeys(Iterator i) {
89  typedef typename std::iterator_traits<Iterator>::iterator_category cat;
90  static_assert(std::is_same<cat, std::output_iterator_tag>::value,
91  "i must be an output iterator.");
92  Init();
93  std::transform(_handlers.begin(), _handlers.end(), i,
94  std::bind(&Handlers::value_type::first, std::placeholders::_1));
95  }
96 
98  //
103  T* get(const Key& name) {
104  Init();
105  if (name.empty()) {
106  return _handlers.empty() ? nullptr : _handlers.begin()->second();
107  }
108 
109  typename Handlers::const_iterator it = _handlers.find(name);
110  if (it == _handlers.end()) return nullptr;
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:59
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:78
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
RegisterHandler(const Key &name)
Definition: GnashFactory.h:69
Definition: GnashKey.h:164
Definition: GnashFactory.h:63
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: GnashKey.h:132
Key key_type
Definition: GnashFactory.h:60
Definition: GnashKey.h:155
void listKeys(Iterator i)
Dump the registered keys to the iterator.
Definition: GnashFactory.h:88
static T * createHandler()
Definition: GnashFactory.h:65
std::map< std::string, CreateHandler > Handlers
Definition: GnashFactory.h:75
Definition: GnashKey.h:159
A generic factory class for registering and retrieving objects by key.
Definition: GnashFactory.h:55
Definition: GnashKey.h:95
std::string name
Definition: LocalConnection_as.cpp:149