#pragma once #include #include "ptr.h" template struct TWithRefCount: public TBase, public TRefCounted, TCounter> { using TBase::TBase; }; template struct TPtrPolicy { inline TPtrPolicy(T* t Y_LIFETIME_BOUND) : T_(t) { } inline T* Ptr() noexcept { return T_; } inline const T* Ptr() const noexcept { return T_; } T* T_; }; template struct TEmbedPolicy { template ::value>::type> inline TEmbedPolicy(Args&&... args) : T_(std::forward(args)...) { } inline T* Ptr() noexcept { return &T_; } inline const T* Ptr() const noexcept { return &T_; } T T_; }; template struct TRefPolicy { using THelper = TWithRefCount; template ::value>::type> inline TRefPolicy(Args&&... args) : T_(new THelper(std::forward(args)...)) { } inline T* Ptr() noexcept { return T_.Get(); } inline const T* Ptr() const noexcept { return T_.Get(); } TIntrusivePtr 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 * struct TProxy { * TAutoEmbedOrPtrPolicy Value_; * // Your proxy code... * }; * * template * TProxy MakeProxy(T&& value) { * // Rvalues are automagically moved-from, and stored inside the proxy. * return {std::forward(value)}; * } * \endcode * * Look at `Reversed` in `adaptor.h` for real example. */ template ::value> struct TAutoEmbedOrPtrPolicy: TPtrPolicy> { using TBase = TPtrPolicy>; TAutoEmbedOrPtrPolicy(T& reference Y_LIFETIME_BOUND) : TBase(&reference) { } }; template struct TAutoEmbedOrPtrPolicy: TEmbedPolicy { using TBase = TEmbedPolicy; TAutoEmbedOrPtrPolicy(T&& object) : TBase(std::move(object)) { } }; template using TAtomicRefPolicy = TRefPolicy; template using TSimpleRefPolicy = TRefPolicy;