NEST main@caf0ae8
 
Loading...
Searching...
No Matches
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// C++ includes:
27#include <map>
28
29// Includes from nestkernel:
30#include "generic_factory.h"
31#include "nest_types.h"
32
33// Includes from nestutil:
34#include "dictionary.h"
35
36
37namespace nest
38{
39class AbstractGeneric;
40
50template < class BaseT >
52{
53public:
54 typedef BaseT* ( *CreatorFunction )( const Dictionary& d );
55 typedef std::map< std::string, CreatorFunction > AssocMap;
56
63 BaseT* create( const std::string& name, const Dictionary& d ) const;
64
75 template < class T >
76 bool register_subtype( const std::string& name );
77
85 bool register_subtype( const std::string& name, CreatorFunction creator );
86
87private:
88 template < class T >
89 static BaseT* new_from_dict_( const Dictionary& d );
90
92};
93
94template < class BaseT >
95inline BaseT*
96GenericFactory< BaseT >::create( const std::string& name, const Dictionary& d ) const
97{
98 typename AssocMap::const_iterator i = associations_.find( name );
99 if ( i != associations_.end() )
100 {
101 return ( i->second )( d );
102 }
103 throw UndefinedName( name );
104}
105
106template < class BaseT >
107template < class T >
108inline bool
110{
111 return register_subtype( name, new_from_dict_< T > );
112}
113
114template < class BaseT >
115inline bool
116GenericFactory< BaseT >::register_subtype( const std::string& name, CreatorFunction creator )
117{
118 return associations_.insert( std::pair< std::string, CreatorFunction >( name, creator ) ).second;
119}
120
121template < class BaseT >
122template < class T >
123BaseT*
125{
126 return new T( d );
127}
128
129} // namespace nest
130
131#endif
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
Generic Factory class for objects deriving from a base class BaseT.
Definition generic_factory.h:52
BaseT * create(const std::string &name, const Dictionary &d) const
Factory function.
Definition generic_factory.h:96
AssocMap associations_
Definition generic_factory.h:91
BaseT *(* CreatorFunction)(const Dictionary &d)
Definition generic_factory.h:54
static BaseT * new_from_dict_(const Dictionary &d)
Definition generic_factory.h:124
std::map< std::string, CreatorFunction > AssocMap
Definition generic_factory.h:55
bool register_subtype(const std::string &name)
Register a new subtype.
Definition generic_factory.h:109
Definition exceptions.h:1775
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33