VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
AttributeArray.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ATTRIBUTEARRAY_H
18 #define ATTRIBUTEARRAY_H
19 
20 #include "RendererCommon.h"
21 
30 template<typename T>
32 public:
37  class Iterator {
38  public:
44  Iterator(GLbyte* ptr, int stride) : mPtr(ptr), mStride(stride) {
45  }
46 
52  bool operator== (const Iterator& other) const {
53  return mPtr == other.mPtr;
54  }
55 
61  bool operator!= (const Iterator& other) const {
62  return !(this->operator ==(other));
63  }
64 
69  const T& operator* () const {
70  return *(T*) (mPtr);
71  }
72 
77  T& operator* () {
78  return *(T*) (mPtr);
79  }
80 
85  T* operator-> () {
86  return (T*) (mPtr);
87  }
88 
92  void operator++ () {
93  mPtr += mStride;
94  }
98  void operator++ (int) {
99  mPtr += mStride;
100  }
104  void operator-- () {
105  mPtr -= mStride;
106  }
110  void operator-- (int) {
111  mPtr -= mStride;
112  }
113  private:
114  GLbyte* mPtr = nullptr;
115  int mStride = 0;
116  };
117 
125  typedef const Iterator const_iterator;
126 
131  }
139  AttributeArray(int count, GLbyte* arrayBase, int stride, const Attribute* attribute)
140  : mCount(count),
141  mArrayBase(arrayBase),
142  mStride(stride),
143  mAttribute(attribute) {
144  }
145 
153  bool isValid() const { return mArrayBase; }
158  const Attribute* getAttribute() const { return mAttribute; }
159 
165  const T& operator[] (int index) const {
166  assert(isValid() && index >= 0 && index < mCount);
167  return *(const T*) (mArrayBase + mStride * index);
168  }
174  T& operator[] (int index) {
175  assert(isValid() && index >= 0 && index < mCount);
176  return *(T*) (mArrayBase + mStride * index);
177  }
178 
182  const Iterator begin() const {
183  assert(isValid());
184  return Iterator(mArrayBase, mStride);
185  }
190  assert(isValid());
191  return Iterator(mArrayBase, mStride);
192  }
196  const Iterator end() const {
197  assert(isValid());
198  return Iterator(mArrayBase + mStride * mCount, mStride);
199  }
204  assert(isValid());
205  return Iterator(mArrayBase + mStride * mCount, mStride);
206  }
207 
216  template<typename Y> AttributeArray<Y> getAsType() const {
217  return AttributeArray<Y>(mCount, mArrayBase, mStride, mAttribute);
218  }
219 private:
220  int mCount = 0;
221  GLbyte* mArrayBase = nullptr;
222  int mStride = 0;
223  const Attribute* mAttribute = nullptr;
224 };
225 
226 #endif // ATTRIBUTEARRAY_H
bool isValid() const
Returns whether this attribute array points to a valid array and can be accessed. ...
Definition: AttributeArray.h:153
Iterator end()
Gets an iterator pointing to immediately beyond the last element of the array.
Definition: AttributeArray.h:203
Header declaring and including types common to renderer classes such as Vector2. Also includes GL hea...
Iterator begin()
Gets an iterator pointing to the beginning of the array.
Definition: AttributeArray.h:189
void operator--()
Moves this iterator back one element.
Definition: AttributeArray.h:104
const T & operator*() const
Returns a constant reference to the current element.
Definition: AttributeArray.h:69
const T & operator[](int index) const
Gets a constant reference to the element at the given index.
Definition: AttributeArray.h:165
The definition of a vertex attribute, such as a position vector, or a texture coordinate.
Definition: Attribute.h:31
void operator++()
Moves this iterator forward one element.
Definition: AttributeArray.h:92
T * operator->()
Arrow operator dereferencing the current element.
Definition: AttributeArray.h:85
AttributeArray< Y > getAsType() const
Reinterprets this array as an array of a different type.
Definition: AttributeArray.h:216
Iterator iterator
Definition: AttributeArray.h:121
An iterator over a vertex attribute array.
Definition: AttributeArray.h:37
Iterator(GLbyte *ptr, int stride)
Construct an iterator.
Definition: AttributeArray.h:44
AttributeArray()
Constructs an attribute array which is marked as invalid.
Definition: AttributeArray.h:130
const Iterator end() const
Gets a const iterator pointing to immediately beyond the last element of the array.
Definition: AttributeArray.h:196
bool operator==(const Iterator &other) const
Compares two iterators to test if they point to the same element.
Definition: AttributeArray.h:52
bool operator!=(const Iterator &other) const
Compares two iterators to test if they do not point to the same element.
Definition: AttributeArray.h:61
const Attribute * getAttribute() const
Gets the definition of the attributes in this array.
Definition: AttributeArray.h:158
const Iterator begin() const
Gets a const iterator pointing to the beginning of the array.
Definition: AttributeArray.h:182
AttributeArray(int count, GLbyte *arrayBase, int stride, const Attribute *attribute)
Constructs an attribute array which points to a buffer in memory.
Definition: AttributeArray.h:139
const Iterator const_iterator
Definition: AttributeArray.h:125
A helper class to access and iterate over an interleaved vertex attribute array.
Definition: AttributeArray.h:31