AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AFlatVector.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Common/AVector.h>
   15#include <utility>
   16
   17
   23template<typename T, std::size_t MAX_SIZE>
   24class [[deprecated("deprecated in favour of AStaticVector")]] AFlatVector {
   25public:
   26
   27    ~AFlatVector() {
   28        destructAll();
   29    }
   30
   31    std::size_t size() const noexcept {
   32        return mSize;
   33    }
   34
   35    T& operator[](std::size_t index) noexcept {
   36        AUI_ASSERTX(index < size(), "index out of bounds");
   37        return data()[index];
   38    }
   39
   40    const T& operator[](std::size_t index) const noexcept {
   41        AUI_ASSERTX(index < size(), "index out of bounds");
   42        return data()[index];
   43    }
   44
   45    [[nodiscard]]
   46    T& at(std::size_t index) {
   47        if (index >= size()) {
   48            aui::impl::outOfBoundsException();
   49        }
   50        return operator[](index);
   51    }
   52
   53    [[nodiscard]]
   54    const T& at(std::size_t index) const {
   55        if (index >= size()) {
   56            aui::impl::outOfBoundsException();
   57        }
   58        return operator[](index);
   59    }
   60
   61    T* data() noexcept {
   62        return reinterpret_cast<T*>(&mStorage);
   63    }
   64
   65    const T* data() const noexcept {
   66        return reinterpret_cast<const T*>(&mStorage);
   67    }
   68
   69    void clear() {
   70        destructAll();
   71        mSize = 0;
   72    }
   73
   74    void push_back(T value) {
   75        AUI_ASSERTX(mSize < MAX_SIZE, "size exceeded");
   76        new (data() + mSize) T(std::move(value));
   77    }
   78
   79    AFlatVector& operator<<(T value) {
   80        push_back(std::move(value));
   81        return *this;
   82    }
   83
   84
   85
   86private:
   87    std::size_t mSize = 0;
   88    std::aligned_storage_t<sizeof(T) * MAX_SIZE, alignof(T)> mStorage;
   89
   90    void destructAll() {
   91        for (size_t i = 0; i < size(); ++i) {
   92            data()[i].~T();
   93        }
   94    }
   95};
Stack-only vector implementation.
Definition AFlatVector.h:24
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74