LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
roleditemsmodel.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
12 
13 namespace LC::Util
14 {
15  template<size_t N>
16  struct Literal
17  {
18  char Chars_ [N];
19 
20  constexpr Literal (const char (&s) [N])
21  {
22  std::copy_n (s, N, Chars_);
23  }
24  };
25 
26  template<Literal RoleArg, auto GetterArg>
28  {
29  static constexpr auto Getter = GetterArg;
30  static constexpr auto Role = RoleArg;
31  };
32 
33  template<Literal RoleArg, auto GetterArg>
35 
36  template<typename T>
38  {
39  public:
40  using FieldGetter_t = QVariant (*) (const T&);
41  using FieldsList_t = QVector<QPair<QByteArray, FieldGetter_t>>;
42  private:
43  const QVector<FieldGetter_t> Fields_;
44  const QHash<int, QByteArray> Roles_;
45  public:
46  template<typename... Fields>
47  RoledItemsModel (QObject *parent, Fields...) noexcept
48  : FlatItemsModelTypedBase<T> { QStringList { {} }, parent }
49  , Fields_ { +[] (const T& t) -> QVariant { return t.*(Fields::Getter); }... }
50  , Roles_ { MakeRoles ({ QByteArray { Fields::Role.Chars_ }... }) }
51  {
52  }
53 
54  QHash<int, QByteArray> roleNames () const override
55  {
56  return Roles_;
57  }
58  protected:
59  QVariant GetData (int row, int, int role) const override
60  {
61  if (const auto getter = Fields_.value (role - this->DataRole - 1))
62  return getter (this->Items_.at (row));
63  return {};
64  }
65  private:
66  QHash<int, QByteArray> MakeRoles (QVector<QByteArray> fields) const
67  {
69  result.reserve (result.size () + fields.size ());
70  for (int i = 0; i < fields.size (); ++i)
71  result [this->DataRole + i + 1] = std::move (fields [i]);
72  return result;
73  }
74  };
75 }
QVector< QPair< QByteArray, FieldGetter_t > > FieldsList_t
QHash< int, QByteArray > roleNames() const override
QVariant GetData(int row, int, int role) const override
static constexpr auto Getter
QModelIndex parent(const QModelIndex &) const override
RoledMemberField< RoleArg, GetterArg > RoledMemberField_v
Fields_t Fields_
constexpr Literal(const char(&s) [N])
static constexpr auto Role
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:41
constexpr detail::MemberPtrs< Ptrs... > fields
Definition: oral.h:954
QVariant(*)(const T &) FieldGetter_t
static constexpr auto DataRole
RoledItemsModel(QObject *parent, Fields...) noexcept