2016-02-25 12:07:21 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
Source file: programs/Hadrons/Factory.hpp
|
2016-02-25 12:07:21 +00:00
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
Copyright (C) 2016
|
2016-02-25 12:07:21 +00:00
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution
|
|
|
|
directory.
|
|
|
|
*******************************************************************************/
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
#ifndef Hadrons_Factory_hpp_
|
|
|
|
#define Hadrons_Factory_hpp_
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
#include <Grid/Hadrons/Global.hpp>
|
2016-04-30 08:17:04 +01:00
|
|
|
|
|
|
|
BEGIN_HADRONS_NAMESPACE
|
2015-12-23 14:21:35 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2016-04-30 08:17:04 +01:00
|
|
|
* abstract factory class *
|
2015-12-23 14:21:35 +00:00
|
|
|
******************************************************************************/
|
2016-04-30 08:17:04 +01:00
|
|
|
template <typename T>
|
|
|
|
class Factory
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
public:
|
|
|
|
typedef std::function<std::unique_ptr<T>(const std::string)> Func;
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
Factory(void) = default;
|
|
|
|
// destructor
|
|
|
|
virtual ~Factory(void) = default;
|
|
|
|
// registration
|
|
|
|
void registerBuilder(const std::string type, const Func &f);
|
2016-05-03 03:31:21 +01:00
|
|
|
// get builder list
|
2016-04-30 08:17:04 +01:00
|
|
|
std::vector<std::string> getBuilderList(void) const;
|
|
|
|
// factory
|
|
|
|
std::unique_ptr<T> create(const std::string type,
|
|
|
|
const std::string name) const;
|
|
|
|
private:
|
|
|
|
std::map<std::string, Func> builder_;
|
|
|
|
};
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* template implementation *
|
|
|
|
******************************************************************************/
|
2015-12-23 14:21:35 +00:00
|
|
|
// registration ////////////////////////////////////////////////////////////////
|
2016-04-30 08:17:04 +01:00
|
|
|
template <typename T>
|
|
|
|
void Factory<T>::registerBuilder(const std::string type, const Func &f)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
builder_[type] = f;
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get module list /////////////////////////////////////////////////////////////
|
2016-04-30 08:17:04 +01:00
|
|
|
template <typename T>
|
|
|
|
std::vector<std::string> Factory<T>::getBuilderList(void) const
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> list;
|
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
for (auto &b: builder_)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
list.push_back(b.first);
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// factory /////////////////////////////////////////////////////////////////////
|
2016-04-30 08:17:04 +01:00
|
|
|
template <typename T>
|
|
|
|
std::unique_ptr<T> Factory<T>::create(const std::string type,
|
|
|
|
const std::string name) const
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
Func func;
|
2015-12-23 14:21:35 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
func = builder_.at(type);
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
2016-05-03 03:31:21 +01:00
|
|
|
catch (std::out_of_range &)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
HADRON_ERROR("object of type '" + type + "' unknown");
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(name);
|
|
|
|
}
|
2016-04-30 08:17:04 +01:00
|
|
|
|
|
|
|
END_HADRONS_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Hadrons_Factory_hpp_
|