Apply the new ground_level method.
[crawl.git] / crawl-ref / source / fixedvector.h
blobbc72ed25a3770287ba1cf61381d145e8405fca4b
1 /*
2 * File: fixedvector.h
3 * Summary: Fixed size vector class that asserts if you do something bad.
4 * Written by: Jesse Jones
5 */
7 #ifndef FIXVEC_H
8 #define FIXVEC_H
10 #include <cstdarg>
11 #include <cstring>
13 #include "debug.h"
14 // ==========================================================================
15 // class FixedVector
16 // ==========================================================================
18 template <class TYPE, int SIZE> class FixedVector
21 //-----------------------------------
22 // Types
24 public:
25 typedef TYPE value_type;
26 typedef TYPE& reference;
27 typedef const TYPE& const_reference;
28 typedef TYPE* pointer;
29 typedef const TYPE* const_pointer;
31 typedef unsigned long size_type;
32 typedef long difference_type;
34 typedef TYPE* iterator;
35 typedef const TYPE* const_iterator;
37 //-----------------------------------
38 // Initialization/Destruction
40 public:
41 ~FixedVector() {}
43 FixedVector() {}
45 FixedVector(TYPE def) : mData()
47 init(def);
50 FixedVector(TYPE value0, TYPE value1, ...);
51 // Allows for something resembling C array initialization, eg
52 // instead of "int a[3] = {0, 1, 2}" you'd use "FixedVector<int, 3>
53 // a(0, 1, 2)". Note that there must be SIZE arguments.
55 //-----------------------------------
56 // API
58 public:
59 // ----- Size -----
60 bool empty() const { return SIZE == 0; }
61 size_t size() const { return SIZE; }
63 // ----- Access -----
64 TYPE& operator[](unsigned long index)
66 #ifdef ASSERTS
67 if (index >= SIZE)
69 // Intentionally printed as signed, it's very, very unlikely we'd
70 // have a genuine big number here, but underflows are common.
71 die("FixedVector out of bounds (%ld / %ld)", (signed long)index,
72 SIZE);
74 #endif
75 return mData[index];
78 const TYPE& operator[](unsigned long index) const
80 #ifdef ASSERTS
81 if (index >= SIZE)
83 die("FixedVector out of bounds (%ld / %ld)", (signed long)index,
84 SIZE);
86 #endif
87 return mData[index];
90 const TYPE* buffer() const { return mData; }
91 TYPE* buffer() { return mData; }
93 // ----- Iterating -----
94 iterator begin() { return mData; }
95 const_iterator begin() const { return mData; }
97 iterator end() { return this->begin() + this->size(); }
98 const_iterator end() const { return this->begin() + this->size(); }
99 void init(const TYPE& def);
101 //-----------------------------------
102 // Member Data
104 protected:
105 TYPE mData[SIZE];
109 // ==========================================================================
110 // Outlined Methods
111 // ==========================================================================
112 template <class TYPE, int SIZE>
113 FixedVector<TYPE, SIZE>::FixedVector(TYPE value0, TYPE value1, ...)
115 mData[0] = value0;
116 mData[1] = value1;
118 va_list ap;
119 va_start(ap, value1); // second argument is last fixed parameter
121 for (int index = 2; index < SIZE; index++)
123 TYPE value = va_arg(ap, TYPE);
124 mData[index] = value;
127 va_end(ap);
130 template <class TYPE, int SIZE>
131 void FixedVector<TYPE, SIZE>::init(const TYPE& def)
133 for (int i = 0; i < SIZE; ++i)
134 mData[i] = def;
137 #endif // FIXVEC_H