diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/object_factory | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/object_factory')
-rw-r--r-- | library/cpp/object_factory/object_factory.h | 268 | ||||
-rw-r--r-- | library/cpp/object_factory/object_factory_ut.cpp | 24 |
2 files changed, 146 insertions, 146 deletions
diff --git a/library/cpp/object_factory/object_factory.h b/library/cpp/object_factory/object_factory.h index 96cc11bcfd..9a91fbee8e 100644 --- a/library/cpp/object_factory/object_factory.h +++ b/library/cpp/object_factory/object_factory.h @@ -4,121 +4,121 @@ #include <util/system/rwlock.h> #include <util/generic/map.h> #include <util/generic/set.h> -#include <util/generic/singleton.h> +#include <util/generic/singleton.h> #include <util/generic/yexception.h> namespace NObjectFactory { - template <class TProduct, class... TArgs> - class IFactoryObjectCreator { - public: + 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...> { + 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> + } + }; + + 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) + 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"; - } + 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; - } + 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; + 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) { @@ -137,13 +137,13 @@ namespace NObjectFactory { return THolder<TProduct>(Construct(std::forward<Args>(args)...)); } - static bool Has(const TKey& key) { - return Singleton<TObjectFactory<TProduct, TKey>>()->HasImpl(key); + 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); - } + return Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys); + } static TSet<TKey> GetRegisteredKeys() { TSet<TKey> keys; @@ -162,39 +162,39 @@ namespace NObjectFactory { return fileredKeys; } - template <class Product> - class TRegistrator { - public: + template <class Product> + class TRegistrator { + public: TRegistrator(const TKey& key, IFactoryObjectCreator<TProduct, void>* creator) { - Singleton<TObjectFactory<TProduct, TKey>>()->template Register<Product>(key, 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()) - { - } - }; - }; + TRegistrator() + : TRegistrator(Product::GetTypeName()) + { + } + }; + }; - template <class TProduct, class TKey, class... TArgs> - class TParametrizedObjectFactory: public IObjectFactory<TProduct, TKey, TArgs...> { + 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); + 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 bool Has(const TKey& key) { + return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->HasImpl(key); } - static TProduct* Construct(const TKey& key, TArgs... args) { + 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) { @@ -208,9 +208,9 @@ namespace NObjectFactory { return THolder<TProduct>(Construct(std::forward<Args>(args)...)); } - static void GetRegisteredKeys(TSet<TKey>& keys) { - return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->GetKeys(keys); - } + static void GetRegisteredKeys(TSet<TKey>& keys) { + return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->GetKeys(keys); + } static TSet<TKey> GetRegisteredKeys() { TSet<TKey> keys; @@ -218,26 +218,26 @@ namespace NObjectFactory { return keys; } - template <class Product> - class TRegistrator { - public: + template <class Product> + class TRegistrator { + public: TRegistrator(const TKey& key, IFactoryObjectCreator<TProduct, TArgs...>* creator) { - Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->template Register<Product>(key, 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()) - { - } + TRegistrator() + : TRegistrator(Product::GetTypeName()) + { + } TString GetName() const { return Product::GetTypeName(); } - }; + }; }; } diff --git a/library/cpp/object_factory/object_factory_ut.cpp b/library/cpp/object_factory/object_factory_ut.cpp index 06fb0739ff..16edb03e3b 100644 --- a/library/cpp/object_factory/object_factory_ut.cpp +++ b/library/cpp/object_factory/object_factory_ut.cpp @@ -14,44 +14,44 @@ struct TArgument { class ICommonInterface { public: - virtual ~ICommonInterface() { - } + virtual ~ICommonInterface() { + } virtual TString GetValue() const = 0; }; -class TDirectOrder: public ICommonInterface { +class TDirectOrder: public ICommonInterface { public: TDirectOrder(const TString& provider, float factor, TArgument& argument) : Provider(provider) , Factor(factor) , Argument(argument) - { - } + { + } TString GetValue() const override { return Provider + ToString(Factor) + Argument.Name; } - + private: const TString Provider; const float Factor; const TArgument Argument; }; -class TInverseOrder: public ICommonInterface { +class TInverseOrder: public ICommonInterface { public: TInverseOrder(const TString& provider, float factor, TArgument& argument) : Provider(provider) , Factor(factor) , Argument(argument) - { - } + { + } TString GetValue() const override { return Argument.Name + ToString(Factor) + Provider; } - + private: const TString Provider; const float Factor; @@ -144,8 +144,8 @@ static TTestFactory::TRegistrator<TDirectOrderDifferentSignature> DirectDs("dire Y_UNIT_TEST_SUITE(TestObjectFactory) { Y_UNIT_TEST(TestParametrized) { - TArgument directArg{"Name", nullptr}; - TArgument inverseArg{"Fake", nullptr}; + TArgument directArg{"Name", nullptr}; + TArgument inverseArg{"Fake", nullptr}; THolder<ICommonInterface> direct(TTestFactory::Construct("direct", "prov", 0.42, directArg)); THolder<ICommonInterface> inverse(TTestFactory::Construct("inverse", "prov2", 1, inverseArg)); |