2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifndef TCG_PTR_WRAPPER
|
|
|
|
#define TCG_PTR_WRAPPER
|
|
|
|
|
|
|
|
// STD includes
|
|
|
|
#include <iterator>
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace tcg {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief The ptr_wrapper class implements the basic functions necessary
|
|
|
|
to wrap a generic pointer object. The use case for this class
|
|
|
|
is to allow pointer inheritance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
2016-06-15 18:43:10 +12:00
|
|
|
class ptr {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef T *ptr_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef typename std::iterator_traits<ptr_type>::iterator_category
|
|
|
|
iterator_category;
|
|
|
|
typedef typename std::iterator_traits<ptr_type>::value_type value_type;
|
|
|
|
typedef
|
|
|
|
typename std::iterator_traits<ptr_type>::difference_type difference_type;
|
|
|
|
typedef typename std::iterator_traits<ptr_type>::pointer pointer;
|
|
|
|
typedef typename std::iterator_traits<ptr_type>::reference reference;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
explicit ptr(ptr_type p = ptr_type()) : m_ptr(p) {}
|
|
|
|
|
|
|
|
operator bool() const { return m_ptr; } // There should be no need to use
|
|
|
|
// the Safe Bool idiom
|
|
|
|
bool operator==(const ptr &other) const { return (m_ptr == other.m_ptr); }
|
|
|
|
bool operator!=(const ptr &other) const { return (m_ptr != other.m_ptr); }
|
|
|
|
|
|
|
|
bool operator<(const ptr &other) const { return (m_ptr < other.m_ptr); }
|
|
|
|
bool operator>(const ptr &other) const { return (m_ptr > other.m_ptr); }
|
|
|
|
bool operator<=(const ptr &other) const { return (m_ptr <= other.m_ptr); }
|
|
|
|
bool operator>=(const ptr &other) const { return (m_ptr >= other.m_ptr); }
|
|
|
|
|
|
|
|
ptr &operator++() {
|
|
|
|
++m_ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ptr operator++(int) { return ptr(m_ptr++, *this); }
|
|
|
|
|
|
|
|
ptr &operator--() {
|
|
|
|
--m_ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ptr operator--(int) { return ptr(m_ptr--, *this); }
|
|
|
|
|
|
|
|
ptr operator+(difference_type d) const { return ptr(m_ptr + d, *this); }
|
|
|
|
ptr &operator+=(difference_type d) {
|
|
|
|
m_ptr += d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr operator-(difference_type d) const { return ptr(m_ptr - d, *this); }
|
|
|
|
ptr &operator-=(difference_type d) {
|
|
|
|
m_ptr -= d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
difference_type operator-(const ptr &other) const {
|
|
|
|
return m_ptr - other.m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer operator->() const { return m_ptr; }
|
|
|
|
reference operator*() const { return *m_ptr; }
|
|
|
|
reference operator[](difference_type d) const { return m_ptr[d]; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
ptr_type m_ptr;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=======================================================
|
|
|
|
|
|
|
|
template <typename T>
|
2016-06-15 18:43:10 +12:00
|
|
|
ptr<T> make_ptr(T *p) {
|
|
|
|
return ptr<T>(p);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace tcg
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TCG_PTR_WRAPPER
|