PxStrideIterator.h
Go to the documentation of this file.00001 // This code contains NVIDIA Confidential Information and is disclosed to you 00002 // under a form of NVIDIA software license agreement provided separately to you. 00003 // 00004 // Notice 00005 // NVIDIA Corporation and its licensors retain all intellectual property and 00006 // proprietary rights in and to this software and related documentation and 00007 // any modifications thereto. Any use, reproduction, disclosure, or 00008 // distribution of this software and related documentation without an express 00009 // license agreement from NVIDIA Corporation is strictly prohibited. 00010 // 00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES 00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO 00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT, 00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. 00015 // 00016 // Information and code furnished is believed to be accurate and reliable. 00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such 00018 // information or for any infringement of patents or other rights of third parties that may 00019 // result from its use. No license is granted by implication or otherwise under any patent 00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice. 00021 // This code supersedes and replaces all information previously supplied. 00022 // NVIDIA Corporation products are not authorized for use as critical 00023 // components in life support devices or systems without express written approval of 00024 // NVIDIA Corporation. 00025 // 00026 // Copyright (c) 2008-2012 NVIDIA Corporation. All rights reserved. 00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. 00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. 00029 00030 00031 #ifndef PX_FOUNDATION_PX_STRIDE_ITERATOR_H 00032 #define PX_FOUNDATION_PX_STRIDE_ITERATOR_H 00033 00034 #include "foundation/PxAssert.h" 00035 00040 #ifndef PX_DOXYGEN 00041 namespace physx 00042 { 00043 #endif 00044 00045 template<typename T> 00046 class PxStrideIterator 00047 { 00048 template <typename X> 00049 struct StripConst 00050 { 00051 typedef X Type; 00052 }; 00053 00054 template <typename X> 00055 struct StripConst<const X> 00056 { 00057 typedef X Type; 00058 }; 00059 00060 public: 00061 explicit PX_INLINE PxStrideIterator(T* ptr = NULL, PxU32 stride = sizeof(T)) : 00062 mPtr(ptr), mStride(stride) 00063 { 00064 PX_ASSERT(mStride == 0 || sizeof(T) <= mStride); 00065 } 00066 00067 PX_INLINE PxStrideIterator(const PxStrideIterator<typename StripConst<T>::Type>& strideIterator) : 00068 mPtr(strideIterator.ptr()), mStride(strideIterator.stride()) 00069 { 00070 PX_ASSERT(mStride == 0 || sizeof(T) <= mStride); 00071 } 00072 00073 PX_INLINE T* ptr() const 00074 { 00075 return mPtr; 00076 } 00077 00078 PX_INLINE PxU32 stride() const 00079 { 00080 return mStride; 00081 } 00082 00083 PX_INLINE T& operator*() const 00084 { 00085 return *mPtr; 00086 } 00087 00088 PX_INLINE T* operator->() const 00089 { 00090 return mPtr; 00091 } 00092 00093 PX_INLINE T& operator[](int i) const 00094 { 00095 return *byteAdd(mPtr, i * stride()); 00096 } 00097 00098 // preincrement 00099 PX_INLINE PxStrideIterator& operator++() 00100 { 00101 mPtr = byteAdd(mPtr, stride()); 00102 return *this; 00103 } 00104 00105 // postincrement 00106 PX_INLINE PxStrideIterator operator++(int) 00107 { 00108 PxStrideIterator tmp = *this; 00109 mPtr = byteAdd(mPtr, stride()); 00110 return tmp; 00111 } 00112 00113 // predecrement 00114 PX_INLINE PxStrideIterator& operator--() 00115 { 00116 mPtr = byteSub(mPtr, stride()); 00117 return *this; 00118 } 00119 00120 // postdecrement 00121 PX_INLINE PxStrideIterator operator--(int) 00122 { 00123 PxStrideIterator tmp = *this; 00124 mPtr = byteSub(mPtr, stride()); 00125 return tmp; 00126 } 00127 00128 PX_INLINE PxStrideIterator& operator+=(int i) 00129 { 00130 mPtr = byteAdd(mPtr, i * stride()); 00131 return *this; 00132 } 00133 00134 PX_INLINE PxStrideIterator operator+(int i) const 00135 { 00136 return PxStrideIterator(byteAdd(mPtr, i * stride()), stride()); 00137 } 00138 00139 PX_INLINE PxStrideIterator& operator-=(int i) 00140 { 00141 mPtr = byteSub(mPtr, i * stride()); 00142 return *this; 00143 } 00144 00145 PX_INLINE PxStrideIterator operator-(int i) const 00146 { 00147 return PxStrideIterator(byteSub(mPtr, i * stride()), stride()); 00148 } 00149 00150 // iterator difference 00151 PX_INLINE int operator-(const PxStrideIterator& other) const 00152 { 00153 PX_ASSERT(isCompatible(other)); 00154 int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr)); 00155 return byteDiff / static_cast<int>(stride()); 00156 } 00157 00158 PX_INLINE bool operator==(const PxStrideIterator& other) const 00159 { 00160 PX_ASSERT(isCompatible(other)); 00161 return mPtr == other.mPtr; 00162 } 00163 00164 PX_INLINE bool operator!=(const PxStrideIterator& other) const 00165 { 00166 PX_ASSERT(isCompatible(other)); 00167 return mPtr != other.mPtr; 00168 } 00169 00170 PX_INLINE bool operator<(const PxStrideIterator& other) const 00171 { 00172 PX_ASSERT(isCompatible(other)); 00173 return mPtr < other.mPtr; 00174 } 00175 00176 PX_INLINE bool operator>(const PxStrideIterator& other) const 00177 { 00178 PX_ASSERT(isCompatible(other)); 00179 return mPtr > other.mPtr; 00180 } 00181 00182 PX_INLINE bool operator<=(const PxStrideIterator& other) const 00183 { 00184 PX_ASSERT(isCompatible(other)); 00185 return mPtr <= other.mPtr; 00186 } 00187 00188 PX_INLINE bool operator>=(const PxStrideIterator& other) const 00189 { 00190 PX_ASSERT(isCompatible(other)); 00191 return mPtr >= other.mPtr; 00192 } 00193 00194 private: 00195 PX_INLINE static T* byteAdd(T* ptr, PxU32 bytes) 00196 { 00197 return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) + bytes)); 00198 } 00199 00200 PX_INLINE static T* byteSub(T* ptr, PxU32 bytes) 00201 { 00202 return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) - bytes)); 00203 } 00204 00205 PX_INLINE bool isCompatible(const PxStrideIterator& other) const 00206 { 00207 int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr)); 00208 return (stride() == other.stride()) && (abs(byteDiff) % stride() == 0); 00209 } 00210 00211 T* mPtr; 00212 PxU32 mStride; 00213 }; 00214 00215 template<typename T> 00216 PX_INLINE PxStrideIterator<T> operator+(int i, PxStrideIterator<T> it) 00217 { 00218 it += i; 00219 return it; 00220 } 00221 00222 template<typename T> 00223 PX_INLINE PxStrideIterator<T> PxMakeIterator(T* ptr, PxU32 stride = sizeof(T)) 00224 { 00225 return PxStrideIterator<T>(ptr, stride); 00226 } 00227 00228 template<typename T> 00229 PX_INLINE PxStrideIterator<const T> PxMakeIterator(const T* ptr, PxU32 stride = sizeof(T)) 00230 { 00231 return PxStrideIterator<const T>(ptr, stride); 00232 } 00233 00234 #ifndef PX_DOXYGEN 00235 } // namespace physx 00236 #endif 00237 00239 #endif // PX_FOUNDATION_PX_STRIDE_ITERATOR_H
Copyright © 2008-2012 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com