AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AListModelSelection.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//
   13// Created by alex2 on 04.09.2020.
   14//
   15
   16#pragma once
   17
   18
   19#include "AListModelIndex.h"
   20#include "AListModelRange.h"
   21#include <AUI/Common/ASet.h>
   22#include <AUI/Common/AVector.h>
   23#include <cassert>
   24
   25template<typename T> class IListModel;
   26
   27template <typename T>
   28class AListModelSelection {
   29private:
   30    ASet<AListModelIndex> mIndices;
   31    _<IListModel<T>> mModel;
   32
   33public:
   34    AListModelSelection() = default;
   35    AListModelSelection(const ASet<AListModelIndex>& indices, _<IListModel<T>> model) : mIndices(indices),
   36                                                                                    mModel(std::move(model)) {}
   37
   38    class Iterator {
   39    private:
   40        ASet<AListModelIndex>::iterator mIterator;
   41        _<IListModel<T>> mModel;
   42
   43
   44    public:
   45        Iterator(const ASet<AListModelIndex>::iterator& iterator, _<IListModel<T>> model):
   46            mIterator(iterator), mModel(std::move(model)) {}
   47
   48
   49        Iterator& operator*() {
   50            return *this;
   51        }
   52
   53        auto& operator->() {
   54            return mModel->listItemAt(*mIterator);
   55        }
   56        auto get() const {
   57            return mModel->listItemAt(*mIterator);
   58        }
   59
   60        Iterator& operator++() {
   61            ++mIterator;
   62            return *this;
   63        }
   64
   65        bool operator!=(const Iterator& other) const {
   66            AUI_ASSERT(mModel == other.mModel);
   67            return mIterator != other.mIterator;
   68        }
   69
   70        bool operator==(const Iterator& other) const {
   71            AUI_ASSERT(mModel == other.mModel);
   72            return mIterator == other.mIterator;
   73        }
   74
   75        [[nodiscard]]
   76        const AListModelIndex& getIndex() const {
   77            return *mIterator;
   78        }
   79    };
   80
   81    AListModelSelection<T>::Iterator begin() const {
   82        return {mIndices.begin(), mModel};
   83    }
   84    AListModelSelection<T>::Iterator end() const {
   85        return {mIndices.end(), mModel};
   86    }
   87
   88    [[nodiscard]] size_t size() const {
   89        return mIndices.size();
   90    }
   91    [[nodiscard]] bool empty() const {
   92        return mIndices.empty();
   93    }
   94
   95    const _<IListModel<T>>& getModel() const {
   96        return mModel;
   97    }
   98
   99    const ASet<AListModelIndex>& getIndices() const {
  100        return mIndices;
  101    }
  102
  103    AListModelIndex first() {
  104        AUI_ASSERTX(mIndices.begin() != mIndices.end(), "selection model is empty");
  105        return *mIndices.begin();
  106    }
  107
  108    AVector<AListModelRange<T>> ranges() const noexcept;
  109};
  110
  111#include "IListModel.h"
  112
  113template<typename T>
  114AVector<AListModelRange<T>> AListModelSelection<T>::ranges() const noexcept {
  115    return mModel->rangesIncluding([&](size_t i) {
  116        return mIndices.contains(i);
  117    });
  118}
Definition AListModelIndex.h:20
Definition AListModelSelection.h:38
A std::set with AUI extensions.
Definition ASet.h:25
Definition IListModel.h:23
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74