|
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 |
|
|
Shinya Kitaoka |
120a6e |
namespace tcg {
|
|
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>
|
|
Shinya Kitaoka |
120a6e |
class ptr {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
typedef T *ptr_type;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
typedef typename std::iterator_traits<ptr_type>::iterator_category</ptr_type>
|
|
Shinya Kitaoka |
120a6e |
iterator_category;
|
|
Shinya Kitaoka |
120a6e |
typedef typename std::iterator_traits<ptr_type>::value_type value_type;</ptr_type>
|
|
Shinya Kitaoka |
120a6e |
typedef
|
|
Shinya Kitaoka |
120a6e |
typename std::iterator_traits<ptr_type>::difference_type difference_type;</ptr_type>
|
|
Shinya Kitaoka |
120a6e |
typedef typename std::iterator_traits<ptr_type>::pointer pointer;</ptr_type>
|
|
Shinya Kitaoka |
120a6e |
typedef typename std::iterator_traits<ptr_type>::reference reference;</ptr_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
explicit ptr(ptr_type p = ptr_type()) : m_ptr(p) {}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
operator bool() const { return m_ptr; } // There should be no need to use
|
|
Shinya Kitaoka |
120a6e |
// the Safe Bool idiom
|
|
Shinya Kitaoka |
120a6e |
bool operator==(const ptr &other) const { return (m_ptr == other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
bool operator!=(const ptr &other) const { return (m_ptr != other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
bool operator<(const ptr &other) const { return (m_ptr < other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
bool operator>(const ptr &other) const { return (m_ptr > other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
bool operator<=(const ptr &other) const { return (m_ptr <= other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
bool operator>=(const ptr &other) const { return (m_ptr >= other.m_ptr); }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
ptr &operator++() {
|
|
Shinya Kitaoka |
120a6e |
++m_ptr;
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
ptr operator++(int) { return ptr(m_ptr++, *this); }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
ptr &operator--() {
|
|
Shinya Kitaoka |
120a6e |
--m_ptr;
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
ptr operator--(int) { return ptr(m_ptr--, *this); }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
ptr operator+(difference_type d) const { return ptr(m_ptr + d, *this); }
|
|
Shinya Kitaoka |
120a6e |
ptr &operator+=(difference_type d) {
|
|
Shinya Kitaoka |
120a6e |
m_ptr += d;
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
ptr operator-(difference_type d) const { return ptr(m_ptr - d, *this); }
|
|
Shinya Kitaoka |
120a6e |
ptr &operator-=(difference_type d) {
|
|
Shinya Kitaoka |
120a6e |
m_ptr -= d;
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
difference_type operator-(const ptr &other) const {
|
|
Shinya Kitaoka |
120a6e |
return m_ptr - other.m_ptr;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
pointer operator->() const { return m_ptr; }
|
|
Shinya Kitaoka |
120a6e |
reference operator*() const { return *m_ptr; }
|
|
Shinya Kitaoka |
120a6e |
reference operator[](difference_type d) const { return m_ptr[d]; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
protected:
|
|
Shinya Kitaoka |
120a6e |
ptr_type m_ptr;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=======================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Shinya Kitaoka |
120a6e |
ptr<t> make_ptr(T *p) {</t>
|
|
Shinya Kitaoka |
120a6e |
return ptr<t>(p);</t>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
} // namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
#endif // TCG_PTR_WRAPPER
|