dune-common 2.12-git
Loading...
Searching...
No Matches
parameterizedobject.hh
Go to the documentation of this file.
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>
58 -> decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
59 {
60 return {};
61 }
62
63 template<class F>
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,
122 void define(Key const& key, F&& f)
123 {
124 registry_[key] = f;
125 }
126
141 template<class Impl,
142 typename std::enable_if<
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
Utilities for reduction like operations on ranges.
Utilities for type computations, constraining overloads, ...
A few common exception classes.
auto transformedRangeView(R &&range, F &&f)
Create a TransformedRangeView.
Definition rangeutilities.hh:669
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
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:375
A factory class for parameterized objects.
Definition parameterizedobject.hh:37
auto keys() const
Get a list of the available keys to the object factory.
Definition parameterizedobject.hh:157
static constexpr auto has_proper_signature(Dune::PriorityTag< 1 >) -> decltype(std::declval< F >()(std::declval< Args >()...), std::true_type())
Definition parameterizedobject.hh:57
TypeT Type
The type of objects created by the factory.
Definition parameterizedobject.hh:50
void define(Key const &key, Impl &&t)
Registers a new type with a key.
Definition parameterizedobject.hh:146
KeyT Key
The typ of the keys.
Definition parameterizedobject.hh:47
Type create(Key const &key, Args ... args) const
Creates an object identified by a key from given parameters.
Definition parameterizedobject.hh:78
void define(Key const &key)
Registers a new type with a key.
Definition parameterizedobject.hh:102
void define(Key const &key, F &&f)
Registers a new creator with a key.
Definition parameterizedobject.hh:122
static constexpr std::false_type has_proper_signature(Dune::PriorityTag< 0 >)
Definition parameterizedobject.hh:64
bool contains(Key const &key) const
Definition parameterizedobject.hh:151
Helper class for tagging priorities.
Definition typeutilities.hh:73