]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/core/external_1D.h
beginnings of scene rendering
[chaz/yoink] / src / Moof / cml / core / external_1D.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 *
12 * Defines the fixed-size and runtime-sized external 1D arrays.
13 *
14 * @todo Need a better way to designate non-resizable, run-time sized
15 * arrays (e.g. by a resizeable tag).
16 */
17
18 #ifndef external_1D_h
19 #define external_1D_h
20
21 #include <cml/core/common.h>
22 #include <cml/core/cml_meta.h>
23 #include <cml/core/cml_assert.h>
24 #include <cml/external.h>
25
26 namespace cml {
27
28 /** Fixed-size external 1D array.
29 *
30 * Both the memory and the size are fixed at compile time, and cannot be
31 * changed.
32 */
33 template<typename Element, int Size = -1>
34 class external_1D
35 {
36 public:
37
38 /* Require Size > 0: */
39 CML_STATIC_REQUIRE(Size > 0);
40
41 /* Record the generator: */
42 typedef external<Size,-1> generator_type;
43
44 /* Standard: */
45 typedef Element value_type;
46 typedef Element* pointer;
47 typedef Element& reference;
48 typedef const Element& const_reference;
49 typedef const Element* const_pointer;
50
51 /* Array implementation: */
52 typedef value_type array_impl[Size];
53
54 /* For matching by memory type: */
55 typedef external_memory_tag memory_tag;
56
57 /* For matching by size type: */
58 typedef fixed_size_tag size_tag;
59
60 /* For matching by resizability: */
61 typedef not_resizable_tag resizing_tag;
62
63 /* For matching by dimensions: */
64 typedef oned_tag dimension_tag;
65
66
67 public:
68
69 /** The length as an enumerated value. */
70 enum { array_size = Size };
71
72
73 public:
74
75 external_1D(pointer const ptr)
76 : m_data(ptr) {}
77
78
79 public:
80
81 /** Return the number of elements in the array. */
82 size_t size() const { return size_t(array_size); }
83
84 /** Access to the data as a C array.
85 *
86 * @param i a size_t index into the array.
87 * @return a mutable reference to the array value at i.
88 *
89 * @note This function does not range-check the argument.
90 */
91 reference operator[](size_t i) { return m_data[i]; }
92
93 /** Const access to the data as a C array.
94 *
95 * @param i a size_t index into the array.
96 * @return a const reference to the array value at i.
97 *
98 * @note This function does not range-check the argument.
99 */
100 const_reference operator[](size_t i) const { return m_data[i]; }
101
102 /** Return access to the data as a raw pointer. */
103 pointer data() { return m_data; }
104
105 /** Return access to the data as a raw pointer. */
106 const_pointer data() const { return m_data; }
107
108
109 protected:
110
111 pointer const m_data;
112
113
114 private:
115
116 /* Initialization without an argument isn't allowed: */
117 external_1D();
118 };
119
120 /** Run-time sized external 1D array.
121 *
122 * Both the memory and the size are fixed at run-time, and cannot be
123 * changed. This is a specialization for the case that Rows and Cols are
124 * not specified (i.e. given as the default of -1,-1).
125 */
126 template<typename Element>
127 class external_1D<Element,-1>
128 {
129 public:
130
131 /* Record the generator. Note: this is *not* unique, as it is the same
132 * generator used by external_2D. However, external_2D is used only by
133 * matrix<> classes, so this is not a problem.
134 */
135 typedef external<> generator_type;
136
137 /* Standard: */
138 typedef Element value_type;
139 typedef Element* pointer;
140 typedef Element& reference;
141 typedef const Element& const_reference;
142 typedef const Element* const_pointer;
143
144 /* For matching by memory type: */
145 typedef external_memory_tag memory_tag;
146
147 /* For matching by size type: */
148 typedef dynamic_size_tag size_tag;
149
150 /* For matching by resizability: */
151 typedef not_resizable_tag resizing_tag;
152
153 /* For matching by dimensions: */
154 typedef oned_tag dimension_tag;
155
156
157 public:
158
159 /** The length as an enumerated value. */
160 enum { array_size = -1 };
161
162
163 public:
164
165 external_1D(pointer const ptr, size_t size)
166 : m_data(ptr), m_size(size) {}
167
168
169 public:
170
171 /** Return the number of elements in the array. */
172 size_t size() const { return m_size; }
173
174 /** Access to the data as a C array.
175 *
176 * @param i a size_t index into the array.
177 * @return a mutable reference to the array value at i.
178 *
179 * @note This function does not range-check the argument.
180 */
181 reference operator[](size_t i) { return m_data[i]; }
182
183 /** Const access to the data as a C array.
184 *
185 * @param i a size_t index into the array.
186 * @return a const reference to the array value at i.
187 *
188 * @note This function does not range-check the argument.
189 */
190 const_reference operator[](size_t i) const { return m_data[i]; }
191
192 /** Return access to the data as a raw pointer. */
193 pointer data() { return m_data; }
194
195 /** Return access to the data as a raw pointer. */
196 const_pointer data() const { return m_data; }
197
198
199 protected:
200
201 pointer const m_data;
202 const size_t m_size;
203
204
205 private:
206
207 /* Initialization without an argument isn't allowed: */
208 external_1D();
209 };
210
211 } // namespace cml
212
213 #endif
214
215 // -------------------------------------------------------------------------
216 // vim:ft=cpp
This page took 0.042308 seconds and 4 git commands to generate.