File: parameterizedobject.hh

package info (click to toggle)
dune-common 2.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: cpp: 52,256; python: 3,979; sh: 1,658; makefile: 17
file content (194 lines) | stat: -rw-r--r-- 6,480 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- 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 {

/**
 * @brief A factory class for parameterized objects.
 *
 * It allows the construction of objects adhering to a certain interface that
 * might be constructed quite differently for one another.
 *
 * The Signature parameter defined the "virtual" constructor signature
 * in the form of Interface(Args...), where Interface is the type of
 * the (abstract) interface class and Args... is the set of
 * constructor parameters.
 *
 * Each type constructed by this factory is identified by a different key. This class
 * allows for easy registration of type with new keys.
 *
 * @tparam Signature Signature of the "virtual" constructor call in the form for Interface(Args...). For default constructors one can omit the ()-brackets.
 * @tparam KeyT The type of the objects that are used as keys in the lookup [DEFAULT: std::string].
 */
template<typename Signature,
         typename KeyT = std::string>
class ParameterizedObjectFactory;

template<typename TypeT,
         typename KeyT,
         typename... Args>
class ParameterizedObjectFactory<TypeT(Args...), KeyT>
{
    public:

        /** @brief The typ of the keys. */
        typedef KeyT Key;

        /** @brief The type of objects created by the factory. */
        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:

        /**
         * @brief Creates an object identified by a key from given parameters
         *
         * @param key The key the object is registered with @see define.
         * @param args The parameters used for the construction.
         * @return The object wrapped as Type
         */
        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...);
        }

        /**
         * @brief Registers a new type with a key.
         *
         * After registration objects of this type can be constructed with the
         * specified key using a matching default creation function. If Type
         * is a unique_ptr or shared_ptr, the object is created via make_unique
         * or make_shared, respectively. Otherwise a constructor of Impl
         * is called.
         *
         * @tparam Impl The type of objects to create.
         *
         * @param key The key associated with this type.
         */
        template<class Impl>
        void define(Key const& key)
        {
            registry_[key] = DefaultCreator<Impl>();
        }

        /**
         * @brief Registers a new creator with a key.
         *
         * After registration objects can be constructed using
         * the given creator function.
         *
         * @tparam F Type of creator function. This must be callable with Args... .
         *
         * @param key The key associated with this type.
         * @param f Function for creation of objects of type Impl
         *
         * \todo Replace has_proper_signature by concept check
         */
        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;
        }

        /**
         * @brief Registers a new type with a key.
         *
         * After registration objects of this type can be created.
         * This method will store a copy of the given object and
         * create will hand out a copy to this.
         *
         * @tparam Impl The type of objects to create.
         *
         * @param key The key associated with this type.
         * @param t reference object, "create" will call the copy-constructor
         *
         * note, this does not work fundamental types
         */
        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