5#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
6#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
35template<
typename Signature,
36 typename KeyT = std::string>
39template<
typename TypeT,
54 using Creator = std::function<Type(Args...)>;
58 ->
decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
78 Type create(Key
const& key, Args ... args)
const {
79 typename Registry::const_iterator i = registry_.find(key);
80 if (i == registry_.end()) {
82 "ParametrizedObjectFactory: key ``" <<
83 key <<
"'' not registered");
85 else return i->second(args...);
102 void define(Key
const& key)
104 registry_[key] = DefaultCreator<Impl>();
121 typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()),
int>::type = 0>
122 void define(Key
const& key, F&& f)
142 typename std::enable_if<
143 std::is_convertible<Impl, Type>::value
144 and not std::is_convertible<Impl, Creator>::value,
146 void define(Key
const& key, Impl&& t)
148 registry_[key] = [=](Args...) {
return t;};
153 return registry_.count(key);
167 struct DefaultCreator
170 Type operator()(T&&... args)
const
172 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
175 template<
class Target,
class... T>
176 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
177 return Impl(std::forward<T>(args)...);
180 template<
class Target,
class... T>
181 static Type create(Tag<std::unique_ptr<Target>>, PriorityTag<2>, T&& ... args) {
182 return std::make_unique<Impl>(std::forward<T>(args)...);
185 template<
class Target,
class... T>
186 static Type create(Tag<std::shared_ptr<Target>>, PriorityTag<3>, T&& ... args) {
187 return std::make_shared<Impl>(std::forward<T>(args)...);
192 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:375
A factory class for parameterized objects.
Definition: parameterizedobject.hh:37
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
auto transformedRangeView(R &&range, F &&f)
Create a TransformedRangeView.
Definition: rangeutilities.hh:664
Dune namespace
Definition: alignedallocator.hh:13
constexpr std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137
Utilities for reduction like operations on ranges.
Helper class for tagging priorities.
Definition: typeutilities.hh:87
Helper class for tagging priorities.
Definition: typeutilities.hh:73
Utilities for type computations, constraining overloads, ...