Go to the documentation of this file.
3 #ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
4 #define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
32 template<
typename Signature,
33 typename KeyT = std::string>
36 template<
typename TypeT,
55 -> decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
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...);
101 registry_[key] = DefaultCreator<Impl>();
118 typename std::enable_if<has_proper_signature<F>(
PriorityTag<42>()),
int>::type = 0>
139 typename std::enable_if<
140 std::is_convertible<Impl, Type>::value
141 and not std::is_convertible<Impl, Creator>::value,
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;
187 #endif // DUNE_COMMON_PARAMETERIZEDOBJECT_HH
void define(Key const &key, Impl &&t)
Registers a new type with a key.
Definition: parameterizedobject.hh:143
A few common exception classes.
Dune namespace.
Definition: alignedallocator.hh:9
TypeT Type
The type of objects created by the factory.
Definition: parameterizedobject.hh:47
KeyT Key
The typ of the keys.
Definition: parameterizedobject.hh:44
Type create(Key const &key, Args ... args)
Creates an object identified by a key from given parameters.
Definition: parameterizedobject.hh:75
static constexpr std::false_type has_proper_signature(Dune::PriorityTag< 0 >)
Definition: parameterizedobject.hh:61
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
Helper class for tagging priorities.
Definition: typeutilities.hh:82
void define(Key const &key)
Registers a new type with a key.
Definition: parameterizedobject.hh:99
Helper class for tagging priorities.
Definition: typeutilities.hh:70
A factory class for parameterized objects.
Definition: parameterizedobject.hh:34
Utilities for type computations, constraining overloads, ...
static constexpr auto has_proper_signature(Dune::PriorityTag< 1 >) -> decltype(std::declval< F >()(std::declval< Args >()...), std::true_type())
Definition: parameterizedobject.hh:54
void define(Key const &key, F &&f)
Registers a new creator with a key.
Definition: parameterizedobject.hh:119
std::function< Type(Args...)> Creator
Definition: parameterizedobject.hh:51
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216