PxStrideIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA CORPORATION and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA CORPORATION is strictly prohibited.
9  */
10 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12 
13 
14 #ifndef PX_FOUNDATION_PX_STRIDE_ITERATOR_H
15 #define PX_FOUNDATION_PX_STRIDE_ITERATOR_H
16 
17 #include "foundation/PxAssert.h"
18 
23 #ifndef PX_DOXYGEN
24 namespace physx
25 {
26 #endif
27 
63 template<typename T>
65 {
66 
67 #ifndef PX_DOXYGEN
68  template <typename X>
69  struct StripConst
70  {
71  typedef X Type;
72  };
73 
74  template <typename X>
75  struct StripConst<const X>
76  {
77  typedef X Type;
78  };
79 #endif
80 
81 public:
82 
91  explicit PX_INLINE PxStrideIterator(T* ptr = NULL, PxU32 stride = sizeof(T)) :
92  mPtr(ptr), mStride(stride)
93  {
94  PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
95  }
96 
102  PX_INLINE PxStrideIterator(const PxStrideIterator<typename StripConst<T>::Type>& strideIterator) :
103  mPtr(strideIterator.ptr()), mStride(strideIterator.stride())
104  {
105  PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
106  }
107 
111  PX_INLINE T* ptr() const
112  {
113  return mPtr;
114  }
115 
119  PX_INLINE PxU32 stride() const
120  {
121  return mStride;
122  }
123 
127  PX_INLINE T& operator*() const
128  {
129  return *mPtr;
130  }
131 
135  PX_INLINE T* operator->() const
136  {
137  return mPtr;
138  }
139 
143  PX_INLINE T& operator[](unsigned int i) const
144  {
145  return *byteAdd(mPtr, i * stride());
146  }
147 
151  PX_DEPRECATED PX_INLINE T& operator[](int i) const
152  {
153  PX_ASSERT(i >= 0);
154  return this->operator[]((unsigned int)i);
155  }
156 
161  {
162  mPtr = byteAdd(mPtr, stride());
163  return *this;
164  }
165 
169  PX_INLINE PxStrideIterator operator++(int)
170  {
171  PxStrideIterator tmp = *this;
172  mPtr = byteAdd(mPtr, stride());
173  return tmp;
174  }
175 
180  {
181  mPtr = byteSub(mPtr, stride());
182  return *this;
183  }
184 
188  PX_INLINE PxStrideIterator operator--(int)
189  {
190  PxStrideIterator tmp = *this;
191  mPtr = byteSub(mPtr, stride());
192  return tmp;
193  }
194 
198  PX_INLINE PxStrideIterator operator+(unsigned int i) const
199  {
200  return PxStrideIterator(byteAdd(mPtr, i * stride()), stride());
201  }
202 
207  {
208  return PxStrideIterator(byteAdd(mPtr, i * stride()), stride());
209  }
210 
214  PX_INLINE PxStrideIterator operator-(unsigned int i) const
215  {
216  return PxStrideIterator(byteSub(mPtr, i * stride()), stride());
217  }
218 
222  PX_DEPRECATED PX_INLINE PxStrideIterator operator-(int i) const
223  {
224  return PxStrideIterator(byteSub(mPtr, i * stride()), stride());
225  }
226 
230  PX_INLINE PxStrideIterator& operator+=(unsigned int i)
231  {
232  mPtr = byteAdd(mPtr, i * stride());
233  return *this;
234  }
235 
240  {
241  mPtr = byteAdd(mPtr, i * stride());
242  return *this;
243  }
244 
248  PX_INLINE PxStrideIterator& operator-=(unsigned int i)
249  {
250  mPtr = byteSub(mPtr, i * stride());
251  return *this;
252  }
253 
258  {
259  mPtr = byteSub(mPtr, i * stride());
260  return *this;
261  }
262 
266  PX_INLINE int operator-(const PxStrideIterator& other) const
267  {
268  PX_ASSERT(isCompatible(other));
269  int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
270  return byteDiff / static_cast<int>(stride());
271  }
272 
276  PX_INLINE bool operator==(const PxStrideIterator& other) const
277  {
278  PX_ASSERT(isCompatible(other));
279  return mPtr == other.mPtr;
280  }
281 
285  PX_INLINE bool operator!=(const PxStrideIterator& other) const
286  {
287  PX_ASSERT(isCompatible(other));
288  return mPtr != other.mPtr;
289  }
290 
294  PX_INLINE bool operator<(const PxStrideIterator& other) const
295  {
296  PX_ASSERT(isCompatible(other));
297  return mPtr < other.mPtr;
298  }
299 
303  PX_INLINE bool operator>(const PxStrideIterator& other) const
304  {
305  PX_ASSERT(isCompatible(other));
306  return mPtr > other.mPtr;
307  }
308 
312  PX_INLINE bool operator<=(const PxStrideIterator& other) const
313  {
314  PX_ASSERT(isCompatible(other));
315  return mPtr <= other.mPtr;
316  }
317 
321  PX_INLINE bool operator>=(const PxStrideIterator& other) const
322  {
323  PX_ASSERT(isCompatible(other));
324  return mPtr >= other.mPtr;
325  }
326 
327 private:
328  PX_INLINE static T* byteAdd(T* ptr, PxU32 bytes)
329  {
330  return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) + bytes));
331  }
332 
333  PX_INLINE static T* byteSub(T* ptr, PxU32 bytes)
334  {
335  return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) - bytes));
336  }
337 
338  PX_INLINE bool isCompatible(const PxStrideIterator& other) const
339  {
340  int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
341  return (stride() == other.stride()) && (abs(byteDiff) % stride() == 0);
342  }
343 
344  T* mPtr;
346 };
347 
351 template<typename T>
353 {
354  it += i;
355  return it;
356 }
357 
361 template<typename T>
363 {
364  return PxStrideIterator<T>(ptr, stride);
365 }
366 
370 template<typename T>
371 PX_INLINE PxStrideIterator<const T> PxMakeIterator(const T* ptr, PxU32 stride = sizeof(T))
372 {
373  return PxStrideIterator<const T>(ptr, stride);
374 }
375 
376 #ifndef PX_DOXYGEN
377 } // namespace physx
378 #endif
379 
381 #endif // PX_FOUNDATION_PX_STRIDE_ITERATOR_H


Copyright © 2008-2015 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com