3#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
4#define DUNE_COMMON_PARAMETERIZEDOBJECT_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)
const {
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;};
148 bool contains(Key
const& key)
const
150 return registry_.count(key);
159 struct DefaultCreator
162 Type operator()(T&&... args)
const
164 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
167 template<
class Target,
class... T>
168 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
169 return Impl(std::forward<T>(args)...);
172 template<
class Target,
class... T>
173 static Type create(Tag<std::unique_ptr<Target>>, PriorityTag<2>, T&& ... args) {
174 return std::make_unique<Impl>(std::forward<T>(args)...);
177 template<
class Target,
class... T>
178 static Type create(Tag<std::shared_ptr<Target>>, PriorityTag<3>, T&& ... args) {
179 return std::make_shared<Impl>(std::forward<T>(args)...);
184 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:11
Helper class for tagging priorities.
Definition: typeutilities.hh:85
Helper class for tagging priorities.
Definition: typeutilities.hh:71
Utilities for type computations, constraining overloads, ...