5#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
6#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
34template<
typename Signature,
35 typename KeyT = std::string>
38template<
typename TypeT,
57 ->
decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
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...);
103 registry_[key] = DefaultCreator<Impl>();
120 typename std::enable_if<has_proper_signature<F>(
PriorityTag<42>()),
int>::type = 0>
141 typename std::enable_if<
142 std::is_convertible<Impl, Type>::value
143 and not std::is_convertible<Impl, Creator>::value,
147 registry_[key] = [=](Args...) {
return t;};
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;
A few common exception classes.
Utilities for type computations, constraining overloads, ...
#define DUNE_THROW(E, m)
Definition exceptions.hh:218
Dune namespace.
Definition alignedallocator.hh:13
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
std::function< Type(Args...)> Creator
Definition parameterizedobject.hh:53
static constexpr auto has_proper_signature(Dune::PriorityTag< 1 >) -> decltype(std::declval< F >()(std::declval< Args >()...), std::true_type())
Definition parameterizedobject.hh:56
TypeT Type
The type of objects created by the factory.
Definition parameterizedobject.hh:49
void define(Key const &key, Impl &&t)
Registers a new type with a key.
Definition parameterizedobject.hh:145
KeyT Key
The typ of the keys.
Definition parameterizedobject.hh:46
Type create(Key const &key, Args ... args) const
Creates an object identified by a key from given parameters.
Definition parameterizedobject.hh:77
void define(Key const &key)
Registers a new type with a key.
Definition parameterizedobject.hh:101
void define(Key const &key, F &&f)
Registers a new creator with a key.
Definition parameterizedobject.hh:121
static constexpr std::false_type has_proper_signature(Dune::PriorityTag< 0 >)
Definition parameterizedobject.hh:63
bool contains(Key const &key) const
Definition parameterizedobject.hh:150
Helper class for tagging priorities.
Definition typeutilities.hh:73