memory: add get_ptr
This was migrated from WinStd being platform independent. Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
5675f8b139
commit
4fa30d5630
@ -139,6 +139,160 @@ namespace stdex
|
||||
public:
|
||||
unsigned char m_data[N]; ///< BLOB data
|
||||
};
|
||||
|
||||
///
|
||||
/// Helper class for returning pointers to std::unique_ptr
|
||||
///
|
||||
template <typename T, typename D>
|
||||
class ref_unique_ptr
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Takes ownership of the pointer
|
||||
///
|
||||
/// \param[in,out] owner Object to attach helper to
|
||||
///
|
||||
ref_unique_ptr(_Inout_ std::unique_ptr<T, D> &owner) :
|
||||
m_own(owner),
|
||||
m_ptr(owner.release())
|
||||
{}
|
||||
|
||||
///
|
||||
/// Moves object
|
||||
///
|
||||
/// \param[in,out] other Source object
|
||||
///
|
||||
ref_unique_ptr(_Inout_ ref_unique_ptr<T, D> &&other) :
|
||||
m_own(other.m_own),
|
||||
m_ptr(other.m_ptr)
|
||||
{
|
||||
other.m_ptr = nullptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns ownership of the pointer
|
||||
///
|
||||
~ref_unique_ptr()
|
||||
{
|
||||
if (m_ptr != nullptr)
|
||||
m_own.reset(m_ptr);
|
||||
}
|
||||
|
||||
///
|
||||
/// Operator for pointer-to-pointer parameters by value use-cases.
|
||||
///
|
||||
/// \return Pointer to the pointer
|
||||
///
|
||||
operator T**()
|
||||
{
|
||||
return &m_ptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// Operator for reverence-to-pointer parameters by value use-cases.
|
||||
///
|
||||
/// \return Reference to the pointer
|
||||
///
|
||||
operator T*&()
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<T, D> &m_own; ///< Original owner of the pointer
|
||||
T *m_ptr; ///< Pointer
|
||||
};
|
||||
|
||||
///
|
||||
/// Helper function template for returning pointers to std::unique_ptr
|
||||
///
|
||||
/// \param[in,out] owner Original owner of the pointer
|
||||
///
|
||||
/// \returns A helper wrapper class to handle returning a reference to the pointer
|
||||
///
|
||||
template<class T, class D>
|
||||
ref_unique_ptr<T, D> get_ptr(_Inout_ std::unique_ptr<T, D> &owner) noexcept
|
||||
{
|
||||
return ref_unique_ptr<T, D>(owner);
|
||||
}
|
||||
|
||||
///
|
||||
/// Helper class for returning pointers to std::unique_ptr
|
||||
/// (specialization for arrays)
|
||||
///
|
||||
template<typename T, typename D>
|
||||
class ref_unique_ptr<T[], D>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Takes ownership of the pointer
|
||||
///
|
||||
/// \param[in,out] owner Object to attach helper to
|
||||
///
|
||||
ref_unique_ptr(_Inout_ std::unique_ptr<T[], D> &owner) noexcept :
|
||||
m_own(owner),
|
||||
m_ptr(owner.release())
|
||||
{}
|
||||
|
||||
///
|
||||
/// Moves object
|
||||
///
|
||||
/// \param[in,out] other Source object
|
||||
///
|
||||
ref_unique_ptr(_Inout_ ref_unique_ptr<T[], D> &&other) :
|
||||
m_own(other.m_own),
|
||||
m_ptr(other.m_ptr)
|
||||
{
|
||||
other.m_ptr = nullptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns ownership of the pointer
|
||||
///
|
||||
virtual ~ref_unique_ptr()
|
||||
{
|
||||
if (m_ptr != nullptr)
|
||||
m_own.reset(m_ptr);
|
||||
}
|
||||
|
||||
///
|
||||
/// Operator for pointer-to-pointer parameters by value use-cases.
|
||||
///
|
||||
/// \return Pointer to the pointer
|
||||
///
|
||||
operator T**() noexcept
|
||||
{
|
||||
return &m_ptr;
|
||||
}
|
||||
|
||||
///
|
||||
/// Operator for reverence-to-pointer parameters by value use-cases.
|
||||
///
|
||||
/// \return Reference to the pointer
|
||||
///
|
||||
operator T*&()
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<T[], D> &m_own; ///< Original owner of the pointer
|
||||
T *m_ptr; ///< Pointer
|
||||
};
|
||||
|
||||
///
|
||||
/// Helper function template for returning pointers to std::unique_ptr
|
||||
/// (specialization for arrays)
|
||||
///
|
||||
/// \param[in,out] owner Original owner of the pointer
|
||||
///
|
||||
/// \returns A helper wrapper class to handle returning a reference to the pointer
|
||||
///
|
||||
template<class T, class D>
|
||||
ref_unique_ptr<T[], D> get_ptr(_Inout_ std::unique_ptr<T[], D>& owner) noexcept
|
||||
{
|
||||
return ref_unique_ptr<T[], D>(owner);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__GNUC__)
|
||||
|
Loading…
x
Reference in New Issue
Block a user