godot_voxel/util/fixed_array.h
Marc Gilleron 922f361cb0 Made it compile in Godot 4
- Made a bunch of changes to comply with Godot 4 API
- Use Godot's Vector3i and add the missing stuff with helper functions
- Transvoxel uses custom attributes API, the old way would not work
- Wrap MeshOptimizer in a unique namespace (see build script why)
- Added clang-format file for the module as some rules now differ
- Prevent thirdparty code and lookup tables from being clang-formatted
- Very likely full of runtime bugs that need fixing
2021-12-13 21:38:10 +00:00

79 lines
1.4 KiB
C++

#ifndef FIXED_ARRAY_H
#define FIXED_ARRAY_H
#include <core/error/error_macros.h>
// TODO Could use std::array, but due to how Godot compiles,
// I couldn't find a way to enable boundary checks without failing to link my module with the rest of Godot...
template <typename T, unsigned int N>
class FixedArray {
public:
inline FixedArray() {}
inline FixedArray(T defval) {
fill(defval);
}
inline void fill(T v) {
for (unsigned int i = 0; i < N; ++i) {
_data[i] = v;
}
}
// TODO Optimization: move semantics
inline T &operator[](unsigned int i) {
#ifdef DEBUG_ENABLED
CRASH_COND(i >= N);
#endif
return _data[i];
}
inline const T &operator[](unsigned int i) const {
#ifdef DEBUG_ENABLED
CRASH_COND(i >= N);
#endif
return _data[i];
}
inline bool equals(const FixedArray<T, N> &other) const {
for (unsigned int i = 0; i < N; ++i) {
if (_data[i] != other._data[i]) {
return false;
}
}
return true;
}
inline bool operator==(const FixedArray<T, N> &other) const {
return equals(other);
}
inline bool operator!=(const FixedArray<T, N> &other) const {
return !equals(other);
}
inline void operator=(const FixedArray<T, N> &other) {
for (unsigned int i = 0; i < N; ++i) {
_data[i] = other._data[i];
}
}
inline T *data() {
return _data;
}
inline const T *data() const {
return _data;
}
inline unsigned int size() const {
return N;
}
private:
T _data[N];
};
#endif // FIXED_ARRAY_H