HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
strided_ptr.h
Go to the documentation of this file.
1 /*
2  Copyright 2014 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 #pragma once
33 
34 #include <cstddef>
35 
37 #include <OpenImageIO/platform.h>
38 
39 
41 
42 
43 /// strided_ptr<T> looks like a 'T*', but it incorporates a stride, so
44 /// it's not limited to adjacent elements.
45 /// Operators ++, --, [], and so on, take the stride into account when
46 /// computing where each "array element" actually exists.
47 ///
48 /// A strided_ptr<T> is mutable (the values pointed to may be modified),
49 /// whereas an strided_ptr<const T> is not mutable.
50 ///
51 /// Fun trick: strided_ptr<T>(&my_value,0) makes a strided_pointer that
52 /// is addressed like an array, but because the stride is 0, every
53 /// accessed "element" actually will actually refer to the same value.
54 ///
55 /// By default, if StrideUnits == sizeof(T), then the stride refers to
56 /// multiples of the size of T. But every once in a while, you need a
57 /// a byte-addressable stride, and in that case you use a StrideUnits
58 /// of 1, like: strided_ptr<T,1>.
59 template<typename T, int StrideUnits = sizeof(T)> class strided_ptr {
60 public:
61  constexpr strided_ptr(T* ptr = nullptr, ptrdiff_t stride = 1) noexcept
62  : m_ptr(ptr)
63  , m_stride(stride)
64  {
65  }
66  constexpr strided_ptr(const strided_ptr& p) noexcept
67  : strided_ptr(p.data(), p.stride())
68  {
69  }
70 
71  const strided_ptr& operator=(const strided_ptr& p) noexcept
72  {
73  m_ptr = p.m_ptr;
74  m_stride = p.m_stride;
75  return *this;
76  }
77 
78  // Assignment of a pointer sets the pointer and implies a stride of 1.
79  const strided_ptr& operator=(T* p) noexcept
80  {
81  m_ptr = p;
82  m_stride = 1;
83  return *this;
84  }
85 
86  constexpr T& operator*() const { return *m_ptr; }
87  constexpr T& operator[](ptrdiff_t pos) const { return get(pos); }
88  constexpr T* data() const { return m_ptr; }
89  constexpr ptrdiff_t stride() const { return m_stride; }
90 
91  // Careful: == and != only compare the pointer
92  constexpr bool operator==(const T* p) const { return m_ptr == p; }
93  constexpr bool operator!=(const T* p) const { return m_ptr != p; }
94 
95  // Increment and decrement moves the pointer to the next element
96  // one stride length away.
98  { // prefix
99  m_ptr = getptr(1);
100  return *this;
101  }
103  { // postfix
104  strided_ptr r(*this);
105  ++(*this);
106  return r;
107  }
109  { // prefix
110  m_ptr = getptr(-1);
111  return *this;
112  }
114  { // postfix
115  strided_ptr r(*this);
116  --(*this);
117  return r;
118  }
119 
120  // Addition and subtraction returns new strided pointers that are
121  // the given number of strides away.
122  constexpr strided_ptr operator+(ptrdiff_t d) const noexcept
123  {
124  return strided_ptr(getptr(d), m_stride);
125  }
126  constexpr strided_ptr operator-(ptrdiff_t d) const noexcept
127  {
128  return strided_ptr(getptr(-d), m_stride);
129  }
130  const strided_ptr& operator+=(ptrdiff_t d) noexcept
131  {
132  m_ptr = getptr(d);
133  return *this;
134  }
135  const strided_ptr& operator-=(ptrdiff_t d)
136  {
137  m_ptr = getptr(-d);
138  return *this;
139  }
140 
141 private:
142  // The implementation of a strided_ptr is just the pointer and a stride.
143  // Note that when computing addressing, the stride is implicitly
144  // multiplied by the StrideUnits, which defaults to sizeof(T), but when
145  // StrideUnits==1 means that your stride value is measured in bytes.
146  T* m_ptr = nullptr;
147  ptrdiff_t m_stride = 1;
148 
149  // getptr is the real brains of the operation, computing the pointer
150  // for a given element, with strides taken into consideration.
151  constexpr inline T* getptr(ptrdiff_t pos = 0) const noexcept
152  {
153  return (T*)((char*)m_ptr + pos * m_stride * StrideUnits);
154  }
155  constexpr inline T& get(ptrdiff_t pos = 0) const noexcept
156  {
157  return *getptr(pos);
158  }
159 };
160 
161 
162 
constexpr strided_ptr operator-(ptrdiff_t d) const noexcept
Definition: strided_ptr.h:126
constexpr T * data() const
Definition: strided_ptr.h:88
const strided_ptr & operator=(T *p) noexcept
Definition: strided_ptr.h:79
Platform-related macros.
constexpr T & operator*() const
Definition: strided_ptr.h:86
constexpr T & operator[](ptrdiff_t pos) const
Definition: strided_ptr.h:87
constexpr bool operator==(const T *p) const
Definition: strided_ptr.h:92
constexpr strided_ptr(T *ptr=nullptr, ptrdiff_t stride=1) noexcept
Definition: strided_ptr.h:61
const strided_ptr & operator--()
Definition: strided_ptr.h:108
GLsizei stride
Definition: glew.h:1523
const strided_ptr & operator=(const strided_ptr &p) noexcept
Definition: strided_ptr.h:71
GLfloat GLfloat p
Definition: glew.h:16321
const strided_ptr & operator+=(ptrdiff_t d) noexcept
Definition: strided_ptr.h:130
const void * ptr(const T *p)
Definition: format.h:3292
const strided_ptr operator--(int)
Definition: strided_ptr.h:113
const strided_ptr & operator++()
Definition: strided_ptr.h:97
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
constexpr strided_ptr(const strided_ptr &p) noexcept
Definition: strided_ptr.h:66
const strided_ptr & operator-=(ptrdiff_t d)
Definition: strided_ptr.h:135
constexpr bool operator!=(const T *p) const
Definition: strided_ptr.h:93
const strided_ptr operator++(int)
Definition: strided_ptr.h:102
#define const
Definition: zconf.h:214
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
constexpr strided_ptr operator+(ptrdiff_t d) const noexcept
Definition: strided_ptr.h:122
constexpr ptrdiff_t stride() const
Definition: strided_ptr.h:89
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65