/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #ifndef CRYINCLUDE_CRYCOMMON_STRIDEDPTR_H #define CRYINCLUDE_CRYCOMMON_STRIDEDPTR_H #pragma once template class strided_pointer { public: strided_pointer() : data(0) , iStride(sizeof(dtype)) { } strided_pointer(dtype* pdata, int32 stride = sizeof(dtype)) : data(pdata) , iStride(stride) { } template strided_pointer(dtype1* pdata) { set(pdata, sizeof(dtype1)); } template strided_pointer(const strided_pointer& src) { set(src.data, src.iStride); } template ILINE strided_pointer& operator=(const strided_pointer& src) { set(src.data, src.iStride); return *this; } ILINE dtype& operator[](int32 idx) { return *(dtype*)((char*)data + idx * iStride); } ILINE const dtype& operator[](int32 idx) const { return *(const dtype*)((const char*)data + idx * iStride); } ILINE strided_pointer operator+(int32 idx) const { return strided_pointer((dtype*)((char*)data + idx * iStride), iStride); } ILINE strided_pointer operator-(int32 idx) const { return strided_pointer((dtype*)((char*)data - idx * iStride), iStride); } ILINE operator bool() const { return data != 0; } private: template ILINE void set(dtype1* pdata, int32 stride) { # if !defined(eLittleEndian) # error eLittleEndian is not defined, please include CryEndian.h. # endif COMPILE_TIME_ASSERT(metautils::is_const::value || !metautils::is_const::value); // note: we allow xint32 -> xint16 converting COMPILE_TIME_ASSERT( (metautils::is_same::type, typename metautils::remove_const::type>::value || ((metautils::is_same::type, sint32>::value || metautils::is_same::type, uint32>::value || metautils::is_same::type, sint16>::value || metautils::is_same::type, uint16>::value) && (metautils::is_same::type, sint16>::value || metautils::is_same::type, uint16>::value)))); data = (dtype*)pdata + (sizeof(dtype1) / sizeof(dtype) - 1) * (int)eLittleEndian; iStride = stride; } private: // Prevents assignment of a structure member's address by mistake: // stridedPtrObject = &myStruct.member; // Use explicit constructor instead: // stridedPtrObject = strided_pointer(&myStruct.member, sizeof(myStruct)); // // Keep it private and non-implemented. strided_pointer& operator=(dtype* pdata); // Prevents using the address directly by mistake: // memcpy(destination, stridedPtrObject, sizeof(baseType) * n); // Use address of the first element instead: // memcpy(destination, &stridedPtrObject[0], stridedPtrObject.iStride * n); // // Keep it private and non-implemented. operator void*() const; // Prevents direct dereferencing to avoid confusing it with "normal" pointers: // val = *stridedPtrObject; // Use operator [] instead: // val = stridedPtrObject[0]; // // Keep it private and non-implemented. dtype& operator*() const; public: dtype* data; int32 iStride; }; #endif // CRYINCLUDE_CRYCOMMON_STRIDEDPTR_H