Dune Core Modules (unstable)

parameterizedobject.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
6#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
7
8#include <functional>
9#include <map>
10#include <memory>
11
15
16namespace Dune {
17
35template<typename Signature,
36 typename KeyT = std::string>
38
39template<typename TypeT,
40 typename KeyT,
41 typename... Args>
42class ParameterizedObjectFactory<TypeT(Args...), KeyT>
43{
44 public:
45
47 typedef KeyT Key;
48
50 using Type = TypeT;
51
52 protected:
53
54 using Creator = std::function<Type(Args...)>;
55
56 template<class F>
57 static constexpr auto has_proper_signature(Dune::PriorityTag<1>)
58 -> decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
59 {
60 return {};
61 }
62
63 template<class F>
64 static constexpr std::false_type has_proper_signature(Dune::PriorityTag<0>)
65 {
66 return {};
67 }
68
69 public:
70
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");
84 }
85 else return i->second(args...);
86 }
87
101 template<class Impl>
102 void define(Key const& key)
103 {
104 registry_[key] = DefaultCreator<Impl>();
105 }
106
120 template<class F,
121 typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()), int>::type = 0>
122 void define(Key const& key, F&& f)
123 {
124 registry_[key] = f;
125 }
126
141 template<class Impl,
142 typename std::enable_if<
143 std::is_convertible<Impl, Type>::value
144 and not std::is_convertible<Impl, Creator>::value,
145 int>::type = 0>
146 void define(Key const& key, Impl&& t)
147 {
148 registry_[key] = [=](Args...) { return t;};
149 }
150
151 bool contains(Key const& key) const
152 {
153 return registry_.count(key);
154 }
155
157 auto keys() const {
158 return transformedRangeView(registry_, [](const auto & entry) { return entry.first; } );
159 }
160
161 private:
162
163 template<class T>
164 struct Tag{};
165
166 template<class Impl>
167 struct DefaultCreator
168 {
169 template<class... T>
170 Type operator()(T&&... args) const
171 {
172 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
173 }
174
175 template<class Target, class... T>
176 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
177 return Impl(std::forward<T>(args)...);
178 }
179
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)...);
183 }
184
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)...);
188 }
189
190 };
191
192 typedef std::map<Key, Creator> Registry;
193 Registry registry_;
194};
195
196
197
198} // end namespace Dune
199
200#endif // DUNE_COMMON_PARAMETERIZEDOBJECT_HH
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, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:46, 2026)