aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/object_factory/object_factory.h
blob: a5508e0afe569d8f07bbf0a332b2bd226473976e (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once

#include <util/system/guard.h>
#include <util/system/rwlock.h>
#include <util/generic/map.h>
#include <util/generic/set.h>
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>

namespace NObjectFactory {
    template <class TProduct, class... TArgs>
    class IFactoryObjectCreator {
    public:
        virtual TProduct* Create(TArgs... args) const = 0;
        virtual ~IFactoryObjectCreator() {
        }
    };

    template <class TProduct>
    class IFactoryObjectCreator<TProduct, void> {
    public:
        virtual TProduct* Create(void) const = 0;
        virtual ~IFactoryObjectCreator() {
        }
    };

#define FACTORY_OBJECT_NAME(Name)              \
    static TString GetTypeName() {             \
        return #Name;                          \
    }                                          \
    virtual TString GetType() const override { \
        return #Name;                          \
    }

    template <class TBaseProduct, class TDerivedProduct, class... TArgs>
    class TFactoryObjectCreator: public IFactoryObjectCreator<TBaseProduct, TArgs...> {
        TDerivedProduct* Create(TArgs... args) const override {
            return new TDerivedProduct(std::forward<TArgs>(args)...);
        }
    };

    template <class TBaseProduct, class TDerivedProduct>
    class TFactoryObjectCreator<TBaseProduct, TDerivedProduct, void>: public IFactoryObjectCreator<TBaseProduct, void> {
        TDerivedProduct* Create() const override {
            return new TDerivedProduct();
        }
    };

    template <class P, class K, class... TArgs>
    class IObjectFactory {
    public:
        typedef P TProduct;
        typedef K TKey;

    public:
        template <class TDerivedProduct>
        void Register(const TKey& key, IFactoryObjectCreator<TProduct, TArgs...>* creator) {
            if (!creator)
                ythrow yexception() << "Please specify non-null creator for " << key;

            TWriteGuard guard(CreatorsLock);
            if (!Creators.insert(typename ICreators::value_type(key, creator)).second)
                ythrow yexception() << "Product with key " << key << " already registered";
        }

        template <class TDerivedProduct>
        void Register(const TKey& key) {
            Register<TDerivedProduct>(key, new TFactoryObjectCreator<TProduct, TDerivedProduct, TArgs...>);
        }

        void GetKeys(TSet<TKey>& keys) const {
            TReadGuard guard(CreatorsLock);
            keys.clear();
            for (typename ICreators::const_iterator i = Creators.begin(), e = Creators.end(); i != e; ++i) {
                keys.insert(i->first);
            }
        }

    protected:
        IFactoryObjectCreator<TProduct, TArgs...>* GetCreator(const TKey& key) const {
            TReadGuard guard(CreatorsLock);
            typename ICreators::const_iterator i = Creators.find(key);
            return i == Creators.end() ? nullptr : i->second.Get();
        }

        bool HasImpl(const TKey& key) const {
            TReadGuard guard(CreatorsLock);
            return Creators.find(key) != Creators.end();
        }

    private:
        typedef TSimpleSharedPtr<IFactoryObjectCreator<TProduct, TArgs...>> ICreatorPtr;
        typedef TMap<TKey, ICreatorPtr> ICreators;
        ICreators Creators;
        TRWMutex CreatorsLock;
    };

    template <class TProduct, class TKey>
    class TObjectFactory: public IObjectFactory<TProduct, TKey, void> {
    public:
        TProduct* Create(const TKey& key) const {
            IFactoryObjectCreator<TProduct, void>* creator = IObjectFactory<TProduct, TKey, void>::GetCreator(key);
            return creator == nullptr ? nullptr : creator->Create();
        }

        static TString KeysDebugString() {
            TSet<TString> keys;
            Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys);
            TString keysStr;
            for (auto&& k : keys) {
                keysStr += k + " ";
            }
            return keysStr;
        }

        static TProduct* Construct(const TKey& key, const TKey& defKey) {
            TProduct* result = Singleton<TObjectFactory<TProduct, TKey>>()->Create(key);
            if (!result && !!defKey) {
                result = Singleton<TObjectFactory<TProduct, TKey>>()->Create(defKey);
            }
            return result;
        }

        static TProduct* Construct(const TKey& key) {
            TProduct* result = Singleton<TObjectFactory<TProduct, TKey>>()->Create(key);
            return result;
        }

        static THolder<TProduct> VerifiedConstruct(const TKey& key) {
            auto result = MakeHolder(key);
            Y_VERIFY(result, "Construct by factory failed");
            return result;
        }

        template<class... Args>
        static THolder<TProduct> MakeHolder(Args&&... args) {
            return THolder<TProduct>(Construct(std::forward<Args>(args)...)); 
        }

        static bool Has(const TKey& key) {
            return Singleton<TObjectFactory<TProduct, TKey>>()->HasImpl(key);
        }

        static void GetRegisteredKeys(TSet<TKey>& keys) {
            return Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys);
        }

        static TSet<TKey> GetRegisteredKeys() {
            TSet<TKey> keys;
            Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys);
            return keys;
        }

        template <class TDerivedProduct>
        static TSet<TKey> GetRegisteredKeys() {
            TSet<TKey> registeredKeys(GetRegisteredKeys());
            TSet<TKey> fileredKeys;
            std::copy_if(registeredKeys.begin(), registeredKeys.end(), std::inserter(fileredKeys, fileredKeys.end()), [](const TKey& key) {
                THolder<TProduct> objectHolder(Construct(key));
                return !!dynamic_cast<const TDerivedProduct*>(objectHolder.Get());
            });
            return fileredKeys;
        }

        template <class Product>
        class TRegistrator {
        public:
            TRegistrator(const TKey& key, IFactoryObjectCreator<TProduct, void>* creator) {
                Singleton<TObjectFactory<TProduct, TKey>>()->template Register<Product>(key, creator);
            }

            TRegistrator(const TKey& key) {
                Singleton<TObjectFactory<TProduct, TKey>>()->template Register<Product>(key);
            }

            TRegistrator()
                : TRegistrator(Product::GetTypeName())
            {
            }
        };
    };

    template <class TProduct, class TKey, class... TArgs>
    class TParametrizedObjectFactory: public IObjectFactory<TProduct, TKey, TArgs...> {
    public:
        TProduct* Create(const TKey& key, TArgs... args) const {
            IFactoryObjectCreator<TProduct, TArgs...>* creator = IObjectFactory<TProduct, TKey, TArgs...>::GetCreator(key);
            return creator == nullptr ? nullptr : creator->Create(std::forward<TArgs>(args)...);
        }

        static bool Has(const TKey& key) {
            return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->HasImpl(key);
        }

        static TProduct* Construct(const TKey& key, TArgs... args) {
            return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->Create(key, std::forward<TArgs>(args)...);
        }

        template <class... Args>
        static THolder<TProduct> VerifiedConstruct(Args&&... args) {
            auto result = MakeHolder(std::forward<Args>(args)...);
            Y_VERIFY(result, "Construct by factory failed");
            return result;
        }

        template<class... Args>
        static THolder<TProduct> MakeHolder(Args&&... args) {
            return THolder<TProduct>(Construct(std::forward<Args>(args)...));
        }

        static void GetRegisteredKeys(TSet<TKey>& keys) {
            return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->GetKeys(keys);
        }

        static TSet<TKey> GetRegisteredKeys() {
            TSet<TKey> keys;
            Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->GetKeys(keys);
            return keys;
        }

        template <class Product>
        class TRegistrator {
        public:
            TRegistrator(const TKey& key, IFactoryObjectCreator<TProduct, TArgs...>* creator) {
                Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->template Register<Product>(key, creator);
            }

            TRegistrator(const TKey& key) {
                Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->template Register<Product>(key);
            }

            TRegistrator()
                : TRegistrator(Product::GetTypeName())
            {
            }

            TString GetName() const {
                return Product::GetTypeName();
            }
        };
    };

}