3#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
4#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
10#include <dune/common/std/memory.hh>
32template<
typename Signature,
33 typename KeyT = std::string>
36template<
typename TypeT,
51 using Creator = std::function<Type(Args...)>;
55 ->
decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
75 Type create(Key
const& key, Args ... args) {
76 typename Registry::const_iterator i = registry_.find(key);
77 if (i == registry_.end()) {
79 "ParametrizedObjectFactory: key ``" <<
80 key <<
"'' not registered");
82 else return i->second(args...);
99 void define(Key
const& key)
101 registry_[key] = DefaultCreator<Impl>();
118 typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()),
int>::type = 0>
119 void define(Key
const& key, F&& f)
139 typename std::enable_if<
140 std::is_convertible<Impl, Type>::value
141 and not std::is_convertible<Impl, Creator>::value,
143 void define(Key
const& key, Impl&& t)
145 registry_[key] = [=](Args...) {
return t;};
154 struct DefaultCreator
157 Type operator()(T&&... args)
const
159 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
162 template<
class Target,
class... T>
163 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
164 return Impl(std::forward<T>(args)...);
167 template<
class Target,
class... T>
168 static Type create(Tag<std::unique_ptr<Target>>, PriorityTag<2>, T&& ... args) {
169 return Dune::Std::make_unique<Impl>(std::forward<T>(args)...);
172 template<
class Target,
class... T>
173 static Type create(Tag<std::shared_ptr<Target>>, PriorityTag<3>, T&& ... args) {
174 return std::make_shared<Impl>(std::forward<T>(args)...);
179 typedef std::map<Key, Creator> Registry;
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
A factory class for parameterized objects.
Definition: parameterizedobject.hh:34
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:10
Helper class for tagging priorities.
Definition: typeutilities.hh:83
Helper class for tagging priorities.
Definition: typeutilities.hh:71
Utilities for type computations, constraining overloads, ...