AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ATreeModelIndex.h
    1#pragma once
    2
    3#include <AUI/Common/AException.h>
    4#include <any>
    5#include <variant>
    6
   11public:
   12    class Exception: public AException {
   13    public:
   14        using AException::AException;
   15    };
   16
   22    explicit ATreeModelIndex(std::size_t row, std::size_t column, std::any userdata) noexcept:
   23        mRow(row),
   24        mColumn(column),
   25        mUserdata(std::move(userdata)) {}
   26
   31    template<typename T>
   32    [[nodiscard]]
   33    T as() const {
   34        try {
   35            return std::any_cast<T>(mUserdata);
   36        } catch (...) {
   37            throw Exception("ATreeModelIndex::as any_cast failed (asked for {}, actually stored {})"_format(
   38                typeid(T).name(), mUserdata.type().name()));
   39        }
   40    }
   41
   42    [[nodiscard]]
   43    bool hasValue() const noexcept {
   44        return mUserdata.has_value();
   45    }
   46
   50    [[nodiscard]]
   51    std::size_t row() const {
   52        return mRow;
   53    }
   54
   58    [[nodiscard]]
   59    std::size_t column() const {
   60        return mColumn;
   61    }
   62
   68    static constexpr struct {} ROOT {};
   69
   70private:
   74    std::size_t mRow;
   75
   79    std::size_t mColumn;
   80    
   84    std::any mUserdata;
   85};
   86
   99using ATreeModelIndexOrRoot = std::variant<ATreeModelIndex, decltype(ATreeModelIndex::ROOT)>;
  100
  101// these allow the following expression:
  102// vertex == ATreeModelIndex::ROOT
  103
  104inline bool operator==(const ATreeModelIndexOrRoot& lhs, const decltype(ATreeModelIndex::ROOT)&) {
  105    return !std::holds_alternative<ATreeModelIndex>(lhs);
  106}
  107
  108inline bool operator!=(const ATreeModelIndexOrRoot& lhs, const decltype(ATreeModelIndex::ROOT)&) {
  109    return std::holds_alternative<ATreeModelIndex>(lhs);
  110}
  111
  112// this allows the following expression:
  113// ATreeModelIndexOrRoot vertex = ...
  114// ATreeModelIndex index = *vertex
  115
  116inline const ATreeModelIndex& operator*(const ATreeModelIndexOrRoot& v) {
  117    if (auto i = std::get_if<ATreeModelIndex>(&v)) {
  118        return *i;
  119    }
  120    throw ATreeModelIndex::Exception("ATreeModelIndexOrRoot holds root");
  121}
Definition ATreeModelIndex.h:12
Valid index of ITreeModel.
Definition ATreeModelIndex.h:10
ATreeModelIndex(std::size_t row, std::size_t column, std::any userdata) noexcept
Definition ATreeModelIndex.h:22
std::size_t row() const
row of the vertex relative to it's parent.
Definition ATreeModelIndex.h:51
static constexpr struct ATreeModelIndex::@102253001377275224255007337255000205374254314015 ROOT
Tag type to define root vertex.
T as() const
any_cast the external user data stored in this vertex.
Definition ATreeModelIndex.h:33
std::size_t column() const
column of the vertex relative to it's parent.
Definition ATreeModelIndex.h:59