NEST  2.6.0,not_revisioned_source_dir@0
generic_factory.h
Go to the documentation of this file.
1 /*
2  * generic_factory.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef GENERIC_FACTORY_H
24 #define GENERIC_FACTORY_H
25 
26 #include <map>
27 #include "nest.h"
28 #include "dictdatum.h"
29 
30 namespace nest
31 {
32  class AbstractGeneric;
33 
42  template<class BaseT>
44  public:
45  typedef BaseT * (*CreatorFunction)(const DictionaryDatum &d);
46  typedef std::map<Name,CreatorFunction> AssocMap;
47 
54  BaseT *create(const Name& name, const DictionaryDatum &d) const;
55 
64  template<class T>
65  bool register_subtype(const Name & name);
66 
74  bool register_subtype(const Name& name, CreatorFunction creator);
75 
76  private:
77  template<class T>
78  static
79  BaseT * new_from_dict_(const DictionaryDatum &d);
80 
82  };
83 
84  template<class BaseT>
85  inline
86  BaseT * GenericFactory<BaseT>::create(const Name& name, const DictionaryDatum &d) const
87  {
88  typename AssocMap::const_iterator i = associations_.find(name);
89  if ( i != associations_.end() ) {
90  return (i->second)(d);
91  }
92  throw UndefinedName(name.toString());
93  }
94 
95  template<class BaseT>
96  template<class T>
97  inline
99  {
100  return register_subtype(name,new_from_dict_<T>);
101  }
102 
103  template<class BaseT>
104  inline
105  bool GenericFactory<BaseT>::register_subtype(const Name& name, CreatorFunction creator)
106  {
107  return associations_.insert(std::pair<Name,CreatorFunction>(name,creator)).second;
108  }
109 
110  template<class BaseT>
111  template<class T>
113  return new T(d);
114  }
115 
116 } // namespace nest
117 
118 #endif
BaseT * create(const Name &name, const DictionaryDatum &d) const
Factory function.
Definition: generic_factory.h:86
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
bool register_subtype(const Name &name)
Register a new subtype.
Definition: generic_factory.h:98
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
std::map< Name, CreatorFunction > AssocMap
Definition: generic_factory.h:46
const std::string & toString(void) const
Return string represented by Name.
Definition: name.cc:63
static BaseT * new_from_dict_(const DictionaryDatum &d)
Definition: generic_factory.h:112
AssocMap associations_
Definition: generic_factory.h:81
BaseT *(* CreatorFunction)(const DictionaryDatum &d)
Definition: generic_factory.h:45
Generic Factory class for objects deriving from a base class BaseT.
Definition: generic_factory.h:43
Default types used by the NEST kernel.
Exception to be thrown if an entry referenced inside a dictionary does not exist. ...
Definition: sliexceptions.h:263