]> Dogcows Code - chaz/yoink/blob - src/moof/cml/core/fixed_1D.h
eb691af1a10d01c99b6e26b282fbe149862cb7ba
[chaz/yoink] / src / moof / cml / core / fixed_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
13 #ifndef fixed_1D_h
14 #define fixed_1D_h
15
16 #include <cml/core/common.h>
17 #include <cml/core/cml_meta.h>
18 #include <cml/core/cml_assert.h>
19 #include <cml/fixed.h>
20
21 namespace cml {
22
23 /** Statically-allocated array.
24 *
25 * @note This class is designed to have the same size as a C array with the
26 * same length. It's therefore possible (but not recommended!) to coerce
27 * a normal C array into a fixed_1D<> like this:
28 *
29 * typedef fixed_1D<double,10> array;
30 * double c_array[10];
31 * array& array_object = *((array*)&c_array);
32 * double e1 = array_object[1];
33 *
34 * It's also possible to do this with a pointer to an array of values (e.g. a
35 * double*), whether or not it was actually declared as a fixed C array. This
36 * is HIGHLY DISCOURAGED, though. It's relatively straightforward to implement
37 * a separate class to take a C array (or pointer) and turn it into an array
38 * object.
39 *
40 * @sa cml::fixed
41 *
42 * @internal Do <em>not</em> add the empty constructor and destructor; at
43 * least one compiler (Intel C++ 9.0) fails to optimize them away, and they
44 * aren't needed anyway here.
45 */
46 template<typename Element, int Size>
47 class fixed_1D
48 {
49 public:
50
51 /* Require Size > 0: */
52 CML_STATIC_REQUIRE(Size > 0);
53
54 /* Record the generator: */
55 typedef fixed<Size,-1> generator_type;
56
57 /* Standard: */
58 typedef Element value_type;
59 typedef Element* pointer;
60 typedef Element& reference;
61 typedef const Element& const_reference;
62 typedef const Element* const_pointer;
63
64 /* Array implementation: */
65 typedef value_type array_impl[Size];
66
67 /* For matching by memory type: */
68 typedef fixed_memory_tag memory_tag;
69
70 /* For matching by size type: */
71 typedef fixed_size_tag size_tag;
72
73 /* For matching by resizability: */
74 typedef not_resizable_tag resizing_tag;
75
76 /* For matching by dimensions: */
77 typedef oned_tag dimension_tag;
78
79
80 public:
81
82 /** The length as an enumerated value. */
83 enum { array_size = Size };
84
85
86 public:
87
88 /** Return the number of elements in the array. */
89 size_t size() const { return size_t(array_size); }
90
91 /** Access to the data as a C array.
92 *
93 * @param i a size_t index into the array.
94 * @return a mutable reference to the array value at i.
95 *
96 * @note This function does not range-check the argument.
97 */
98 reference operator[](size_t i) { return m_data[i]; }
99
100 /** Const access to the data as a C array.
101 *
102 * @param i a size_t index into the array.
103 * @return a const reference to the array value at i.
104 *
105 * @note This function does not range-check the argument.
106 */
107 const_reference operator[](size_t i) const { return m_data[i]; }
108
109 /** Return access to the data as a raw pointer. */
110 pointer data() { return &m_data[0]; }
111
112 /** Return access to the data as a raw pointer. */
113 const_pointer data() const { return &m_data[0]; }
114
115 protected:
116
117 fixed_1D() {}
118
119
120 protected:
121
122 array_impl m_data;
123 };
124
125 } // namespace cml
126
127 #endif
128
129 // -------------------------------------------------------------------------
130 // vim:ft=cpp
This page took 0.037818 seconds and 3 git commands to generate.