diff options
author | claiff <claiff@mail.ru> | 2022-10-06 12:26:45 +0300 |
---|---|---|
committer | claiff <claiff@mail.ru> | 2022-10-06 12:26:45 +0300 |
commit | da5f91a0ce15351a82beb061c3ccc965164529bc (patch) | |
tree | 1f4cd2d1e2f8cf2442d055c519d06c6e0692febc /src/general/idecorator.hpp | |
parent | 8edef99a5b52643e0b919c424357752cbbc9a8dd (diff) | |
download | obmc-sila-smtp-da5f91a0ce15351a82beb061c3ccc965164529bc.tar.xz |
create settings on startbugfix/proxy_file_reader
Diffstat (limited to 'src/general/idecorator.hpp')
-rw-r--r-- | src/general/idecorator.hpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/general/idecorator.hpp b/src/general/idecorator.hpp new file mode 100644 index 0000000..53efda5 --- /dev/null +++ b/src/general/idecorator.hpp @@ -0,0 +1,82 @@ +#pragma once
+
+#include <memory>
+
+namespace smtp::general
+{
+
+/**
+ * @brief Базовый интерфейс описания декоратора
+ * @details См. https://refactoring.guru/ru/design-patterns/decorator
+ * @tparam T Тип декорируемого класса, T должен обладать возможностью наследования (в том числе виртуальный деструктор)
+ */
+ template < typename T, typename TPointerType = std::shared_ptr<T> > class IDecorator : public T
+ {
+ public:
+ using PointerType = TPointerType;
+ using Type = IDecorator<T, TPointerType>;
+
+ ~IDecorator() override = default;
+
+ /**
+ * @brief Установить указатель на декорируемый класс
+ * @details По возможности используйте метод Apply() как наиболее универсальный
+ * @param base Указатель на декорируемый класс
+ */
+ void SetBase( PointerType const& base ) noexcept
+ {
+ mBase = base;
+ }
+
+ void SetBase( PointerType&& base ) noexcept
+ {
+ mBase = std::move( base );
+ }
+
+ /**
+ * @brief Установить указатель на декорируемый класс (цепочка обязанностей)
+ * @details См. https://refactoring.guru/ru/design-patterns/chain-of-responsibility/cpp/example
+ * @details Цепь вызовов: декоратор1 -> декоратор2 -> базовый класс
+ * @param base_or_decorator Указатель на декорируемый класс или целевой декоратор
+ */
+ Type& Apply( PointerType const& base_or_decorator ) noexcept
+ {
+ if( !base_or_decorator )
+ {
+ return *this;
+ }
+
+ SetBase( base_or_decorator );
+ return GetApplyResult();
+ }
+
+ Type& Apply( PointerType&& base_or_decorator ) noexcept
+ {
+ if( !base_or_decorator )
+ {
+ return *this;
+ }
+
+ SetBase( std::move( base_or_decorator ) );
+ return GetApplyResult();
+ }
+
+ private:
+ Type& GetApplyResult() noexcept
+ {
+ auto as_decorator = dynamic_cast< Type * >( mBase.get() );
+ if( as_decorator )
+ {
+ return *as_decorator;
+ }
+
+ return *this;
+ }
+
+ protected:
+ PointerType mBase;
+ };
+
+ template < typename T, typename TDeleter = std::default_delete<T> > using IDecoratorUnique = IDecorator<T, std::unique_ptr<T, TDeleter> >;
+
+} // namespace sbis::devices::generic::types
|