1 #ifndef SAKI_UTIL_STACTOR_H 2 #define SAKI_UTIL_STACTOR_H 22 constexpr T* launder(T* p) noexcept
27 return std::launder(p);
37 explicit Range(
const T *begin,
const T *end) : mBegin(begin), mEnd(end) {}
38 const T *begin()
const noexcept {
return mBegin; }
39 const T *end()
const noexcept {
return mEnd; }
40 bool empty()
const noexcept {
return mBegin == mEnd; }
49 template<
typename T,
size_t MAX>
55 StactorBase(std::initializer_list<T> inits) noexcept
57 for (
const auto &e : inits)
64 using const_iterator =
const T *;
66 const T *data() const noexcept
68 return launder(reinterpret_cast<const T *>(mData));
73 return launder(reinterpret_cast<T *>(mData));
76 T &operator[](
size_t i) noexcept
82 const T &operator[](
size_t i)
const noexcept
88 T &at(
size_t i) noexcept
93 const T &at(
size_t i)
const noexcept
98 bool empty()
const noexcept
103 bool full()
const noexcept
108 size_t size()
const noexcept
113 int iSize()
const noexcept
115 return static_cast<int>(size());
123 const T *begin()
const noexcept
130 return begin() + mSize;
133 const T *end()
const noexcept
135 return begin() + mSize;
146 return operator[](0);
149 const T &front()
const noexcept
152 return operator[](0);
158 return operator[](mSize - 1);
161 const T &back()
const noexcept
164 return operator[](mSize - 1);
167 template<
typename... Args>
168 void emplaceBack(Args && ... elem) noexcept
170 assert(mSize + 1 <= MAX);
171 new (mData + mSize) T(std::forward<Args>(elem) ...);
175 void pushBack(
const T &elem) noexcept
177 assert(mSize + 1 <= MAX);
178 new (mData + mSize) T(elem);
182 void pushBack(T &&elem) noexcept
184 assert(mSize + 1 <= MAX);
185 new (mData + mSize) T(std::move(elem));
189 void pushBack(
const Range<T> &range) noexcept
191 for (
const T &v : range)
204 pushBack(std::forward<T>(elem));
207 void popBack() noexcept
210 (data() + mSize - 1)->T::~T();
214 void clear() noexcept
221 static const size_t ALIGN = std::alignment_of_v<T>;
222 std::aligned_storage_t<sizeof(T), ALIGN> mData[MAX];
246 template<
typename T,
size_t MAX,
typename SFINAE =
void>
251 template<
typename T,
size_t MAX>
252 class Stactor<T, MAX,
std::enable_if_t<std::is_trivially_copyable_v<T>>>
261 template<
typename T,
size_t MAX>
262 class Stactor<T, MAX,
std::enable_if_t<!std::is_trivially_copyable_v<T>>>
268 using Base::StactorBase;
277 for (
const auto &e : copy)
282 :
Stactor(static_cast<const Stactor &>(move))
289 for (
const auto &e : that)
295 *
this =
static_cast<const Stactor &
>(that);
311 #endif // SAKI_UTIL_STACTOR_H void push_back(const T &elem) noexcept
alias to enable std::back_inserter
Definition: stactor.h:196
void push_back(T &&elem) noexcept
alias to enable std::back_inserter
Definition: stactor.h:202
Definition: json.hpp:16563
T value_type
enable std funcs
Definition: stactor.h:62
Stactor = statically allocated vector.
Definition: stactor.h:247