Vlasiator
ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
object_factory.h
Go to the documentation of this file.
1
/*
2
* This file is part of Vlasiator.
3
* Copyright 2010-2016 Finnish Meteorological Institute
4
*
5
* For details of usage, see the COPYING file and read the "Rules of the Road"
6
* at http://www.physics.helsinki.fi/vlasiator/
7
*
8
* This program 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
* This program 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 along
19
* with this program; if not, write to the Free Software Foundation, Inc.,
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
*/
22
23
#ifndef OBJECT_FACTORY_H
24
#define OBJECT_FACTORY_H
25
26
#include <iostream>
27
#include <map>
28
29
#include "
definitions.h
"
30
35
template
<
typename
PRODUCT>
36
class
ObjectFactory
{
37
public
:
38
39
PRODUCT*
create
(
const
std::string& name)
const
;
40
bool
add
(
const
std::string& name,PRODUCT* (*maker)());
41
size_t
size
()
const
;
42
43
private
:
44
45
// Here the mysterious "PRODUCT* (*)()" is just a function pointer.
46
// If it had a name 'maker', it could be expanded as
47
// PRODUCT* (*maker)()
48
// In other words, it is a pointer to a function that takes no arguments,
49
// and that returns a pointer to PRODUCT.
50
std::map<std::string,PRODUCT* (*)() >
manufacturers
;
51
};
52
57
template
<
typename
PRODUCT>
inline
58
PRODUCT*
ObjectFactory<PRODUCT>::create
(
const
std::string& name)
const
{
59
typename
std::map<std::string,PRODUCT* (*)()>::const_iterator it =
manufacturers
.find(name);
60
if
(it ==
manufacturers
.end()) {
61
return
NULL;
62
}
63
return
(*it->second)();
64
}
65
71
template
<
typename
PRODUCT>
inline
72
bool
ObjectFactory<PRODUCT>::add
(
const
std::string& name,PRODUCT* (*maker)()) {
73
// The insert returns a pair<iterator,bool>, where the boolean value is 'true'
74
// if the maker function was inserted to the map. Skip the pair creation
75
// and just return the boolean.
76
return
manufacturers
.insert(make_pair(name,maker)).second;
77
}
78
79
template
<
typename
PRODUCT>
inline
80
size_t
ObjectFactory<PRODUCT>::size
()
const
{
81
return
this->
manufacturers
.size();
82
}
83
84
#endif
ObjectFactory
Definition
object_factory.h:36
ObjectFactory::size
size_t size() const
Definition
object_factory.h:80
ObjectFactory::manufacturers
std::map< std::string, PRODUCT *(*)() > manufacturers
Definition
object_factory.h:50
ObjectFactory::add
bool add(const std::string &name, PRODUCT *(*maker)())
Definition
object_factory.h:72
ObjectFactory::create
PRODUCT * create(const std::string &name) const
Definition
object_factory.h:58
definitions.h
object_factory.h
Generated on
for Vlasiator by
1.16.1