Dune Core Modules (2.10.0)

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
14
15namespace Dune {
16
34template<typename Signature,
35 typename KeyT = std::string>
37
38template<typename TypeT,
39 typename KeyT,
40 typename... Args>
41class ParameterizedObjectFactory<TypeT(Args...), KeyT>
42{
43 public:
44
46 typedef KeyT Key;
47
49 using Type = TypeT;
50
51 protected:
52
53 using Creator = std::function<Type(Args...)>;
54
55 template<class F>
56 static constexpr auto has_proper_signature(Dune::PriorityTag<1>)
57 -> decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
58 {
59 return {};
60 }
61
62 template<class F>
63 static constexpr std::false_type has_proper_signature(Dune::PriorityTag<0>)
64 {
65 return {};
66 }
67
68 public:
69
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");
83 }
84 else return i->second(args...);
85 }
86
100 template<class Impl>
101 void define(Key const& key)
102 {
103 registry_[key] = DefaultCreator<Impl>();
104 }
105
119 template<class F,
120 typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()), int>::type = 0>
121 void define(Key const& key, F&& f)
122 {
123 registry_[key] = f;
124 }
125
140 template<class Impl,
141 typename std::enable_if<
142 std::is_convertible<Impl, Type>::value
143 and not std::is_convertible<Impl, Creator>::value,
144 int>::type = 0>
145 void define(Key const& key, Impl&& t)
146 {
147 registry_[key] = [=](Args...) { return t;};
148 }
149
150 bool contains(Key const& key) const
151 {
152 return registry_.count(key);
153 }
154
155 private:
156
157 template<class T>
158 struct Tag{};
159
160 template<class Impl>
161 struct DefaultCreator
162 {
163 template<class... T>
164 Type operator()(T&&... args) const
165 {
166 return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
167 }
168
169 template<class Target, class... T>
170 static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
171 return Impl(std::forward<T>(args)...);
172 }
173
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)...);
177 }
178
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)...);
182 }
183
184 };
185
186 typedef std::map<Key, Creator> Registry;
187 Registry registry_;
188};
189
190
191
192} // end namespace Dune
193
194#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: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
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
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  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)