|
Shinya Kitaoka |
810553 |
#pragma once
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef TCG_PTR_WRAPPER
|
|
Toshihiro Shimizu |
890ddd |
#define TCG_PTR_WRAPPER
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// STD includes
|
|
Toshihiro Shimizu |
890ddd |
#include <iterator></iterator>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
/*!
|
|
Toshihiro Shimizu |
890ddd |
\brief The ptr_wrapper class implements the basic functions necessary
|
|
Toshihiro Shimizu |
890ddd |
to wrap a generic pointer object. The use case for this class
|
|
Toshihiro Shimizu |
890ddd |
is to allow pointer inheritance.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
class ptr
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
typedef T *ptr_type;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<ptr_type>::iterator_category iterator_category;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<ptr_type>::value_type value_type;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<ptr_type>::difference_type difference_type;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<ptr_type>::pointer pointer;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<ptr_type>::reference reference;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
explicit ptr(ptr_type p = ptr_type()) : m_ptr(p) {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
operator bool() const { return m_ptr; } // There should be no need to use
|
|
Toshihiro Shimizu |
890ddd |
// the Safe Bool idiom
|
|
Toshihiro Shimizu |
890ddd |
bool operator==(const ptr &other) const { return (m_ptr == other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
bool operator!=(const ptr &other) const { return (m_ptr != other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool operator<(const ptr &other) const { return (m_ptr < other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
bool operator>(const ptr &other) const { return (m_ptr > other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
bool operator<=(const ptr &other) const { return (m_ptr <= other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
bool operator>=(const ptr &other) const { return (m_ptr >= other.m_ptr); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
ptr &operator++()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
++m_ptr;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
ptr operator++(int) { return ptr(m_ptr++, *this); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
ptr &operator--()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
--m_ptr;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
ptr operator--(int) { return ptr(m_ptr--, *this); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
ptr operator+(difference_type d) const { return ptr(m_ptr + d, *this); }
|
|
Toshihiro Shimizu |
890ddd |
ptr &operator+=(difference_type d)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_ptr += d;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
ptr operator-(difference_type d) const { return ptr(m_ptr - d, *this); }
|
|
Toshihiro Shimizu |
890ddd |
ptr &operator-=(difference_type d)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_ptr -= d;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
difference_type operator-(const ptr &other) const { return m_ptr - other.m_ptr; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pointer operator->() const { return m_ptr; }
|
|
Toshihiro Shimizu |
890ddd |
reference operator*() const { return *m_ptr; }
|
|
Toshihiro Shimizu |
890ddd |
reference operator[](difference_type d) const { return m_ptr[d]; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
protected:
|
|
Toshihiro Shimizu |
890ddd |
ptr_type m_ptr;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=======================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
ptr<t> make_ptr(T *p) { return ptr<t>(p); }</t></t>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif // TCG_PTR_WRAPPER
|