Template Class relocatable_ptr

Class Documentation

template<typename T>
class relocatable_ptr

Smart pointer type that allows objects using it to able to be copied by memcpy without invalidating the pointer. This applies only if it points to memory owned by the object itself (i.e. not to memory outside of the object). This is useful to improve copy-efficiency and allow the types build with relocatable pointers only to be stored in shared memory. It is useable like a raw pointer of the corresponding type and can be implicily converted to one.

Todo:

specialize for another pointer class for this use case once it is fully defined/understood

Note

It is advisable to use relocatable_ptr only for storage (e.g. member variables), not to pass them around as function arguments or as return value. There should be no need for this, since as pass-around type regular pointers do the job just fine and do not incur the slight runtime overhead of a relocatable_ptr. There should be no memory overhead on 64 bit systems.

Note

relocatable_ptr is not trivially copyable since in general the copy constructor requires additional logic. Hence obects that contain it re not trivially copyable in the C++ sense. However, if the pointees of a host object containing the relocatable ptr are all located inside the object and the obect is otherwise trivially copyable it can be safely copied by memcpy.

Template Parameters:

T – the native type wrapped by the relocatable_ptr, i.e. relocatable_ptr<T> has native type T and corresponds to a raw pointer of type T*.

Public Types

using ptr_t = T*

Public Functions

relocatable_ptr(T *ptr = nullptr) noexcept

Construct from raw pointer.

relocatable_ptr(const relocatable_ptr &other) noexcept

Construct from other relocatable pointer.

relocatable_ptr(relocatable_ptr &&other)

Move construct from other relocatable pointer.

relocatable_ptr &operator=(const relocatable_ptr &rhs) noexcept

Assign from relocatable pointer rhs.

relocatable_ptr &operator=(relocatable_ptr &&rhs) noexcept

Move assign from relocatable pointer rhs.

T *get() noexcept

Get the corresponding raw pointer.

Returns:

corresponding raw pointer

const T *get() const noexcept

Get the corresponding raw pointer from const relocatable_ptr.

Returns:

corresponding raw pointer

template<typename S = T>
S &operator*() noexcept

Dereference a relocatable_ptr.

Note

not available for T=void

Returns:

reference to the pointee

template<typename S = T>
const S &operator*() const noexcept

Dereference a const relocatable_ptr.

Note

not available for T=void

Returns:

reference to the pointee

T *operator->() noexcept

Get the corresponding raw pointer with arrow operator syntax.

Returns:

corresponding raw pointer

const T *operator->() const noexcept

Get the corresponding raw pointer with arrow operator syntax from a const relocatable_ptr.

Returns:

corresponding raw pointer

operator T*() noexcept

Convert to the corresponding raw pointer.

Returns:

corresponding raw pointer

operator const T*() const noexcept

Convert to the corresponding const raw pointer.

Returns:

corresponding const raw pointer