Grid 0.7.0
Factory.h
Go to the documentation of this file.
1/*************************************************************************************
2Grid physics library, www.github.com/paboyle/Grid
3Source file: Factory.h
4
5Copyright (C) 2015
6Copyright (C) 2016
7
8Author: Antonin Portelli <antonin.portelli@me.com>
9Author: Guido Cossu <guido.cossu@ed.ac.uk>
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2 of the License, or
14(at your option) any later version.
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19You should have received a copy of the GNU General Public License along
20with this program; if not, write to the Free Software Foundation, Inc.,
2151 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22See the full license in the file "LICENSE" in the top level distribution directory
23*************************************************************************************/
24/* END LEGAL */
25
26#ifndef Factory_hpp_
27#define Factory_hpp_
28
29
30namespace Grid{
31
32
33
34
35/******************************************************************************
36 * abstract factory class *
37 ******************************************************************************/
38template <typename T, typename CreatorInput>
40{
41public:
42 typedef std::function< std::unique_ptr<T> (const CreatorInput&) > Func;
43
44 // constructor
45 Factory(void) = default;
46 // destructor
47 virtual ~Factory(void) = default;
48 // registration
49 void registerBuilder(const std::string type, const Func &f);
50 // get builder list
51 std::vector<std::string> getBuilderList(void) const;
52 // factory
53 std::unique_ptr<T> create(const std::string type,
54 const CreatorInput& input) const;
55private:
56 std::map<std::string, Func> builder_;
57 virtual std::string obj_type() const = 0;
58};
59
60/******************************************************************************
61 * template implementation *
62 ******************************************************************************/
63// registration ////////////////////////////////////////////////////////////////
64template <typename T, typename CreatorInput>
65void Factory<T, CreatorInput>::registerBuilder(const std::string type, const Func &f)
66{
67 builder_[type] = f;
68}
69
70// get module list /////////////////////////////////////////////////////////////
71template <typename T, typename CreatorInput>
72std::vector<std::string> Factory<T, CreatorInput>::getBuilderList(void) const
73{
74 std::vector<std::string> list;
75
76 for (auto &b: builder_)
77 {
78 list.push_back(b.first);
79 }
80
81 return list;
82}
83
84// factory /////////////////////////////////////////////////////////////////////
85template <typename T, typename CreatorInput>
86std::unique_ptr<T> Factory<T, CreatorInput>::create(const std::string type,
87 const CreatorInput& input) const
88{
89 Func func;
90
91 std::cout << GridLogDebug << "Creating object of type "<< type << std::endl;
92 try
93 {
94 func = builder_.at(type);
95 }
96 catch (std::out_of_range &)
97 {
98 //HADRONS_ERROR("object of type '" + type + "' unknown");
99 std::cout << GridLogError << "Error" << std::endl;
100 std::cout << GridLogError << obj_type() << " object of name [" << type << "] unknown" << std::endl;
101 exit(1);
102 }
103
104 return func(input);
105}
106
107}
108
109#endif // Factory_hpp_
GridLogger GridLogError(1, "Error", GridLogColours, "RED")
GridLogger GridLogDebug(1, "Debug", GridLogColours, "PURPLE")
std::vector< std::string > getBuilderList(void) const
Definition Factory.h:72
std::map< std::string, Func > builder_
Definition Factory.h:56
Factory(void)=default
virtual std::string obj_type() const =0
virtual ~Factory(void)=default
void registerBuilder(const std::string type, const Func &f)
Definition Factory.h:65
std::function< std::unique_ptr< T >(const CreatorInput &) > Func
Definition Factory.h:42
std::unique_ptr< T > create(const std::string type, const CreatorInput &input) const
Definition Factory.h:86