AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AArrayView.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
   15#include <cstdint>
   16#include <cstddef>
   17#include <cassert>
   18#include <AUI/Common/AVector.h>
   19#include <AUI/Common/AStaticVector.h>
   20
   21template<typename Vector, typename T>
   22concept AAnyVector = requires(Vector& vector)
   23{
   24    { vector.data() } -> aui::convertible_to<const T*>;
   25    { vector.size() } -> aui::convertible_to<std::size_t>;
   26};
   27
   31template<typename T>
   32class AArrayView {
   33public:
   34    AArrayView(const T* data, std::size_t count) noexcept : mData(data), mCount(count) {}
   35
   36    AArrayView(const AAnyVector<T> auto& vector) noexcept: mData(vector.data()), mCount(vector.size()) {
   37
   38    }
   39
   40    template<std::size_t N>
   41    AArrayView(const T (&rawArray)[N]) noexcept: mData(rawArray), mCount(N) {}
   42
   43    template<std::size_t N>
   44    AArrayView(const std::array<T, N>& array) noexcept: mData(array.data()), mCount(N) {}
   45
   46    [[nodiscard]]
   47    const T* data() const noexcept {
   48        return mData;
   49    }
   50
   51    [[nodiscard]]
   52    size_t size() const noexcept {
   53        return mCount;
   54    }
   55    [[nodiscard]]
   56    size_t sizeInBytes() const noexcept {
   57        return mCount * sizeof(T);
   58    }
   59
   60    const T& operator[](std::size_t index) const noexcept {
   61        AUI_ASSERTX(index < mCount, "out of bounds");
   62        return mData[index];
   63    }
   64
   65    const T* begin() const noexcept {
   66        return mData;
   67    }
   68
   69    const T* end() const noexcept {
   70        return mData + mCount;
   71    }
   72
   73private:
   74    const T* mData;
   75    std::size_t mCount;
   76};
   77
   78template<typename Vector>
   79AArrayView(const Vector& vector) noexcept -> AArrayView<std::decay_t<decltype(*vector.data())>>;
Definition AArrayView.h:32
Definition AArrayView.h:22
Definition concepts.h:42
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74