pointer: add thin pointer wrapper

This commit is contained in:
Danny Robson 2020-11-25 14:21:20 +10:00
parent 252a870a22
commit 8bfcab5f40

View File

@ -13,6 +13,57 @@
#include <cstdint>
namespace cruft::ptr {
/// A utiliity function that deletes the provided pointer.
///
/// Useful as a means of moving `delete` calls inside a translation unit
/// and hence removing the need to include implementation headers for a
/// type that needs deletion.
template <typename ValueT>
void destroy (ValueT*);
/// A smart pointer that defers deletion to `destroy`.
///
/// This is solely useful as a way of avoid undefined type errors when
/// using std::unique_ptr and forward declarations.
template <typename ValueT>
class thin {
public:
explicit thin (ValueT *_value) { m_value = _value; }
~thin () { destroy (m_value); }
thin (thin const&) = delete;
thin& operator= (thin const&) = delete;
thin (thin &&rhs)
: m_value (rhs.m_value)
{ rhs.m_value = nullptr; }
thin& operator= (thin &&rhs) noexcept
{
std::swap (m_value, rhs.m_value);
}
ValueT & operator* (void) &{ return *m_value; }
ValueT const& operator* (void) const &{ return *m_value; }
ValueT * operator->(void) &{ return m_value; }
ValueT const* operator->(void) const &{ return m_value; }
private:
ValueT *m_value;
};
template <typename ValueT, typename ...ArgsT>
thin<ValueT>
make_thin (ArgsT &&...args)
{
return thin (new ValueT (std::forward<ArgsT> (args)...));
}
}
namespace cruft::align {
///////////////////////////////////////////////////////////////////////////
/// round the pointer upwards to satisfy the provided alignment