Stellarium 0.12.4
StelIndexBuffer.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2012 Ferdinand Majerech
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELINDEXBUFFER_HPP_
21 #define _STELINDEXBUFFER_HPP_
22 
23 
24 #include <QtGlobal>
25 
26 
28 enum IndexType
29 {
35  IndexType_U16,
37  IndexType_U32
38 };
39 
46 {
47 public:
49  virtual ~StelIndexBuffer(){}
50 
56  void addIndex(const uint index)
57  {
58  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
59  "Trying to set an index in a locked index buffer");
60  Q_ASSERT_X(indexType_ == IndexType_U32 || index < 65536, Q_FUNC_INFO,
61  "Trying to add a 16-bit index with value greater than 65536");
62 
63  addIndex_(index);
64  ++indexCount_;
65  }
66 
73  uint getIndex(const int which) const
74  {
75  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
76  "Trying to get an index from a locked index buffer");
77  Q_ASSERT_X(which < indexCount_, Q_FUNC_INFO,
78  "Index to an index buffer element out of bounds");
79  return getIndex_(which);
80  }
81 
88  void setIndex(const int which, const uint index)
89  {
90  Q_ASSERT_X(!locked_, Q_FUNC_INFO,
91  "Trying to set an index in a locked index buffer");
92  Q_ASSERT_X(which < indexCount_, Q_FUNC_INFO,
93  "Index to an index buffer element out of bounds");
94  Q_ASSERT_X(indexType_ == IndexType_U32 || index < 65536, Q_FUNC_INFO,
95  "Trying to set a 16-bit index to a value greater than 65536");
96  setIndex_(which, index);
97  }
98 
106  void clear()
107  {
108  Q_ASSERT_X(!locked_, Q_FUNC_INFO, "Trying to clear a locked index buffer");
109  clear_();
110  indexCount_ = 0;
111  }
112 
114  void lock()
115  {
116  locked_ = true;
117  lock_();
118  }
119 
121  void unlock()
122  {
123  unlock_();
124  locked_ = false;
125  }
126 
128  bool locked() const
129  {
130  return locked_;
131  }
132 
134  IndexType indexType() const
135  {
136  return indexType_;
137  }
138 
140  int length() const
141  {
142  return indexCount_;
143  }
144 
145 protected:
147  const IndexType indexType_;
148 
151  : indexType_(indexType)
152  , locked_(false)
153  , indexCount_(0)
154  {
155  }
156 
160  virtual void addIndex_(const uint index) = 0;
161 
165  virtual uint getIndex_(const int which) const = 0;
166 
170  virtual void setIndex_(const int which, const uint index) = 0;
171 
175  virtual void clear_() = 0;
176 
180  virtual void lock_() = 0;
181 
185  virtual void unlock_() = 0;
186 
187 private:
189  bool locked_;
190 
192  int indexCount_;
193 };
194 
195 
196 #endif //_STELINDEXBUFFER_HPP_
197