diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/store_policy.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/store_policy.h')
-rw-r--r-- | util/generic/store_policy.h | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/util/generic/store_policy.h b/util/generic/store_policy.h new file mode 100644 index 0000000000..148821c70c --- /dev/null +++ b/util/generic/store_policy.h @@ -0,0 +1,120 @@ +#pragma once + +#include <utility> +#include "ptr.h" + +template <class TBase, class TCounter> +struct TWithRefCount: public TBase, public TRefCounted<TWithRefCount<TBase, TCounter>, TCounter> { + template <typename... Args> + inline TWithRefCount(Args&&... args) + : TBase(std::forward<Args>(args)...) + { + } +}; + +template <class T> +struct TPtrPolicy { + inline TPtrPolicy(T* t) + : T_(t) + { + } + + inline T* Ptr() noexcept { + return T_; + } + + inline const T* Ptr() const noexcept { + return T_; + } + + T* T_; +}; + +template <class T> +struct TEmbedPolicy { + template <typename... Args> + inline TEmbedPolicy(Args&&... args) + : T_(std::forward<Args>(args)...) + { + } + + inline T* Ptr() noexcept { + return &T_; + } + + inline const T* Ptr() const noexcept { + return &T_; + } + + T T_; +}; + +template <class T, class TCounter> +struct TRefPolicy { + using THelper = TWithRefCount<T, TCounter>; + + template <typename... Args> + inline TRefPolicy(Args&&... args) + : T_(new THelper(std::forward<Args>(args)...)) + { + } + + inline T* Ptr() noexcept { + return T_.Get(); + } + + inline const T* Ptr() const noexcept { + return T_.Get(); + } + + TIntrusivePtr<THelper> T_; +}; + +/** + * Storage class that can be handy for implementing proxies / adaptors that can + * accept both lvalues and rvalues. In the latter case it's often required to + * extend the lifetime of the passed rvalue, and the only option is to store it + * in your proxy / adaptor. + * + * Example usage: + * \code + * template<class T> + * struct TProxy { + * TAutoEmbedOrPtrPolicy<T> Value_; + * // Your proxy code... + * }; + * + * template<class T> + * TProxy<T> MakeProxy(T&& value) { + * // Rvalues are automagically moved-from, and stored inside the proxy. + * return {std::forward<T>(value)}; + * } + * \endcode + * + * Look at `Reversed` in `adaptor.h` for real example. + */ +template <class T, bool IsReference = std::is_reference<T>::value> +struct TAutoEmbedOrPtrPolicy: TPtrPolicy<std::remove_reference_t<T>> { + using TBase = TPtrPolicy<std::remove_reference_t<T>>; + + TAutoEmbedOrPtrPolicy(T& reference) + : TBase(&reference) + { + } +}; + +template <class T> +struct TAutoEmbedOrPtrPolicy<T, false>: TEmbedPolicy<T> { + using TBase = TEmbedPolicy<T>; + + TAutoEmbedOrPtrPolicy(T&& object) + : TBase(std::move(object)) + { + } +}; + +template <class T> +using TAtomicRefPolicy = TRefPolicy<T, TAtomicCounter>; + +template <class T> +using TSimpleRefPolicy = TRefPolicy<T, TSimpleCounter>; |