Loading...
Searching...
No Matches
parameterizedobject.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_PARAMETERIZEDOBJECT_HH
#define DUNE_COMMON_PARAMETERIZEDOBJECT_HH
#include <functional>
#include <map>
#include <memory>
#include <dune/common/exceptions.hh>
#include <dune/common/typeutilities.hh>
namespace Dune {
template<typename Signature,
typename KeyT = std::string>
class ParameterizedObjectFactory;
template<typename TypeT,
typename KeyT,
typename... Args>
class ParameterizedObjectFactory<TypeT(Args...), KeyT>
{
public:
typedef KeyT Key;
using Type = TypeT;
protected:
using Creator = std::function<Type(Args...)>;
template<class F>
static constexpr auto has_proper_signature(Dune::PriorityTag<1>)
-> decltype( std::declval<F>()(std::declval<Args>()...), std::true_type())
{
return {};
}
template<class F>
static constexpr std::false_type has_proper_signature(Dune::PriorityTag<0>)
{
return {};
}
public:
Type create(Key const& key, Args ... args) const {
typename Registry::const_iterator i = registry_.find(key);
if (i == registry_.end()) {
DUNE_THROW(Dune::InvalidStateException,
"ParametrizedObjectFactory: key ``" <<
key << "'' not registered");
}
else return i->second(args...);
}
template<class Impl>
void define(Key const& key)
{
registry_[key] = DefaultCreator<Impl>();
}
template<class F,
typename std::enable_if<has_proper_signature<F>(PriorityTag<42>()), int>::type = 0>
void define(Key const& key, F&& f)
{
registry_[key] = f;
}
template<class Impl,
typename std::enable_if<
std::is_convertible<Impl, Type>::value
and not std::is_convertible<Impl, Creator>::value,
int>::type = 0>
void define(Key const& key, Impl&& t)
{
registry_[key] = [=](Args...) { return t;};
}
bool contains(Key const& key) const
{
return registry_.count(key);
}
private:
template<class T>
struct Tag{};
template<class Impl>
struct DefaultCreator
{
template<class... T>
Type operator()(T&&... args) const
{
return DefaultCreator::create(Tag<Type>(), PriorityTag<42>(), std::forward<T>(args)...);
}
template<class Target, class... T>
static Type create(Tag<Target>, PriorityTag<1>, T&& ... args) {
return Impl(std::forward<T>(args)...);
}
template<class Target, class... T>
static Type create(Tag<std::unique_ptr<Target>>, PriorityTag<2>, T&& ... args) {
return std::make_unique<Impl>(std::forward<T>(args)...);
}
template<class Target, class... T>
static Type create(Tag<std::shared_ptr<Target>>, PriorityTag<3>, T&& ... args) {
return std::make_shared<Impl>(std::forward<T>(args)...);
}
};
typedef std::map<Key, Creator> Registry;
Registry registry_;
};
} // end namespace Dune
#endif // DUNE_COMMON_PARAMETERIZEDOBJECT_HH
typeutilities.hh
Utilities for type computations, constraining overloads, ...
exceptions.hh
A few common exception classes.
DUNE_THROW
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::InvalidStateException
Default exception if a function was called while the object is not in a valid state for that function...
Definition exceptions.hh:375
Dune::ParameterizedObjectFactory
A factory class for parameterized objects.
Definition parameterizedobject.hh:36
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::Creator
std::function< Type(Args...)> Creator
Definition parameterizedobject.hh:53
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::has_proper_signature
static constexpr auto has_proper_signature(Dune::PriorityTag< 1 >) -> decltype(std::declval< F >()(std::declval< Args >()...), std::true_type())
Definition parameterizedobject.hh:56
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::Type
TypeT Type
The type of objects created by the factory.
Definition parameterizedobject.hh:49
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::define
void define(Key const &key, Impl &&t)
Registers a new type with a key.
Definition parameterizedobject.hh:145
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::Key
KeyT Key
The typ of the keys.
Definition parameterizedobject.hh:46
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::create
Type create(Key const &key, Args ... args) const
Creates an object identified by a key from given parameters.
Definition parameterizedobject.hh:77
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::define
void define(Key const &key)
Registers a new type with a key.
Definition parameterizedobject.hh:101
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::define
void define(Key const &key, F &&f)
Registers a new creator with a key.
Definition parameterizedobject.hh:121
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::has_proper_signature
static constexpr std::false_type has_proper_signature(Dune::PriorityTag< 0 >)
Definition parameterizedobject.hh:63
Dune::ParameterizedObjectFactory< TypeT(Args...), KeyT >::contains
bool contains(Key const &key) const
Definition parameterizedobject.hh:150
Dune::type
Dune::PriorityTag
Helper class for tagging priorities.
Definition typeutilities.hh:73