Files
devilutionX/Source/utils/static_vector.hpp
T
Gleb Mazovetskiy 3b2e560fa0 Non-int versions of Point and Displacement (#4824)
* Non-int versions of `Point` and `Displacement`

This will allow us to make some structs, such as `ActorPosition`, much
smaller.

* ActorPosition: Use smaller types

`Monsters`: 56K -> 46K

* player.cpp: Reduce size of `DirectionSettings`

* CrawlTable: Displacement -> DisplacementOf<int8_t>

* CrawlTable: vector<vector> -> array<vector>

Also only allocate one vector during construction instead of two.
A bit less indirection.

* Monster#enemyPosition: Point -> WorldTilePosition

sizeof(Monster): 240 -> 232

* Monster: Further optimize field layout and sizes

sizeof(Monster): 232 -> 208

`Monsters` is down to 40,000 bytes

* DMonsterStr: _mx/_my -> position
2022-07-02 01:49:17 +02:00

87 lines
1.7 KiB
C++

#pragma once
#include <cstddef>
#include <initializer_list>
#include <memory>
#include <type_traits>
#include <utility>
#include "appfat.h"
namespace devilution {
/**
* @brief A stack-allocated vector with a fixed capacity.
*
* @tparam T element type.
* @tparam N capacity.
*/
template <class T, size_t N>
class StaticVector {
public:
StaticVector() = default;
template <typename U>
StaticVector(std::initializer_list<U> elements)
{
for (auto &&element : elements) {
emplace_back(element);
}
}
[[nodiscard]] const T *begin() const
{
return &(*this)[0];
}
[[nodiscard]] const T *end() const
{
return begin() + size_;
}
[[nodiscard]] size_t size() const
{
return size_;
}
template <typename... Args>
T &emplace_back(Args &&...args) // NOLINT(readability-identifier-naming)
{
assert(size_ < N);
::new (&data_[size_]) T(std::forward<Args>(args)...);
#if __cplusplus >= 201703L
T &result = *std::launder(reinterpret_cast<T *>(&data_[size_]));
#else
T &result = *reinterpret_cast<T *>(&data_[size_]);
#endif
++size_;
return result;
}
const T &operator[](std::size_t pos) const
{
#if __cplusplus >= 201703L
return *std::launder(reinterpret_cast<const T *>(&data_[pos]));
#else
return *reinterpret_cast<const T *>(&data_[pos]);
#endif
}
~StaticVector()
{
for (std::size_t pos = 0; pos < size_; ++pos) {
#if __cplusplus >= 201703L
std::destroy_at(std::launder(reinterpret_cast<T *>(&data_[pos])));
#else
reinterpret_cast<T *>(&data_[pos])->~T();
#endif
}
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> data_[N];
std::size_t size_ = 0;
};
} // namespace devilution