5#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
6#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
34template<
typename Signature,
35 typename KeyT = std::string>
38template<
typename TypeT,
53 using Creator = std::function<Type(Args...)>;
57 ->
decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
77 Type create(Key
const& key, Args ... args)
const {
78 typename Registry::const_iterator i = registry_.find(key);
79 if (i == registry_.end()) {
81 "ParametrizedObjectFactory: key ``" <<
82 key <<
"'' not registered");
84 else return i->second(args...);
101 void define(Key
const& key)
103 registry_[key] = DefaultCreator<Impl>();
120 typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()),
int>::type = 0>
121 void define(Key
const& key, F&& f)
141 typename std::enable_if<
142 std::is_convertible<Impl, Type>::value
143 and not std::is_convertible<Impl, Creator>::value,
145 void define(Key
const& key, Impl&& t)
147 registry_[key] = [=](Args...) {
return t;};
150 bool contains(Key
const& key)
const
152 return registry_.count(key);
161 struct DefaultCreator
164 Type operator()(T&&... args)
const
166 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
169 template<
class Target,
class... T>
170 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
171 return Impl(std::forward<T>(args)...);
174 template<
class Target,
class... T>
175 static Type create(Tag<std::unique_ptr<Target>>, PriorityTag<2>, T&& ... args) {
176 return std::make_unique<Impl>(std::forward<T>(args)...);
179 template<
class Target,
class... T>
180 static Type create(Tag<std::shared_ptr<Target>>, PriorityTag<3>, T&& ... args) {
181 return std::make_shared<Impl>(std::forward<T>(args)...);
186 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:281
A factory class for parameterized objects.
Definition: parameterizedobject.hh:36
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
Helper class for tagging priorities.
Definition: typeutilities.hh:87
Helper class for tagging priorities.
Definition: typeutilities.hh:73
Utilities for type computations, constraining overloads, ...