...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::interprocess::offset_ptr
// In header: <boost/interprocess/offset_ptr.hpp> template<typename PointedType> class offset_ptr { public: // types typedef PointedType * pointer; typedef unspecified reference; typedef PointedType value_type; typedef std::ptrdiff_t difference_type; typedef std::random_access_iterator_tag iterator_category; // member classes/structs/unions union internal_type { std::ptrdiff_t m_offset; PointedType * aliasing_helper; }; // construct/copy/destruct offset_ptr(pointer = 0); template<typename T> offset_ptr(T *); offset_ptr(const offset_ptr &); template<typename T2> offset_ptr(const offset_ptr< T2 > &); template<typename Y> offset_ptr(const offset_ptr< Y > &, unspecified); template<typename Y> offset_ptr(const offset_ptr< Y > &, unspecified); template<typename Y> offset_ptr(const offset_ptr< Y > &, unspecified); template<typename Y> offset_ptr(const offset_ptr< Y > &, unspecified); offset_ptr& operator=(pointer); offset_ptr& operator=(const offset_ptr &); template<typename T2> offset_ptr& operator=(const offset_ptr< T2 > &); // private member functions void unspecified_bool_type_func() const; void set_offset(const PointedType *); void * get_pointer() const; void inc_offset(std::ptrdiff_t); void dec_offset(std::ptrdiff_t); // public member functions pointer get() const; std::ptrdiff_t get_offset() const; pointer operator->() const; reference operator*() const; reference operator[](std::ptrdiff_t) const; offset_ptr operator+(std::ptrdiff_t) const; offset_ptr operator-(std::ptrdiff_t) const; offset_ptr & operator+=(std::ptrdiff_t); offset_ptr & operator-=(std::ptrdiff_t); offset_ptr & operator++(void); offset_ptr operator++(int); offset_ptr & operator--(void); offset_ptr operator--(int); operator unspecified_bool_type() const; bool operator!() const; };
A smart pointer that stores the offset between between the pointer and the the object it points. This allows offset allows special properties, since the pointer is independent from the address address of the pointee, if the pointer and the pointee are still separated by the same offset. This feature converts offset_ptr in a smart pointer that can be placed in shared memory and memory mapped files mapped in different addresses in every process.
offset_ptr
public
construct/copy/destructoffset_ptr(pointer ptr = 0);
Constructor from raw pointer (allows "0" pointer conversion). Never throws.
template<typename T> offset_ptr(T * ptr);
Constructor from other pointer. Never throws.
offset_ptr(const offset_ptr & ptr);
Constructor from other offset_ptr Never throws.
template<typename T2> offset_ptr(const offset_ptr< T2 > & ptr);
Constructor from other offset_ptr. If pointers of pointee types are convertible, offset_ptrs will be convertibles. Never throws.
template<typename Y> offset_ptr(const offset_ptr< Y > & r, unspecified);
Emulates static_cast operator. Never throws.
template<typename Y> offset_ptr(const offset_ptr< Y > & r, unspecified);
Emulates const_cast operator. Never throws.
template<typename Y> offset_ptr(const offset_ptr< Y > & r, unspecified);
Emulates dynamic_cast operator. Never throws.
template<typename Y> offset_ptr(const offset_ptr< Y > & r, unspecified);
Emulates reinterpret_cast operator. Never throws.
offset_ptr& operator=(pointer from);
Assignment from pointer (saves extra conversion). Never throws.
offset_ptr& operator=(const offset_ptr & pt);
Assignment from other offset_ptr. Never throws.
template<typename T2> offset_ptr& operator=(const offset_ptr< T2 > & pt);
Assignment from related offset_ptr. If pointers of pointee types are assignable, offset_ptrs will be assignable. Never throws.
offset_ptr
public member functionspointer get() const;
Obtains raw pointer from offset. Never throws.
std::ptrdiff_t get_offset() const;
pointer operator->() const;
Pointer-like -> operator. It can return 0 pointer. Never throws.
reference operator*() const;
Dereferencing operator, if it is a null offset_ptr behavior is undefined. Never throws.
reference operator[](std::ptrdiff_t idx) const;
Indexing operator. Never throws.
offset_ptr operator+(std::ptrdiff_t offset) const;
offset_ptr + std::ptrdiff_t. Never throws.
offset_ptr operator-(std::ptrdiff_t offset) const;
offset_ptr - std::ptrdiff_t. Never throws.
offset_ptr & operator+=(std::ptrdiff_t offset);
offset_ptr += std::ptrdiff_t. Never throws.
offset_ptr & operator-=(std::ptrdiff_t offset);
offset_ptr -= std::ptrdiff_t. Never throws.
offset_ptr & operator++(void);
++offset_ptr. Never throws.
offset_ptr operator++(int);
offset_ptr++. Never throws.
offset_ptr & operator--(void);
--offset_ptr. Never throws.
offset_ptr operator--(int);
offset_ptr--. Never throws.
operator unspecified_bool_type() const;
safe bool conversion operator. Never throws.
bool operator!() const;
Not operator. Not needed in theory, but improves portability. Never throws