Flow123d  JS_before_hm-2206-gca2a8366f
factory_impl.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file factory_impl.hh
15  * @brief
16  */
17 
18 #ifndef FACTORY_IMPL_HH_
19 #define FACTORY_IMPL_HH_
20 
21 
22 namespace Input {
23 
24 using namespace std;
25 
26 template<class Type, class... Arguments>
28 {
29  static Factory<Type, Arguments...> factory;
30  return &factory;
31 }
32 
33 
34 template<class Type, class... Arguments>
35 template<class Child>
37  auto creating_function =
38  [](Arguments... args)->std::shared_ptr<Type>
39  { return std::make_shared<Child>(args...); };
40 
41  auto func_wrapper = std::function<std::shared_ptr<Type>(Arguments...)>(creating_function);
42  Factory<Type, Arguments...>::instance()->factory_registry_[class_name] = func_wrapper;
43  //DebugOut().fmt("Adding name: {} of class {} to factory of class {}.\n", class_name.c_str(), typeid(Child).name(), typeid(Type).name());
44  return 0;
45 }
46 
47 
48 template<class Type, class... Arguments>
49 const shared_ptr<Type> Factory<Type, Arguments...>::create(string name, Arguments... arguments) const
50 {
51  // find name in the registry and call factory method.
52  auto it = factory_registry_.find(name);
53  if(it != factory_registry_.end()) {
54  auto factory_function = boost::any_cast<std::function< shared_ptr<Type>(Arguments...)> > (it->second);
55  return factory_function(arguments...);
56  } else {
57  THROW( ExcNotRegistredClass() << EI_KeyName(name) << EI_TypeName(typeid(Type).name()) );
58  return nullptr;
59  }
60 }
61 
62 
63 template <class ChildType, class... Arguments>
64 int register_class(string class_name)
65 {
66  return Input::Factory<typename ChildType::FactoryBaseType, Arguments...>::template register_class< ChildType >(class_name);
67 }
68 
69 
70 
71 } // namespace Input
72 
73 #endif /* FACTORY_IMPL_HH_ */
Input
Abstract linear system class.
Definition: balance.hh:40
Input::register_class
int register_class(string class_name)
Function allows simplified call of registering class to factory.
Definition: factory_impl.hh:64
Input::Factory
This class implements more general factory mechanism to construct classes.
Definition: factory.hh:132
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Input::Factory::register_class
static int register_class(string class_name)
Register lambda function that calls default constructor of Type.
Definition: factory_impl.hh:36
Input::Type
Definition: balance.hh:41
std
Definition: doxy_dummy_defs.hh:5
Input::Factory::factory_registry_
map< string, boost::any > factory_registry_
the registry of factory functions
Definition: factory.hh:153
Input::Factory::instance
static Factory * instance()
Get the single instance of the factory.
Definition: factory_impl.hh:27
Input::Factory::create
const shared_ptr< Type > create(string name, Arguments... arguments) const
create an instance of a registered class
Definition: factory_impl.hh:49