include/foundation/PxStrideIterator.h
File members: include/foundation/PxStrideIterator.h
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef PX_STRIDE_ITERATOR_H
#define PX_STRIDE_ITERATOR_H
#include "foundation/Px.h"
#include "foundation/PxAssert.h"
#if !PX_DOXYGEN
namespace physx
{
#endif
template <typename T>
class PxStrideIterator
{
#if !PX_DOXYGEN
template <typename X>
struct StripConst
{
typedef X Type;
};
template <typename X>
struct StripConst<const X>
{
typedef X Type;
};
#endif
public:
explicit PX_INLINE PxStrideIterator(T* ptr = NULL, PxU32 stride = sizeof(T)) : mPtr(ptr), mStride(stride)
{
PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
}
PX_INLINE PxStrideIterator(const PxStrideIterator<typename StripConst<T>::Type>& strideIterator)
: mPtr(strideIterator.ptr()), mStride(strideIterator.stride())
{
PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
}
PX_INLINE T* ptr() const
{
return mPtr;
}
PX_INLINE PxU32 stride() const
{
return mStride;
}
PX_INLINE T& operator*() const
{
return *mPtr;
}
PX_INLINE T* operator->() const
{
return mPtr;
}
PX_INLINE T& operator[](unsigned int i) const
{
return *byteAdd(mPtr, i * stride());
}
PX_INLINE PxStrideIterator& operator++()
{
mPtr = byteAdd(mPtr, stride());
return *this;
}
PX_INLINE PxStrideIterator operator++(int)
{
PxStrideIterator tmp = *this;
mPtr = byteAdd(mPtr, stride());
return tmp;
}
PX_INLINE PxStrideIterator& operator--()
{
mPtr = byteSub(mPtr, stride());
return *this;
}
PX_INLINE PxStrideIterator operator--(int)
{
PxStrideIterator tmp = *this;
mPtr = byteSub(mPtr, stride());
return tmp;
}
PX_INLINE PxStrideIterator operator+(unsigned int i) const
{
return PxStrideIterator(byteAdd(mPtr, i * stride()), stride());
}
PX_INLINE PxStrideIterator operator-(unsigned int i) const
{
return PxStrideIterator(byteSub(mPtr, i * stride()), stride());
}
PX_INLINE PxStrideIterator& operator+=(unsigned int i)
{
mPtr = byteAdd(mPtr, i * stride());
return *this;
}
PX_INLINE PxStrideIterator& operator-=(unsigned int i)
{
mPtr = byteSub(mPtr, i * stride());
return *this;
}
PX_INLINE int operator-(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
return byteDiff / static_cast<int>(stride());
}
PX_INLINE bool operator==(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr == other.mPtr;
}
PX_INLINE bool operator!=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr != other.mPtr;
}
PX_INLINE bool operator<(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr < other.mPtr;
}
PX_INLINE bool operator>(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr > other.mPtr;
}
PX_INLINE bool operator<=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr <= other.mPtr;
}
PX_INLINE bool operator>=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr >= other.mPtr;
}
private:
PX_INLINE static T* byteAdd(T* ptr, PxU32 bytes)
{
return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) + bytes));
}
PX_INLINE static T* byteSub(T* ptr, PxU32 bytes)
{
return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) - bytes));
}
PX_INLINE bool isCompatible(const PxStrideIterator& other) const
{
int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
return (stride() == other.stride()) && (abs(byteDiff) % stride() == 0);
}
T* mPtr;
PxU32 mStride;
};
template <typename T>
PX_INLINE PxStrideIterator<T> operator+(int i, PxStrideIterator<T> it)
{
it += i;
return it;
}
template <typename T>
PX_INLINE PxStrideIterator<T> PxMakeIterator(T* ptr, PxU32 stride = sizeof(T))
{
return PxStrideIterator<T>(ptr, stride);
}
template <typename T>
PX_INLINE PxStrideIterator<const T> PxMakeIterator(const T* ptr, PxU32 stride = sizeof(T))
{
return PxStrideIterator<const T>(ptr, stride);
}
#if !PX_DOXYGEN
} // namespace physx
#endif
#endif