OpenImageIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
varyingref.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 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 
34 
35 #ifndef OPENIMAGEIO_VARYINGREF_H
36 #define OPENIMAGEIO_VARYINGREF_H
37 
38 #include "version.h"
39 
40 OIIO_NAMESPACE_ENTER
41 {
42 
128 
129 template<class T>
130 class VaryingRef {
131 public:
132  VaryingRef () { init (0, 0); }
133 
138  VaryingRef (void *ptr_, int step_=0) { init ((T *)ptr_,step_); }
139 
142  VaryingRef (T &ptr_) { init (&ptr_, 0); }
143 
148  void init (T *ptr_, int step_=0) {
149  m_ptr = ptr_;
150  m_step = step_;
151  }
152 
155  const VaryingRef & operator= (T &ptr_) { init (&ptr_); return *this; }
156 
159  bool is_null () const { return (m_ptr == 0); }
160 
163  operator void*() const { return m_ptr; }
164 
167  bool is_varying () const { return (m_step != 0); }
168 
171  bool is_uniform () const { return (m_step == 0); }
172 
177  VaryingRef & operator++ () { // Prefix form ++i
178  char *p = (char *)m_ptr;
179  p += m_step;
180  m_ptr = (T *) p;
181  return *this;
182  }
187  void operator++ (int) { // Postfix form i++ : return nothing to avoid copy
188  // VaryingRef<T> tmp = *this;
189  char *p = (char *)m_ptr;
190  p += m_step;
191  m_ptr = (T *) p;
192  // return tmp;
193  }
194 
197  T & operator* () const { return *m_ptr; }
198 
202  T & operator[] (int i) const { return *(T *) ((char *)m_ptr + i*m_step); }
203 
206  T * ptr () const { return m_ptr; }
207 
210  int step () const { return m_step; }
211 
212 private:
213  T *m_ptr;
214  int m_step;
215 };
216 
217 
218 
221 template<class T>
222 VaryingRef<T> Varying (T *x) { return VaryingRef<T> (x, sizeof(T)); }
223 
226 template<class T>
227 VaryingRef<T> Uniform (T *x) { return VaryingRef<T> (x, 0); }
228 
231 template<class T>
232 VaryingRef<T> Uniform (T &x) { return VaryingRef<T> (&x, 0); }
233 
234 
235 }
236 OIIO_NAMESPACE_EXIT
237 
238 #endif // OPENIMAGEIO_VARYINGREF_H