18 template <
typename T,
typename TA>
class VPtr;
20 namespace private_utils {
22 template <
typename T>
struct Dereferenced {
typedef T type; };
23 template <
typename T>
struct Dereferenced<T *(*)()> {
typedef T *type; };
24 template <
typename T>
struct Dereferenced<T *> {
typedef T type; };
25 template <
typename T,
typename A>
struct Dereferenced<VPtr<T, A> > {
typedef T type; };
28 template <
typename T> T *pointerTo(
const T &val) {
return (T *)&(
char &)val; }
45 template <
typename T,
typename TA>
class VPtr :
public BaseVPtr
58 #ifdef VIRTMEM_WRAP_CPOINTERS
62 return static_cast<T *
>(
getAlloc()->read(p,
sizeof(T)));
64 T *read(
void)
const {
return read(
ptr); }
66 static void write(
PtrNum p,
const T *d)
68 #ifdef VIRTMEM_WRAP_CPOINTERS
75 void write(
const T *d) { write(
ptr, d); }
77 ThisVPtr copy(
void)
const { ThisVPtr ret; ret.ptr =
ptr;
return ret; }
78 template <
typename>
friend class VPtrLock;
92 template <
typename,
typename>
friend class VPtr;
99 inline operator T(
void)
const {
return *read(ptr); }
100 template <
typename T2>
VIRTMEM_EXPLICIT inline operator T2(
void)
const {
return static_cast<T2
>(
operator T()); }
110 const T val = *read(v.ptr);
121 const T val = *read(v.ptr);
127 inline ValueWrapper &operator=(
const T &v) { write(ptr, &v);
return *
this; }
128 inline ThisVPtr operator&(
void) { ThisVPtr ret; ret.
ptr = ptr;
return ret; }
132 inline T operator->(
void) {
return operator T(); }
133 inline const T operator->(
void)
const {
return operator T(); }
138 {
return operator T()[i]; }
140 {
return operator T()[i]; }
142 template <
typename T2>
inline bool operator==(
const T2 &v)
const {
return operator T() == v; }
143 template <
typename T2>
inline bool operator!=(
const T2 &v)
const {
return operator T() != v; }
145 ValueWrapper &operator+=(
int n) { T newv =
operator T() + n; write(ptr, private_utils::pointerTo(newv));
return *
this; }
146 ValueWrapper &operator-=(
int n) {
return operator+=(-n); }
147 ValueWrapper &operator*=(
int n) { T newv =
operator T() * n; write(ptr, private_utils::pointerTo(newv));
return *
this; }
148 ValueWrapper &operator/=(
int n) { T newv =
operator T() / n; write(ptr, private_utils::pointerTo(newv));
return *
this; }
149 ValueWrapper &operator++(
void) {
return operator +=(1); }
150 T operator++(
int) { T ret =
operator T(); operator++();
return ret; }
163 template <
typename,
typename>
friend class VPtr;
173 #ifdef VIRTMEM_WRAP_CPOINTERS
186 #ifdef VIRTMEM_WRAP_CPOINTERS
190 return static_cast<T *
>(
getAlloc()->makeDataLock(getPtrNum(ptr),
sizeof(T)));
192 const T *operator->(
void)
const
194 #ifdef VIRTMEM_WRAP_CPOINTERS
198 return static_cast<T *
>(
getAlloc()->makeDataLock(getPtrNum(ptr),
sizeof(T),
true));
205 VPtr(
void) =
default;
209 #ifdef VIRTMEM_WRAP_CPOINTERS
236 static ThisVPtr
wrap(
const T *p)
263 #ifdef VIRTMEM_VIRT_ADDRESS_OPERATOR
292 ValueWrapper operator*(
void) {
return ValueWrapper(
ptr); }
293 MemberWrapper operator->(
void) {
return MemberWrapper(
ptr); }
294 const MemberWrapper operator->(
void)
const {
return MemberWrapper(
ptr); }
295 const ValueWrapper operator[](
int i)
const {
return ValueWrapper(
ptr + (i *
sizeof(T))); }
296 ValueWrapper operator[](
int i) {
return ValueWrapper(
ptr + (i *
sizeof(T))); }
303 inline operator VPtr<const T, Allocator>(void) { VPtr<const T, Allocator> ret; ret.ptr =
ptr;
return ret; }
305 template <
typename T2>
VIRTMEM_EXPLICIT inline operator VPtr<T2, Allocator>(void) { VPtr<T2, Allocator> ret; ret.ptr =
ptr;
return ret; }
314 ThisVPtr &operator+=(
int n) {
ptr += (n * (int)
sizeof(T));
return *
this; }
315 inline ThisVPtr &operator++(
void) {
return operator +=(1); }
316 inline ThisVPtr operator++(
int) { ThisVPtr ret = copy(); operator++();
return ret; }
317 inline ThisVPtr &operator-=(
int n) {
return operator +=(-n); }
318 inline ThisVPtr &operator--(
void) {
return operator -=(1); }
319 inline ThisVPtr operator--(
int) { ThisVPtr ret = copy(); operator--();
return ret; }
320 inline ThisVPtr operator+(
int n)
const {
return (copy() += n); }
321 inline ThisVPtr operator-(
int n)
const {
return (copy() -= n); }
322 int operator-(
const ThisVPtr &other)
const {
return (
ptr - other.ptr) /
sizeof(T); }
330 static inline Allocator *
getAlloc(
void) {
return static_cast<Allocator *
>(Allocator::getInstance()); }
336 #endif // VIRTMEM_VPTR_H
contains all code from virtmem
Definition: base_alloc.cpp:22
PtrNum ptr
Numeric representation of this virtual pointer.
Definition: base_vptr.h:96
private_utils::Conditional<(sizeof(intptr_t) > sizeof(VPtrNum)), intptr_t, VPtrNum >::type PtrNum
Platform dependent numeric type to store raw (virtual/regular) pointer addresses. ...
Definition: base_vptr.h:72
void * unwrap(void)
Provide access to wrapped regular pointer (non static version).
Definition: base_vptr.h:174
VPtr(NILL_t)
Construct from NILL/nullptr (C++11 only)
Definition: vptr.h:206
T * TPtr
(non virtual) pointer type to the base type of this virtual pointer.
Definition: vptr.h:48
This header file contains several variables that can be used to customize virtmem.
#define VIRTMEM_EXPLICIT
Used for explicit conversion operators.
Definition: config.h:72
static Allocator * getAlloc(void)
Returns instance of allocator bound to this virtual pointer.
Definition: vptr.h:330
Proxy class used when member access is requested on a virtual pointer.
Definition: vptr.h:159
virtual memory class header
T * unwrap(void)
Provide access to wrapped regular pointer (non static version).
Definition: vptr.h:255
VPtr * addressOf(void)
Returns a regular pointer to the address of this virtual pointer.
Definition: vptr.h:279
VPtr< ThisVPtr, Allocator > operator&(void)
Returns a virtual pointer that has the memory address of this virtual pointer wrapped.
Definition: vptr.h:270
static T * unwrap(const ThisVPtr &p)
Provide access to wrapped regular pointer.
Definition: vptr.h:249
Proxy class returned when dereferencing virtual pointers.
Definition: vptr.h:85
const T * unwrap(void) const
Provide access to wrapped regular pointer (non static const version).
Definition: vptr.h:261
bool isWrapped(void) const
Returns whether a virtual pointer has wrapped a regular pointer (non static version).
Definition: base_vptr.h:138
static PtrNum getWrapped(PtrNum p)
Returns raw address of regular pointer wrapped by a virtual pointer.
Definition: base_vptr.h:125
Generalized NULL pointer class.
This file contains the virtual pointer base class.
static ThisVPtr wrap(const T *p)
Wraps a regular pointer.
Definition: vptr.h:236
const VPtr * addressOf(void) const
Returns a regular pointer to the address of this virtual pointer.
Definition: vptr.h:278
Virtual pointer class that provides an interface to access virtual much like 'regular pointers'...
Definition: alloc.h:15
TA Allocator
Allocator type used by this virtual pointer class.
Definition: vptr.h:49
This is the base class of VPtr and can be used for typeless pointers.
Definition: base_vptr.h:51